Tutorial :How to play the iPod Library file in the AVPlayer



Question:

How can I get an ipod library music file into AVAudioPlayer?


Solution:1

The SDK has no provision for reading files from the iPod library (as you'd need to do to use AVAudioPlayer with it), probably for anti-piracy reasons. To play iPod library items, use the MPMusicPlayerController class.

Edit: This is no longer accurate. See the below answers which describe the use of the AVPlayer class.


Solution:2

As David mentions there is more work to do than this, for example you have to manage playing the next track in a collection of media items, but here is one way to do it with a set of MPMediaItems that a user selected from the iPod Picker. The AssetURL is what you use, it gives you a path to the MP3 file (e.g. ipod-library://item/item.mp3?id=-6889145242935454020)

NSURL *anUrl = [[mediaItems objectAtIndex: 0] valueForProperty:MPMediaItemPropertyAssetURL];  self.audioPlayerMusic = [[[AVPlayer alloc] initWithURL:anUrl] retain];                        [self.audioPlayerMusic play];  


Solution:3

Yes, you can play songs from the iPod library using the SDK without resorting to the MPMusicPlayerController class.

The more basic AVPlayer class can handle audio files from the iPod library by using the NSUrl value from the song's MPMediaItemPropertyAssetURL property. You have to do a lot more work to get everything setup properly, but it can be done.


Solution:4

Is there any possibility to get information about dB-metering in MPMusicPlayerController? Perhaps initiating an AVAudioSession for Recording in parallel would do the job?? I need dB-values to build some kind of volume-spectrograph.


Solution:5

- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {      NSURL *url = [[mediaItemCollection.items objectAtIndex:0] valueForProperty:MPMediaItemPropertyAssetURL];        NSError *error;      self.player = [[AVAudioPlayer alloc] url error:&error];        if (!error) {          [self.player prepareToPlay];          [self.player play];      }        [mediaPicker dismissModalViewControllerAnimated:YES];  }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »