MPRemoteCommandCenter暂停/播放按钮没有切换?

时间:2022-08-23 22:53:52

I'm having issues getting the play and pause buttons toggle in the MPRemoteCommandCenter. For whatever reason the audio and events will all work correctly, but the command center doesn't change the play button to the pause button. Here's my code...

我在MPRemoteCommandCenter中获取播放和暂停按钮切换时遇到问题。无论出于何种原因,音频和事件都将正常工作,但命令中心不会将播放按钮更改为暂停按钮。这是我的代码......

- (void)setupMPRemoteCommandCenter{
    MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];

    MPRemoteCommand *play = [commandCenter playCommand];
    [play setEnabled:YES];
    [play addTarget:self action:@selector(playAudio:)];

    MPRemoteCommand *pause = [commandCenter pauseCommand];
    [pause setEnabled:YES];
    [pause addTarget:self action:@selector(playAudio:)];


    [commandCenter.skipBackwardCommand setPreferredIntervals:@[@30.0]];
    MPRemoteCommand *skipBackwards = [commandCenter skipBackwardCommand];
    [skipBackwards setEnabled:YES];
    [skipBackwards addTarget:self action:@selector(skipBackwardEvent:)];

    [commandCenter.skipForwardCommand setPreferredIntervals:@[@30.0]];
    MPRemoteCommand *skipForwards = [commandCenter skipForwardCommand];
    [skipForwards setEnabled:YES];
    [skipForwards addTarget:self action:@selector(skipForwardEvent:)];

}
-(void)playAudio: (MPRemoteCommandHandlerStatus *)event{
    [self playAction];
    //playAction handles the audio pausing and toggling the play button on the app
}

MPRemoteCommandCenter暂停/播放按钮没有切换?

Let me know if you guys can think of anything, I'd love the help. This has been driving me nuts

让我知道,如果你们能想到任何事情,我会很乐意帮助。这让我疯了

1 个解决方案

#1


10  

a few pointers on how I solved this. reading the apple documentation it states "Your app must be the “Now Playing” app. An app does not receive remote control events until it begins playing audio"

关于我如何解决这个问题的一些指示。阅读苹果文档,它说“您的应用程序必须是”正在播放“应用程序。应用程序在开始播放音频之前不会接收远程控制事件”

so first start playing the audio.

所以先开始播放音频。

MPRemoteCommandCenter is a fairly self reliant module. setEnabled is used to explicitly say something is not going to be supported. Do not use it as a toggle during an event, AVFoundation will handle that itself.

MPRemoteCommandCenter是一个相当自我依赖的模块。 setEnabled用于明确表示不支持某些内容。在事件期间不要将它用作切换,AVFoundation将自己处理。

Also note that i had issues toggling in the simulator, it toggles fine on device but not in the simulator, which took a quick 16 hours to figure out :)

另请注意,我在模拟器中切换问题,它在设备上切换很好但在模拟器中没有切换,这需要花费16个小时才能搞清楚:)

#1


10  

a few pointers on how I solved this. reading the apple documentation it states "Your app must be the “Now Playing” app. An app does not receive remote control events until it begins playing audio"

关于我如何解决这个问题的一些指示。阅读苹果文档,它说“您的应用程序必须是”正在播放“应用程序。应用程序在开始播放音频之前不会接收远程控制事件”

so first start playing the audio.

所以先开始播放音频。

MPRemoteCommandCenter is a fairly self reliant module. setEnabled is used to explicitly say something is not going to be supported. Do not use it as a toggle during an event, AVFoundation will handle that itself.

MPRemoteCommandCenter是一个相当自我依赖的模块。 setEnabled用于明确表示不支持某些内容。在事件期间不要将它用作切换,AVFoundation将自己处理。

Also note that i had issues toggling in the simulator, it toggles fine on device but not in the simulator, which took a quick 16 hours to figure out :)

另请注意,我在模拟器中切换问题,它在设备上切换很好但在模拟器中没有切换,这需要花费16个小时才能搞清楚:)