我用AVAudioPlayer
开始播放,然后像这样设置nowPlaying字典:
NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];
MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage: [UIImage imagedNamed:@"AlbumArt"]];
[songInfo setObject:@"Audio Title" forKey:MPMediaItemPropertyTitle];
[songInfo setObject:@"Audio Author" forKey:MPMediaItemPropertyArtist];
[songInfo setObject:@"Audio Album" forKey:MPMediaItemPropertyAlbumTitle];
[songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];
锁定屏幕始终显示暂停按钮。我正确接收了遥控器事件,我可以通过遥控器事件切换播放/暂停,但锁定屏幕即使正在播放也会一直显示“暂停”。
现在我看到了MPMoviePlayerController的这项工作。有人可以解释MPNowPlayingInfoCenter如何确定它是应该显示播放还是暂停按钮?
答案 0 :(得分:1)
您是否在AVAudioSessionCategory
上设置了正确的AudioSession
?它需要AVAudioSessionCategoryPlayback
我相信它能够发挥作用。
答案 1 :(得分:0)
我目前没有使用MPNowPlaying
,但显然我必须这样做,以便在锁定屏幕上显示音频信息。
但是,除了@ user3061915所说的,为了管理play
/ pause
按钮,我使用了UIEventTypeRemoteControl
,它非常适合控制play
/ pause
按钮:
- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
//if it is a remote control event handle it correctly
if (event.type == UIEventTypeRemoteControl)
{
if (event.subtype == UIEventSubtypeRemoteControlPlay)
{
[self playAudio];
}
else if (event.subtype == UIEventSubtypeRemoteControlPause)
{
[self pauseAudio];
}
else if (event.subtype == UIEventSubtypeRemoteControlTogglePlayPause)
{
[self togglePlayPause]; //This method will handle the toggling.
}
}
答案 2 :(得分:0)
我刚在自己的应用中解决了这样的问题。我最初使用[[AVAudioSession sharedInstance] setCategory:withOptions:error:]并提供了AVAudioSessionCategoryOptionMixWithOthers和AVAudioSessionCategoryOptionDuckOthers。结果证明这是我的问题。如果您将mix与其他设置混合,则不会获得任何远程控制事件。他们仍然去iPod应用程序。如果你设置其他人,你会得到远程控制事件,但它似乎会导致你描述的问题:播放/暂停按钮显示错误的事情。我不知道为什么。我通过将选项设置为0来实现播放/暂停按钮,或实际上只是调用setCategory:错误:。