ios Music player [closed]
Hi I am working on an Alarm Clock app.. I am using MPMediaPickerController class,using it I was able to pick and the mp3 file but not able to get the path of the file to play when UINotification gets called. Can anyone suggest something..??
Thanks
You're not given the path, you're given a URL, in such a form as AVFoundation should understand it. I think part of the reason you never get a path is to protect DRM content. Anyway, your delegate should drill the returned MPMediaItemCollection down to the MPMediaItem you want, you can then use valueForProperty
to get the suitable URL.
Example code to play whatever is selected in an MPMediaPickerController immediately (typed for the first time here, please comment if I've made any errors):
// delegate method, to receive the result of the MPMediaPickerController
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker
didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
// use the first item in the collection
MPMediaItem *item = [[mediaItemCollection items] objectAtIndex:0];
// get the URL to the MP3
NSURL *URL = [item valueForProperty:MPMediaItemPropertyAssetURL];
// URL uses a proprietary scheme, but AVFoundation understands it, so...
AVAudioPlayer *player = [[AVAudioPlayer alloc]
initWithContentsOfURL:URL error:NULL];
[player play];
/* NB: player will leak. Do something more sensible in your code */
}
精彩评论