Search code examples
iphoneiphone-sdk-3.0mpmediaitemcollection

How do I clear the queue of a MPMusicPlayerController?


I'm trying to replicate the clear queue functionality of the iPod application, however 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


Solution

  • 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.