Playing from iPod music library
I want to play particular selected song from my ipod music library.Ho开发者_如何学JAVAw can i do that? My idea is to save the title name from MPMediaQuery and then play later on when app starts so any one can have idea to do this?
Thank You.
The basic solution is to save the unique identifier each song in the library has, aka MPMediaItemPropertyPersistentID
. You can use this ID to play the song, and you can save the ID to memory in order to remember the song the user selected between launches. If you don't know how the Media Player Framework works, look at the AddMusic sample code.
Your view controller must implement the MPMediaPickerControllerDelegate
protocol. Assuming you're just allowing the user to select a single song, then the basic outline of your callback will look something like this.
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
NSArray* items = [mediaItemCollection items];
if ([items count] == 1)
{
MPMediaItem* song = (MPMediaItem *)[items objectAtIndex:0];
NSNumber* persistentId = [song valueForProperty:MPMediaItemPropertyPersistentID];
// ...Save/Play here...
}
}
At this point you can use the persistent ID to play the song, and/or save it to user defaults.
精彩评论