AVQueuePlayer暂停,但播放图标不会消失

时间:2013-04-24 20:39:43

标签: mpmusicplayercontroller avaudiosession avqueueplayer

我正在构建一个使用AVQueuePlayer来传输音频的应用程序。我的AVAudioSession设置了AVAudioSessionCategoryPlayback类,我可以从remoteControlReceivedWithEvent:接收事件。但是,当我暂停音频会话时,状态栏中的图标不会消失,App Switcher中的暂停/播放按钮也不会更改。音频暂停,但图标永远不会改变。你知道为什么会这样吗?

我还使用MPMusicPlayerController播放iPod音乐播放器中的音乐。两个玩家总是被初始化,但是一次只能玩一个玩家。当应用程序打开时,如果我首先从MPMusicPlayerController播放内容,则AVQueuePlayer不再具有此暂停冲突。首次从AVQueuePlayer播放时,它只会出现此问题。我提供了代码来向您展示我正在做的事情。

FYI-我的MPMusicController是self.musicPlayer。我的AVQueuePlayer是self.radioPlayer。暂停后,我的AVQueuePlayer的状态是AVPlayerStatusReadyToPlay。

- (void)viewDidLoad {

    [super viewDidLoad];
    self.musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
    self.musicPlayer.nowPlayingItem = self.currentSong;
    self.musicPlayer.shuffleMode = MPMusicShuffleModeSongs;
    self.musicPlayer.repeatMode = MPMusicRepeatModeAll;

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:nil error:nil];
    [[AVAudioSession sharedInstance] setActive:YES error:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(radioItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:[_radioPlayer currentItem]];
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [self resignFirstResponder];

    if (self.source == LIBRARY)
    {
        [self.parentDelgate reloadInformation];
    }
}

- (void)viewDidAppear:(BOOL)animated
{
    if (self.source == RADIO)
    {
        // Code that sets the URLs for the AVQueuePlayer

        [self initializeMusicPlayer];

        [self prepareRadioPlayer];

    }
    else if ( self.source == LIBRARY ) {
        if ( self.currentSong != self.musicPlayer.nowPlayingItem || self.playlist != nil)
        {
            [self initializeMusicPlayer];
            self.musicPlayer.nowPlayingItem = self.currentSong;
        }
    }
}

- (void)initializeMusicPlayer {
    if ( self.source == LIBRARY )
    {
        [self.musicPlayer setQueueWithItemCollection:[MPMediaItemCollection collectionWithItems:(NSArray *)self.tracks]];

        if ([self.radioPlayer rate] != 0.0)
            [self.radioPlayer pause];

        [self.musicPlayer play];
        self.status = PLAYING;
    }
    else if ( self.source == RADIO )
    {
        if ( self.musicPlayer.playbackState == MPMusicPlaybackStatePlaying )
            [self.musicPlayer pause];
    }
}

- (IBAction)pausePressed:(UIButton *)sender {
    if ( self.source == LIBRARY )
    {
        [self.musicPlayer pause];
    }
    else if (self.source == RADIO)
    {
        [self.radioPlayer pause];
    }

    [self onStatePaused];
}

- (void)onStatePaused {
    // UI Stuff
}

- (void)endSession {
    [self.musicPlayer stop];
    [self.radioPlayer pause];
    [self.radioPlayer removeAllItems];

    // Present different view
}

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我弄明白了这个问题。我在AVQueuePlayer / AVPlayerItems中添加了多个观察者实例,这些实例正在干扰这一点。