1
votes

I am accessing the songs using MPMediaItem from iPod Library and saving the assetURL in my coredata then I am using the following code to play the sound but it doesn't plays the sound.

  var url = NSURL.fileURLWithPath(file as String)
    print("FILE :   \(file)  \n")
    var error: NSError?
    var itemPlayer:AVPlayer?
   // itemPlayer = AVPlayer(URL: NSURL(fileURLWithPath: file as String))
    itemPlayer = AVPlayer(playerItem: AVPlayerItem(URL: NSURL(fileURLWithPath: file as String)))
    itemPlayer!.play()

and it just print the following but player is not playing the song

FILE :   ipod-library://item/item.mp3?id=4287130341014298234  

And also if i use AVAudioPlayer to initialize player using above assetURL then it return me Nil. and i think this is because the above url is not local it should be like file://.... . So what should i do to play sound using assetURL of song from iPod Library

1

1 Answers

0
votes

If you are trying to play a music file in your iPod library and already using MPMediaItem you should try using MPMusicPlayerController.

Alternatively, if you wanted to continue with way you were doing, you could do this (this is assuming that your file variable is the MPMediaItem):

 var fileUrl = file.assestURL
 var myPlayerItem = AVPlayerItem(URL: fileURL)
 itemPlayer = AVPlayer(playerItem: myPlayerItem)