MPNowPlayingInfoCenter是否与AVAudioPlayer兼容?

7

我开始使用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如何确定是否应显示播放或暂停按钮吗?
3个回答

1

您是否在 AudioSession 上设置了正确的 AVAudioSessionCategory?我认为需要将其设置为 AVAudioSessionCategoryPlayback 才能使其正常工作。


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.
        }
    }

0

我刚刚在自己的应用程序中解决了一个类似的问题。起初,我使用了[[AVAudioSession sharedInstance] setCategory:withOptions:error:]并提供了AVAudioSessionCategoryOptionMixWithOthers和AVAudioSessionCategoryOptionDuckOthers。结果证明这是我的问题所在。如果你设置与其他应用程序混合,你将无法获得远程控制事件。它们仍然会发送到iPod应用程序。如果你设置减小其他应用程序音量,你将获得远程控制事件,但似乎会导致你所描述的问题:播放/暂停按钮显示错误的内容。我不确定原因所在。我通过将选项设置为0,或者实际上只调用setCategory:error:来使播放/暂停按钮正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接