接收没有音频的远程控制事件

Receive remote control events without audio(接收没有音频的远程控制事件)

本文介绍了接收没有音频的远程控制事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是一些背景信息,否则请跳至粗体问题.我正在构建一个应用程序,我希望它能够访问远程控制/锁定屏幕事件.棘手的部分是这个应用程序本身不播放音频,它控制附近另一台设备的音频.当应用程序在前台时,设备之间的通信不是问题.正如我刚刚发现的那样,一个应用程序在通过播放音频会话播放音频之前不会控制遥控器,并且是最后一次这样做.这带来了一个问题,因为就像我说的那样,该应用程序控制另一个设备的音频并且不需要播放自己的音频.

Here is some background information, otherwise skip ahead to the question in bold. I am building an app and I would like it to have access to the remote control/lock screen events. The tricky part is that this app does not play audio itself, it controls the audio of another device nearby. The communication between devices is not a problem when the app is in the foreground. As I just found out, an app does not assume control of the remote controls until it has played audio with a playback audio session, and was the last do so. This presents a problem because like I said, the app controls ANOTHER device's audio and has no need to play its own.

我的第一个想法是让应用程序在每次打开时播放一个静音剪辑,以便控制遥控器.我必须这样做的事实让我想知道 Apple 是否会允许我这样做,或者是否有另一种方法可以实现这一点,而不会用假音频剪辑欺骗系统.

My first inclination is to have the app play a silent clip every time it is opened in order to assume control of the remote controls. The fact that I have to do this makes me wonder if I am even going to be allowed to do it by Apple or if there is another way to achieve this without fooling the system with fake audio clips.

问题:Apple 是否会批准播放无声音频剪辑的应用程序,以控制远程/锁定屏幕控件以控制其他设备的音频?有没有什么方法可以在没有音频会话的情况下控制遥控器?

附:我希望在 iOS 4.0 及更高版本上拥有此功能.

P.S. I would prefer to have this functionality on iOS 4.0 and up.

PPS 我见过 这个类似的问题让我集思广益,但所提供的答案并不特定于我需要知道的内容.

P.P.S I have seen this similar question and it has gotten me brainstorming but the answer provided is not specific to what I need to know.

推荐答案


注意:从 iOS 7.1 开始,您应该使用 MPRemoteCommandCenter 而不是下面的答案.


NOTE: As of iOS 7.1, you should be using MPRemoteCommandCenter instead of the answer below.

您创建各种系统提供的 MPRemoteCommand 并将它们分配给 [MPRemoteCommandCenter sharedCommandCenter] 的属性.

You create various system-provided subclasses of MPRemoteCommand and assign them to properties of the [MPRemoteCommandCenter sharedCommandCenter].

我将保留其余部分以供历史参考,但不保证以下内容适用于最新的 iOS 版本.事实上,它可能不会.

I'm keeping the rest of this around for historical reference, but the following is not guaranteed to work on recent iOS versions. In fact, it just might not.

您确实需要一个音频播放器,但不一定需要一个明确的会话来控制远程控制事件.(AVAudioSession 对任何播放音频的应用都是隐含的.)我花了相当多的时间玩这个来确认这一点.

You definitely do need an audio player but not necessarily an explicit session to take control of the remote control events. (AVAudioSession is implicit to any app that plays audio.) I spent a decent amount of time playing with this to confirm this.

我在互联网上看到很多关于在哪里设置 removeControlEventRecievedWithEvent: 方法以及响应者链的各种方法的困惑.我知道这种方法适用于 iOS 6 和 iOS 7.其他方法没有.不要浪费时间在应用程序委托(它们曾经工作的地方)或视图控制器中处理远程控制事件,这可能会在应用程序的生命周期内消失.

I've seen a lot of confusion on the internet about where to set up the removeControlEventRecievedWithEvent: method and various approaches to the responder chain. I know this method works on iOS 6 and iOS 7. Other methods have not. Don't waste your time handling remote control events in the app delegate (where they used to work) or in a view controller which may go away during the lifecycle of your app.

我制作了 一个演示项目展示如何做到这一点.

I made a demo project to show how to do this.

以下是必须发生的事情的简要说明:

Here's a quick rundown of what has to happen:

  1. 你需要创建一个UIApplication的子类.当文档说 UIResponder 时,它表示 UIApplication,因为您的应用程序类是 UIResponder 的子类. 在此子类,您将实现 remoteControlReceivedWithEvent:canBecomeFirstResponder 方法.您想从 canBecomeFirstResponder 返回 YES.在远程控制方法中,您可能希望通知您的音频播放器发生了一些变化.

  1. You need to create a subclass of UIApplication. When the documentation says UIResponder, it means UIApplication, since your application class is a subclass of UIResponder. In this subclass, you're going to implement the remoteControlReceivedWithEvent: and canBecomeFirstResponder methods. You want to return YES from canBecomeFirstResponder. In the remote control method, you'll probably want to notify your audio player that something's changed.

您需要告诉 iOS 使用您的自定义类来运行应用程序,而不是默认的 UIApplication.为此,请打开 main.m 并更改:

You need to tell iOS to use your custom class to run the app, instead of the default UIApplication. To do so, open main.m and change this:

 return UIApplicationMain(argc, argv, nil, NSStringFromClass([RCAppDel`egate class]));

看起来像这样:

return UIApplicationMain(argc, argv, NSStringFromClass([RCApplication class]), NSStringFromClass([RCAppDelegate class]));

在我的例子中,RCApplication 是我的自定义类的名称.请改用您的子类的名称.不要忘记 #import 适当的标题.

In my case RCApplication is the name of my custom class. Use the name of your subclass instead. Don't forget to #import the appropriate header.

可选:您应该配置音频会话.这不是必需的,但如果您不这样做,如果手机静音,音频将不会播放.我在演示应用的委托中执行此操作,但在适当的情况下执行此操作.

OPTIONAL: You should configure an audio session. It's not required, but if you don't, audio won't play if the phone is muted. I do this in the demo app's delegate, but do so where appropriate.

玩点什么.在您这样做之前,遥控器将忽略您的应用程序.我只是拿了一个 AVPlayer 并给了它一个我希望启动的流媒体站点的 URL.如果你发现它失败了,把你自己的 URL 放在那里,尽情地玩吧.

Play something. Until you do, the remote controls will ignore your app. I just took an AVPlayer and gave it the URL of a streaming site that I expect to be up. If you find that it fails, put your own URL in there and play with it to your heart's content.

这个例子有更多的代码来注销远程事件,但这并不是那么复杂.我只是定义并传递一些字符串常量.

This example has a little bit more code in there to log out remote events, but it's not all that complicated. I just define and pass around some string constants.

我敢打赌,无声循环 MP3 文件将有助于实现您的目标.

I bet that a silent looping MP3 file would help work towards your goal.

这篇关于接收没有音频的远程控制事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:接收没有音频的远程控制事件

基础教程推荐