Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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];

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.

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.

- (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];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top