Is it possible to pick media items using MPMediaPickerController and then load them into an AVAudioPlayer object?
4 Answers
If MPMusicPlayerController
doesn't meet your needs, you can copy the audio to your local bundle so you can use AVAudioPlayer
.
EDIT
You basically have three options for playing audio from the user's iPod library: MPMediaPlayer
, AVPlayer
and AVAudioPlayer
.
Here are examples for MPMediaPlayer
and AVPlayer
:
- (void) mediaPicker: (MPMediaPickerController *) mediaPicker
didPickMediaItems: (MPMediaItemCollection *) collection {
MPMediaItem *item = [[collection items] objectAtIndex:0];
NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL];
[self dismissModalViewControllerAnimated: YES];
// Play the item using MPMusicPlayer
MPMusicPlayerController* appMusicPlayer = [MPMusicPlayerController applicationMusicPlayer];
[appMusicPlayer setQueueWithItemCollection:collection];
[appMusicPlayer play];
// Play the item using AVPlayer
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:url];
AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
[player play];
}
If you need to use AVAudioPlayer
for some reason, or you need access to the audio file's actual audio data, you have to first copy the audio file to your app's directory and then work with it there. The AVAsset
+ AVPlayer
stuff is the closest analogy to ALAsset
if you're used to working with photos and videos.

- 129,200
- 40
- 280
- 281

- 8,747
- 1
- 37
- 34
-
So audio assets in the form of AVAssets are used like ALAssets? – some_id Mar 07 '11 at 18:16
-
I am trying to play music at host side, connected joins hear same music which is playing on host side. From host side i try to send media item to join side through connected sessions but media item not sending. Kindly tell me how i send media item from host to join (client) side? By the way i successfully send media title and media artwork. Thanks. – Adeel Ishaq Jun 24 '15 at 02:14
-
Link probably should be updated — either to [archival version](https://web.archive.org/web/20130603064710/http://blog.tapsquare.com/post/803301658/ipod-library-access) or straight to the [bitbucket repo](https://bitbucket.org/artgillespie/tslibraryimport) it refers to. – Jari Keinänen Apr 12 '16 at 13:09
-
2By using this: MPMediaItem *item = [[collection items] objectAtIndex:0]; NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL]; It shows me, URL is null. – Parthpatel1105 Jul 29 '16 at 12:14
Just wanted to say that it appears that in iOS 6 and 7, AVAudioPlayer can play file URLs directly from the iPod without having to copy the audio data into your app directory as Art suggested.
- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) collection {
MPMediaItem *item = [[collection items] objectAtIndex:0];
NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL];
// Play the item using AVPlayer
self.avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[self.avAudioPlayer play];
}

- 6,272
- 4
- 28
- 34
-
-
3Can someone verify that AVAudioPlayer can really play a song from the iPod library? It's become more important now that setVolume is deprecated on the MPMusicPlayerController. – Richard Lovejoy Oct 29 '13 at 18:23
-
2i use AVAudioPlayer to play iPod songs in both iOS 6 and 7. It works. – nvrtd frst Oct 31 '13 at 06:22
-
1Some songs don't work - in particular I've noticed some songs that were purchased >6 years ago don't work for me - I assume the DRM does not allow them to be played outside of the Music app... – Ephemera Nov 30 '14 at 06:56
I wanted to add (and I can't comment on SO yet) that it seems like in iOS 8, and perhaps before, songs that are stored on iCloud do not have a value for the property assetURL
and return null
for [npItem valueForProperty:MPMediaItemPropertyAssetURL]
.
Here is a sample output:
MPMediaItem *npItem = self.musicPlayerController.nowPlayingItem;
NSLog(@"Song Title: %@\n assetURL: %@\n Cloud Item: %d", npItem.title, [npItem valueForProperty:MPMediaItemPropertyAssetURL], npItem.cloudItem)
// Log Output
Song Title: Time We Had
assetURL: (null)
Cloud Item: 1
Song Title: The Quiet
assetURL: ipod-library://item/item.m4a?id=1529654720874100371
Cloud Item: 0

- 73
- 1
- 5
-
Is there a way to distinguish between the "old" DRM protected songs and songs on the iCloud? – Ing. Ron Oct 24 '16 at 11:35
I think you'll find that the [NSBundle mainBundle]
is READ-ONLY. It's not possible to write files into the APP, therefore AVAudioPlayer
will not work for iPod-Library MPMediaItems
!

- 129,200
- 40
- 280
- 281

- 25
- 1