如何阻止iOS 7控制中心控制音乐应用程序? [英] How to block iOS 7 control centre from controlling music app?

查看:246
本文介绍了如何阻止iOS 7控制中心控制音乐应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的应用明确成为远程控制事件的第一响应者,使用远程控制(例如来自iOS7之前的旧跳板,耳塞)明确阻止了用户表单.但是,在iOS7上,相同的代码无法绕过控制中心的音乐控件.

Our app explicitly blocks user form using remote-control, e.g., old springboard from pre-iOS7, earbud, by becoming the first responder to the remote-control events. However, on iOS7, the same code fails to bypass the control centre music controls.

从外部测试来看,控制中心似乎绕过了所有音乐控制事件,包括UIEventSubtypeRemoteControlPause和UIEventSubtypeRemoteControlPlay以及UIEventSubtypeRemoteControlTogglePlayPause.

From out tests, control centre seems to have bypassed ALL music control events including UIEventSubtypeRemoteControlPause and UIEventSubtypeRemoteControlPlay, and UIEventSubtypeRemoteControlTogglePlayPause.

是iOS 7中控制中心具有自己的远程控制协议还是拦截远程控制事件的方式发生了改变?

Is it that control centre has its own protocol for remote control or that the way to intercept remote-control events has changed in iOS7?

相同的阻止代码仍可完美地与iOS6设备配合使用.这是我们的工作:

The same blocking code still works perfectly with iOS6 devices. Here is what we do:

  1. 在我们的appDelegate中添加了一个方法:

  1. Added a method in our appDelegate:

(BOOL)canBecomeFirstResponder { 返回是; }

(BOOL)canBecomeFirstResponder { return YES; }

在applicationDidBecomeActive中调用它:

Call this in applicationDidBecomeActive:

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

//将自己设置为第一响应者 [自己成为FirstResponder];

// Set itself as the first responder [self becomeFirstResponder];

在applicationWillResignActive中调用它

Call this in applicationWillResignActive

//关闭远程控制事件传递 [[UIApplication sharedApplication] endReceivingRemoteControlEvents];

// Turn off remote control event delivery [[UIApplication sharedApplication] endReceivingRemoteControlEvents];

//辞去第一响应者的角色 [self resignFirstResponder];

// Resign as first responder [self resignFirstResponder];

最后添加

(void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {

(void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {

    if (receivedEvent.type == UIEventTypeRemoteControl) {
        
        switch (receivedEvent.subtype) {
                
            case UIEventSubtypeRemoteControlTogglePlayPause:
                NSLog(@"Received: UIEventSubtypeRemoteControlTogglePlayPause\n");
                break;
                
            case UIEventSubtypeRemoteControlPreviousTrack:
                NSLog(@"Received: UIEventSubtypeRemoteControlPreviousTrack\n");
                break;
                
            case UIEventSubtypeRemoteControlNextTrack:
                NSLog(@"Received: UIEventSubtypeRemoteControlNextTrack\n");
                break;
                
            case UIEventSubtypeRemoteControlPlay:
                NSLog(@"Received: UIEventSubtypeRemoteControlPlay\n");
                break;

            case UIEventSubtypeRemoteControlPause:
                NSLog(@"Received: UIEventSubtypeRemoteControlPause\n");
                break;

            case UIEventSubtypeRemoteControlStop:
                NSLog(@"Received: UIEventSubtypeRemoteControlStop\n");
                break;
                
            default:
                NSLog(@"Received: Some remove control events\n");
                break;
        }
    }
}

任何指针都会受到赞赏.

Any pointer will be appreciated.

推荐答案

我认为至少在CoreAudio级别上我对发生的事情有更好的了解.

I think I have a better idea of what happened, at least at the CoreAudio level.

当应用的音频会话类别为单独环境时,音乐应用的播放事件会触发音频会话中断,类似于闹钟或电话.这 将以"enter-interruption"状态触发应用程序的音频会话中断侦听器回调.

When the app's audio session category is solo-ambient, the music app's play event triggers an audio session interruption similar to an alarm clock or a phone call. This will trigger app's audio session interruption listener callback with the "enter-interruption" state.

但是,音乐应用程序的暂停事件不会像人们期望的那样以退出中断"状态触发侦听器回调.这个丢失的退出电话有效地冻结了我们应用的音频会话.退出控制中心也不会触发它.除了可以使用我上一封电子邮件中提到的firstResponder技巧来阻止物理遥控器之外,物理遥控器也可以使用相同的方法.它不适用于控制中心.

However, the music app's pause event does not trigger the listener callback with the "exit-interruption" state, as one would expect. This missing exit call effectively freezes our app's audio session. Quitting the control centre does not trigger it either. Same thing applies to a physical remote-control, except that the physical remote-control can be blocked using the firstResponder trick said in my last email. It does not work with Control Centre.

除非我缺少明显的东西,否则我深信在命令链中CoreAudio或其他框架中存在两个错误.

Unless I'm missing something obvious, I am more convinced that there are two bugs in either CoreAudio or other frameworks in the chain of command.

错误1:如果首先在那儿打入电话,则无法从音乐遥控器发出音频会话中断收听者的退出电话.

Bug 1: Audio session interruption listener's exit call cannot be made from music remote control if the entrance call is made first there.

错误2:控制中心的音乐遥控器与遥控事件机制不符.

Bug 2: Control Centre's music remote control does not conform to remote-control event mechanism.

我很惊讶没有人报告这一点.

I'm just surprised that no one ever reported this.

我认为除非有人提出不同的建议,否则我将提交错误报告.

I think I'm going to file a bug report unless someone suggests differently.

更新 错误2是错误的警报.在通过iOS7 SDK彻底重建所有内容几次之后,我们发现问题消失了.错误1仍然存在.

UPDATE Bug 2 was a false alarm. After clean rebuilding everything over iOS7 SDK for a couple times, we found that the problem went away. Bug 1 still holds.

这篇关于如何阻止iOS 7控制中心控制音乐应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆