iOS多媒体 - 音乐播放的坑

使用场景:
tabelView多个cell携带音乐播放.包含音乐库音乐/Bundle中的音乐播放.使用到了MPMusicPlayerControllerAVAudioPlayer类.

抛出问题

以下代码会把系统音乐播放器给停掉

if (self.audioPlayer.isPlaying) {
   [self.audioPlayer stop];
}

问题代码

if (self.audioPlayer.isPlaying) {
        [self.audioPlayer stop];
    }
    // 停止喇叭闪动
    [self.alarmTimer invalidate];
    self.alarmTimer = nil;
    self.menu.isAlarming = NO;  
    [self restoreAlarmBtuttonState];
    MPMusicPlayerController *musicVc = [MPMusicPlayerController systemMusicPlayer];
    if (musicVc.playbackState == MPMusicPlaybackStatePlaying) {
        // 其他cell还在用系统播放器,不能停止
        if ([self.superVc judgeCellIsAlarmSystemMusic]) {
            return ;
        }
        [musicVc stop];
    }

解决问题

   // 停止喇叭闪动
    [self.alarmTimer invalidate];
    self.alarmTimer = nil;
    self.menu.isAlarming = NO;
    
    [self restoreAlarmBtuttonState];
    
    MPMusicPlayerController *musicVc = [MPMusicPlayerController systemMusicPlayer];
    if (musicVc.playbackState == MPMusicPlaybackStatePlaying) {
        // 其他cell还在用系统播放器,不能停止
        if ([self.superVc judgeCellIsAlarmSystemMusic]) {
            return ;
        }
        
        [musicVc stop];
    } else {
        if (self.audioPlayer.isPlaying) {
            [self.audioPlayer stop];
        }
    }

你可能感兴趣的:(iOS多媒体 - 音乐播放的坑)