OGeek|极客世界-中国程序员成长平台

标题: ios - 在后台调用 beginReceivingRemoteControlEvents [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 14:18
标题: ios - 在后台调用 beginReceivingRemoteControlEvents

是否可以在后台调用 beginReceivingRemoteControlEvents?有没有人遇到过类似情况?

到目前为止,我已经得出结论,我无法更改类别并在后台继续使用 Remote 。

当我在类别之间切换时,例如 AVAudioSessionCategoryPlayback 或 AVAudioSessionCategoryPlayAndRecord, Audio Session 被停用,我必须再次调用 beginReceivingRemoteControlEvents。当这在前台完成时,它可以完美地工作。在后台完成后,新的 beginReceivingRemoteControlEvents 似乎不起作用。

非常感谢任何有关我如何实现此类目标的帮助。



Best Answer-推荐答案


您可能希望使用较新的 MPRemoteCommandCenter,而不是使用 beginReceivingRemoteControlEvents。例如:

    MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];

    [commandCenter.togglePlayPauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        NSLog(@"toggle button pressed");
        return MPRemoteCommandHandlerStatusSuccess;
    }];

或者,如果您更喜欢使用方法而不是 block :

    [commandCenter.togglePlayPauseCommand addTarget:self actionselector(toggleButtonAction)];

停止:

    [commandCenter.togglePlayPauseCommand removeTarget:self];

或:

    [commandCenter.togglePlayPauseCommand removeTarget:self actionselector(toggleButtonAction)];

您需要将其添加到文件的包含区域:

@import MediaPlayer;

关于ios - 在后台调用 beginReceivingRemoteControlEvents,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28439169/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4