How to play mp3 audio from URL in ios swift How to play mp3 audio from URL in ios swift ios ios

How to play mp3 audio from URL in ios swift


Use AVPlayer instead of AVAudioPlayer to play remote content. As per documentation AVAudioPlayer needs mp3 file to play Audio. AVAudioPlayer not provide support for streaming.

Try this code , its working fine for me

func play(url:NSURL) {    print("playing \(url)")    do {        let playerItem = AVPlayerItem(URL: url)        self.player = try AVPlayer(playerItem:playerItem)        player!.volume = 1.0        player!.play()    } catch let error as NSError {        self.player = nil        print(error.localizedDescription)    } catch {        print("AVAudioPlayer init failed")    }}

Please keep in mind to set App Transport Security(ATS) in info.plist file.

<key>NSAppTransportSecurity</key>    <dict>        <key>NSAllowsArbitraryLoads</key>        <true/>    </dict>


I tried the following,

let urlstring = "http://radio.spainmedia.es/wp-content/uploads/2015/12/tailtoddle_lo4.mp3"let url = NSURL(string: urlstring)print("the url = \(url!)")downloadFileFromURL(url!)

Add the below methods,

func downloadFileFromURL(url:NSURL){    var downloadTask:NSURLSessionDownloadTask    downloadTask = NSURLSession.sharedSession().downloadTaskWithURL(url, completionHandler: { [weak self](URL, response, error) -> Void in        self?.play(URL)    })            downloadTask.resume()    }

And your play method as it is,

func play(url:NSURL) {    print("playing \(url)")        do {        self.player = try AVAudioPlayer(contentsOfURL: url)        player.prepareToPlay()        player.volume = 1.0        player.play()    } catch let error as NSError {        //self.player = nil        print(error.localizedDescription)    } catch {        print("AVAudioPlayer init failed")    }    }

Download the mp3 file and then try to play it, somehow AVAudioPlayer does not download your mp3 file for you. I am able to download the audio file and player plays it.

Remember to add this in your info.plist since you are loading from a http source and you need the below to be set for iOS 9+

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict>    <key>NSAllowsArbitraryLoads</key>    <true/></dict></plist>


Since AVPlayer is very limited (for example you cannot change url there without regenerate whole AVPlayer I think you should use AVQueuePlayer:

From the docs:

AVQueuePlayer is a subclass of AVPlayer used to play a number of items in sequence. Using this class you can create and manage a queue of player items comprised of local or progressively downloaded file-based media, such as QuickTime movies or MP3 audio files, as well as media served using HTTP Live Streaming.

So in Swift 3 it will work like this:

lazy var playerQueue : AVQueuePlayer = {    return AVQueuePlayer()}()

...

func playTrackOrPlaylist{    if let url = track.streamURL() { //this is an URL fetch from somewhere. In this if you make sure that URL is valid        let playerItem = AVPlayerItem.init(url: url)        self.playerQueue.insert(playerItem, after: nil)        self.playerQueue.play()    }}


matomo