Remote mp3 file taking a lot of time to play in swift ios

吃可爱长大的小学妹 提交于 2019-12-11 02:54:35

问题


i'm in trouble. i want to play a remote mp3 file in my app. but mp3 file taking a lot of time (approx 5-6 minute) to play. why ?

Anyone can suggest what should i do ?

 import UIKit

 import AVFoundation

 class TestViewController: UIViewController, AVAudioPlayerDelegate {

 var player:AVAudioPlayer = AVAudioPlayer()

 override func viewDidLoad() {
    super.viewDidLoad()
 }

 @IBAction func play(sender: AnyObject) {

    let url = "http://www.example.com/song.mp3"

    let fileURL = NSURL(string: url.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()))

    let soundData = NSData.dataWithContentsOfURL(fileURL, options: nil, error: nil)

    var error: NSError?
    self.player = AVAudioPlayer(data: soundData, error: &error)
    if player == nil
    {
        if let e = error
        {
            println(e.localizedDescription)
        }
    }

    player.volume   = 1.0
    player.delegate = self
    player.prepareToPlay()

    player.play()

 }

}

Thanks in advance.


回答1:


Use AVPlayer instead of AVAudioPlayer for streaming audio. It can be as simple as this --

let urlString = "http://www.example.com/song.mp3"
let url = NSURL(string: urlString)

var avPlayer = AVPlayer(URL: url)
avPlayer.play()

You can also set an observer on the AVPlayer.status property to manage its changing status. Check out: https://stackoverflow.com/a/13156942/2484290 (objective c)

And of course the AVPlayer docs are here: https://developer.apple.com/LIBRARY/ios/documentation/AVFoundation/Reference/AVPlayer_Class/index.html#//apple_ref/occ/cl/AVPlayer




回答2:


Try using my example where you can work with the cache and the remote file. player.automaticallyWaitsToMinimizeStalling = false // by default true, so It is important to solve your problem in a mp3 file remotely

This feature is available iOS 10.0+

var player: AVPlayer!
enum AudioType: String {
    case remote
    case cache
}

@IBAction func remotePressed(_ sender: Any) {
    playAudio(type: .remote, fileURL: "http://www.example.com/song.mp3")
}

@IBAction func cachePressed(_ sender: Any) {
    if let fileURL = Bundle.main.path(forResource: "jordan", ofType: "mp3") {
        playAudio(type: .cache, fileURL: fileURL)
    }
}

private func playAudio(type: AudioType, fileURL: String) {

    let url = type == .cache ? URL.init(fileURLWithPath: fileURL) : URL.init(string: fileURL)
    let playerItem: AVPlayerItem = AVPlayerItem(url: url!)
    player = AVPlayer(playerItem: playerItem)
    player.automaticallyWaitsToMinimizeStalling = false//It's important
    player.play()
}



回答3:


Its may be late. Yes. what @lemon lime pomelo has said. you should use AVPlayer instead of AVAudioPlayer.

Also you can use MediaPlayer.

import UIKit
import MediaPlayer

@IBAction func playSong(sender: AnyObject) {
    print("Song")
    self.playAudio("yourAudioURL.mp3")

}

func playAudio(URL:String){
    let movieurl:NSURL? = NSURL(string: "\(URL)")

    if movieurl != nil {
        self.movie = MPMoviePlayerViewController(contentURL: movieurl!)
    }

    if self.movie != nil {
        self.presentViewController(self.movie!, animated: true, completion: nil)
        self.movie?.moviePlayer.play()
    }
}



回答4:


It is slow because your program downloads all the song before starting playing



来源:https://stackoverflow.com/questions/27066772/remote-mp3-file-taking-a-lot-of-time-to-play-in-swift-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!