我正在构建一个使用 AVQueuePlayer 流式传输音频的应用程序。我将我的 AVAudioSession 设置为 AVAudioSessionCategoryPlayback 类别,并且我能够从 remoteControlReceivedWithEvent: 接收事件。但是,当我暂停音频会话时,状态栏中的图标不会消失,App Switcher 中的暂停/播放按钮也不会变为播放。音频暂停,但图标永远不会改变。你知道为什么会发生这种情况吗?
我还使用 MPMusicPlayerController 从 iPod 音乐播放器播放音乐。两个播放器总是被初始化,但是一次只有一个播放器。当应用程序打开时,如果我先从 MPMusicPlayerController 播放某些内容,则 AVQueuePlayer 不再有此暂停冲突。仅当您首先从 AVQueuePlayer 播放时才会出现此问题。我包含了代码来向您展示我在做什么。
仅供参考-我的 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
}
有任何想法吗?