How can I get an ipod library music file into AVAudioPlayer?
-
I would recommend looking at http://superuser.com/ also you may want to give more detail. What program are you using exactly, and have you done any research so far? – Guvante Dec 23 '09 at 18:29
-
9AVAudioPlayer is a class used to.. well... play audio on an iPhone. You know, when you're programming for it. The question is most certainly programming related. – aehiilrs Dec 23 '09 at 18:36
5 Answers
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];

- 7,729
- 6
- 45
- 65
-
-
1This will **not work** for **DRM-protected** songs (like the ones from _Apple Music_ subscription) or for the ones which are not available **offline** but in cloud. If you are interested in checking whether music is DRM-protected or in playing DRM-protected music the right way, read my detailed answer here: https://stackoverflow.com/a/47694472/4331787 – Mohit Singh Dec 07 '17 at 14:28
-
Thanks, for the reply. Here you have explained that how to detect drm track. Please tell me how to play it. I am using AVPlayer but it is not playing. I could play using MPMusicPlayer, but my requirement is I needed to play using AVFoundation. – Arpit B Parekh Dec 08 '17 at 13:42
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.

- 186
- 1
- 4
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.

- 57,021
- 16
- 130
- 131
-
2This is not true, see the other answers that document how to access the iPod library for use with AVAudioPlayer. – Bryce Kahle Dec 21 '10 at 21:12
-
This may have been true in Dec '09 but it is possible now as Steve Tranby explains in his answer. – prendio2 Feb 04 '11 at 15:22
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.
-
12 year old question, but I will add the answer for anyone else who lands here. To get the dB level from the audio, use Core-Audio's AudioStreamBasicDescription (https://developer.apple.com/library/mac/documentation/musicaudio/reference/CoreAudioDataTypesRef/Reference/reference.html#//apple_ref/doc/uid/TP40004488-CH3-SW2 ) with the audio data as a stream. I am using the URL from the ipod library for the asset and am using the data at the URL with core-audio. – some_id Jun 27 '12 at 21:40
-
-
1@Helium3: Is your solution working with IOS10 and DRM protected files, too? – Ing. Ron Oct 18 '16 at 09:57
- (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];
}