如何清除MPMusicPlayerController的队列并在Song根据需求

时间:2015-05-11 06:07:47

标签: ios objective-c xcode mpmusicplayercontroller

每首歌完成后,我正在清除队列,然后添加一个新队列。但是,多次调用通知(播放/暂停/停止/播放/暂停/播放/暂停/暂停)。它最终会停止应用程序中的音频并开始从iTunes播放。

任何人都可以帮我吗?

-(void)clearqueueAndAddNewQueue
{
    NSMutableArray *allTheSongs = [[NSMutableArray alloc] initWithCapacity:0];

    NSLog(@"setupMusic:");
    for (SongsList *obj_songsList in arrSongs) {

        //  NSLog(@"mediaTitle: %@",obj_songsList.mediaTitle);
        MPMediaQuery *songQuery = [MPMediaQuery songsQuery];
        [songQuery addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:obj_songsList.persistentId forProperty:MPMediaItemPropertyPersistentID]];
        NSArray *songs = [songQuery items];
        [allTheSongs addObjectsFromArray: songs];
    }

    //Rearrange Array in Random Objects
    NSUInteger count = [allTheSongs count];
    for (NSUInteger i = 0; i < count; ++i) {
        // Select a random element between i and end of array to swap with.
        NSInteger nElements = count - i;
        NSInteger n = (arc4random() % nElements) + i;
        [allTheSongs exchangeObjectAtIndex:i withObjectAtIndex:n];
    }

    //Modify Queue
    if ([[AppDelegate appDel].musicPlayerCtrl playbackState] == MPMusicPlaybackStatePlaying) {

        MPMusicPlayerController *musicplayercontroller = [MPMusicPlayerController iPodMusicPlayer];
        MPMediaItemCollection *currentQueue = [[MPMediaItemCollection alloc] initWithItems:allTheSongs];
        MPMediaItem *nowPlaying = [[currentQueue items] objectAtIndex:0];
        [musicplayercontroller setQueueWithItemCollection:currentQueue];
        [musicplayercontroller setNowPlayingItem:nowPlaying];
        [AppDelegate appDel].musicPlayerCtrl = musicplayercontroller;
    }

    //I have also used this instead of above mentioed Modify Queue
    if ([[AppDelegate appDel].musicPlayerCtrl playbackState] == MPMusicPlaybackStatePlaying) {
        MPMediaPropertyPredicate *predicate =
        [MPMediaPropertyPredicate predicateWithValue: @"Non_Existant_Song_Name"
                                         forProperty: MPMediaItemPropertyTitle];
        MPMediaQuery *q = [[MPMediaQuery alloc] init];
        [q addFilterPredicate: predicate];
        [[AppDelegate appDel].musicPlayerCtrl setQueueWithQuery:q];
        [AppDelegate appDel].musicPlayerCtrl.nowPlayingItem = nil;
        [[AppDelegate appDel].musicPlayerCtrl stop];



        MPMediaItemCollection *currentQueue = [[MPMediaItemCollection alloc] initWithItems:allTheSongs];
        [[AppDelegate appDel].musicPlayerCtrl setQueueWithItemCollection:currentQueue];
        [[AppDelegate appDel].musicPlayerCtrl play];
    }
}

1 个答案:

答案 0 :(得分:0)

使用此方法有助于清除队列并创建或添加新队列而不会出现任何问题。

- (void)setupMusic:(NSArray *)arrSongs forTaggedValue:(NSString *)taggedValue {

    NSMutableArray *allTheSongs = [[NSMutableArray alloc] initWithCapacity:0];
    NSLog(@"setupMusic:");
    for (SongsList *obj_songsList in arrSongs) {

        //  NSLog(@"mediaTitle: %@",obj_songsList.mediaTitle);
        MPMediaQuery *songQuery = [MPMediaQuery songsQuery];
        [songQuery addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:obj_songsList.persistentId forProperty:MPMediaItemPropertyPersistentID]];
        NSArray *songs = [songQuery items];
        [allTheSongs addObjectsFromArray: songs];
    }

    //Modify Queue
    if ([[AppDelegate appDel].musicPlayerCtrl playbackState] == MPMusicPlaybackStatePlaying) {

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

        MPMediaItemCollection *currentQueue = [[MPMediaItemCollection alloc] initWithItems:allTheSongs];
        [[AppDelegate appDel].musicPlayerCtrl setQueueWithItemCollection:currentQueue];
        [[AppDelegate appDel].musicPlayerCtrl setRepeatMode:MPMusicRepeatModeAll];
        [[AppDelegate appDel].musicPlayerCtrl play];

        [self addNotificationCenterForMusicPlayerController];
        [[AppDelegate appDel].musicPlayerCtrl beginGeneratingPlaybackNotifications];
    }
}

并且还使用此代码,这有助于您的[AppDelegate appDel] .musicPlayerCtrl(MPMusicPlayerContoller&#39; s对象)工作,并阻止转到Itunes。

#pragma mark - For Hadle the Active Bugs Of MPMusicPlayerController
- (BOOL)isPlaybackStateBugActive {
    MPMusicPlaybackState playbackState = [AppDelegate appDel].musicPlayerCtrl.playbackState;
    if (playbackState == MPMusicPlaybackStatePlaying) {
        AudioSessionInitialize (NULL, NULL, NULL, NULL);
        UInt32 sessionCategory = kAudioSessionCategory_AmbientSound;
        AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (sessionCategory), &sessionCategory);
        AudioSessionSetActive (true);

        UInt32 audioIsPlaying;
        UInt32 size = sizeof(audioIsPlaying);
        AudioSessionGetProperty(kAudioSessionProperty_OtherAudioIsPlaying, &size, &audioIsPlaying);

        if (!audioIsPlaying){
            NSLog(@"PlaybackState bug is active");
            [[AppDelegate appDel].musicPlayerCtrl pause];
            [btnPlayOrPause setImage:[UIImage imageNamed:KPlayImage] forState:UIControlStateNormal];
            return YES;
        }
    }

    return NO;
}

addNotificationCenterForMusicPlayerController方法

#pragma mark - Default NotificationCenter For iPodLibrary/MusicPlayerController
- (void)addNotificationCenterForMusicPlayerController {

    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

    [notificationCenter addObserver: self selector: @selector (handle_NowPlayingItemChanged:) name: MPMusicPlayerControllerNowPlayingItemDidChangeNotification object: [AppDelegate appDel].musicPlayerCtrl];

    [notificationCenter addObserver: self selector: @selector (handle_PlaybackStateChanged:) name: MPMusicPlayerControllerPlaybackStateDidChangeNotification object: [AppDelegate appDel].musicPlayerCtrl];



   // [notificationCenter addObserver: self selector: @selector (handle_VolumeChanged:) name: MPMusicPlayerControllerVolumeDidChangeNotification object: [AppDelegate appDel].musicPlayerCtrl];

   // [[AppDelegate appDel].musicPlayerCtrl beginGeneratingPlaybackNotifications];
}

removeNotificationCenterForMusicPlayerController方法

- (void)removeNotificationCenterForMusicPlayerController {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification object: [AppDelegate appDel].musicPlayerCtrl];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMusicPlayerControllerPlaybackStateDidChangeNotification object: [AppDelegate appDel].musicPlayerCtrl];

   // [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMusicPlayerControllerVolumeDidChangeNotification object: [AppDelegate appDel].musicPlayerCtrl];

    [[AppDelegate appDel].musicPlayerCtrl endGeneratingPlaybackNotifications];
}