2

是否可以在后台调用 beginReceivingRemoteControlEvents ?有没有人有类似情况的经验?

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

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

任何有关我如何实现此类目标的帮助将不胜感激。

4

1 回答 1

2

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

    MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];

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

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

    [commandCenter.togglePlayPauseCommand addTarget:self action:@selector(toggleButtonAction)];

停止:

    [commandCenter.togglePlayPauseCommand removeTarget:self];

或者:

    [commandCenter.togglePlayPauseCommand removeTarget:self action:@selector(toggleButtonAction)];

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

@import MediaPlayer;
于 2015-09-03T10:56:32.413 回答