Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play MPMediaItem in MPMusicPlayerController in iOS?

I am trying to play MPMedaiItem in MPMusicPlayerController in iOS. In my case , i have a UITableView that show songs from playlist. When i tap cell on UITableView , i want to play that song with MPMusicPlayerController. And i also want to skip next songs from playlist when i tap Next Button. How can i play it?

Here is some of my codes that write in didSelected Method of UITableView. That doesn't play anything.

    MPMediaItemCollection *songs = [self.arrayOfSongs objectAtIndex:indexPath.row ];

    MPMediaItem *item = [songs representativeItem ];

    NSLog(@"%@",[item valueForProperty:MPMediaItemPropertyTitle ]);

    [self.player setNowPlayingItem:[item valueForProperty:MPMediaItemPropertyAssetURL]];

    [self.player play ];
like image 390
Fire Fist Avatar asked Dec 15 '22 15:12

Fire Fist


1 Answers

I know this is a little late, but your problem is that MPMusicPlayerController's nowPlayingItem property expects a MPMediaItem object, and you're passing it an NSString containing the URL of the asset. Here's an example of how this could be accomplished.

MPMusicPlayerController *controller = [MPMusicPlayerController iPodMusicPlayer];

MPMediaItemCollection *collection = [[MPMediaItemCollection alloc] initWithItems:arrayOfMediaItems];
MPMediaItem *item = [collection representativeItem];

[controller setQueueWithItemCollection:collection];
[controller setNowPlayingItem:item];

[controller prepareToPlay];
[controller play];
like image 65
Mick MacCallum Avatar answered Jan 18 '23 22:01

Mick MacCallum