开发者

How do I clear the queue of a MPMusicPlayerController?

I'm trying to replicate the clear queue functionality of the iPod application, how开发者_StackOverflowever I can't create an empty MPMediaItemCollection with which to call setQueueWithItemCollection:

e.g.

[self.musicPlayer setQueueWithItemCollection:[MPMediaItemCollection collectionWithItems:[NSArray array]]];

where musicPlayer is a MPMusicPlayerController.

throws an exception:

*** Terminating app due to uncaught exception 'MPMediaItemCollectionInitException', reason: 'items array must not be empty'

Is there a way to clear a MPMusicPLayerController queue that avoids this problem?

Any help is greatly appreciated, CV


I don't know if you've managed to overcome your problem, but here's a workaround that seems to be working for me.

MPMediaPropertyPredicate *predicate =
    [MPMediaPropertyPredicate predicateWithValue: @"Non_Existant_Song_Name"
                                     forProperty: MPMediaItemPropertyTitle];
MPMediaQuery *q = [[MPMediaQuery alloc] init];
[q addFilterPredicate: predicate];
[self.player.controller setQueueWithQuery:q];
self.player.controller.nowPlayingItem = nil;
[self.player.controller stop];

This basically sets the play queue up with a query that you're sure will never turn up any songs. Ideally a query that's really quick to perform. And then it nullifies the nowPlayingItem and then for good measure tells the player to stop.

Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜