-1

I am getting a link from itunes search api, and trying to pass it into an AVPlayer, but I am getting an error. http://puu.sh/ldBCw/6bfff69c7d.png. Below is the code to get the link.

 let task = NSURLSession.sharedSession().dataTaskWithURL(url) {(data, response, error) -> Void in
        do {
            if let dict: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary
            {


                    if let previewUrl = dict["results"]![0]["previewUrl"]  {
                        print(previewUrl!)
                         player = AVPlayer(URL: previewUrl)


                }

            }
        } catch let jsonError as NSError {

        }
    }
    task.resume()

}
xxtmanxx
  • 209
  • 2
  • 8

2 Answers2

0

Change this:

if let previewUrl = NSURL(string: dict["results"]![0]["previewUrl"])!  {

Also you could read it more here: http://www.techotopia.com/index.php/IOS_8_Video_Playback_using_AVPlayer_and_AVPlayerViewController and here: How to Play Audio in Background Swift?

Community
  • 1
  • 1
m.aibin
  • 3,528
  • 4
  • 28
  • 47
0

Use player.play() or player.pause. I used this in a recent project let playerItem = AVPlayerItem( URL:NSURL( string:"Add your URL here" )! ) player = AVPlayer(playerItem:playerItem) player.rate = 1.0; player.play()

David
  • 77
  • 2
  • 10