接收没有音频的遥控事件 [英] Receive remote control events without audio

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

问题描述

以下是一些背景信息,否则以粗体显示问题。我正在构建一个应用程序,我希望它能够访问远程控制/锁屏事件。棘手的部分是这个应用程序本身不播放音频,它控制附近另一台设备的音频。当应用程序位于前台时,设备之间的通信不是问题。正如我刚刚发现的那样,应用程序不会控制遥控器,直到它通过播放音频会话播放音频,并且最后这样做。这提出了一个问题,因为正如我所说,该应用程序控制另一个设备的音频,而不需要自己播放。

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.

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

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会批准播放静音音频剪辑的应用程序,以便控制远程/锁定屏幕控制以控制其他设备的音频吗?有没有办法在没有音频会话的情况下控制遥控器?

P.S。我更喜欢在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 ,因为你的应用程序类是<$ c的子类$ c> 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。如果您发现它失败了,请将您自己的网址放在那里并根据您的内容进行播放。

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.

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

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