如何以编程方式感知iPhone静音开关?
我似乎无法在 SDK 中找到如何以编程方式感知 iPhone 上的静音按钮/开关。 当我的应用程序播放背景音乐时,它会正确响应音量按钮,而我没有任何代码可以遵循,但是当我使用静音开关时,它只是继续播放。
如何测试静音位置?
(注意:我的程序有自己的静音开关,但我希望物理开关可以覆盖它。)
I can't seem to find in the SDK how to programatically sense the mute button/switch on the iPhone. When my app plays background music, it responds properly to the volume button without me having any code to follow that but, when I use the mute switch, it just keeps playing away.
How do I test the position of mute?
(NOTE: My program has its own mute switch, but I'd like the physical switch to override that.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
谢谢,摩根大通。 事实上,您提供的链接最终会导致正确的答案。;)为了完整性(因为SO应该是快速答案的来源!)...
Thanks, JPM. Indeed, the link you provide leads to the correct answer (eventually. ;) For completeness (because S.O. should be a source of QUICK answers! )...
我回答了类似的问题此处(链接)。 相关代码:
I answered a similar question here (link). The relevant code:
如果您不处于尊重静音开关的环境模式,其他答案中的某些代码(包括接受的答案)可能无法工作。
我编写了下面的例程来切换到环境,读取开关,然后返回到我的应用程序中所需的设置。
Some of the code in other answers (including the accepted answer) may not work if you aren't in the ambient mode, where the mute switch is respected.
I wrote the routine below to switch to ambient, read the switch, and then return to the settings I need in my app.
为了找出静音开关和音量控制的状态,我编写了这两个函数。 如果您希望在用户尝试创建音频输出之前警告用户,这些是理想的选择。
To find out the state of the mute switch and the volume control I wrote these two functions. These are ideal if you wish to warn the user before they try creating audio output.
我遵循这里的一般理论并让它发挥作用
http://inforceapps.wordpress.com /2009/07/08/detect-mute-switch-state-on-iphone/
这里是一个回顾:播放短促的无声声音。 计时需要玩多长时间。 如果静音开关打开,声音的播放将比声音本身短得多。 我使用了 500ms 的声音,如果声音播放时间小于这个时间,则静音开关打开。 我使用音频服务来播放无声声音(它始终遵循静音开关)。 这篇文章说可以使用AVAudioPlayer来播放这个声音。 如果您使用 AVAudioPlayer,我假设您需要设置 AVAudioSession 的类别以支持静音开关,但我还没有尝试过。
I followed the general theory here and got this to work
http://inforceapps.wordpress.com/2009/07/08/detect-mute-switch-state-on-iphone/
Here is a recap: Play a short silent sound. Time how long it takes to play. If the mute switch is on, the playing of the sound will come back much shorter than the sound itself. I used a 500ms sound and if the sound played in less than this time, then the mute switch was on. I use Audio Services to play the silent sound (which always honors the mute switch). This article says that you can use AVAudioPlayer to play this sound. If you use AVAudioPlayer, I assume you'll need to setup your AVAudioSession's category to honor the mute switch, but I have not tried it`.
使用 Ambient 模式播放视频,使用 PlayAndRecord 模式在相机屏幕上录制视频,解决了我们案例中的问题。
application 中的代码:didFinishLaunchingWithOptions:
cameraController 上 viewWillAppear 中的代码(如果您必须在应用程序中使用相机或录制)
cameraController 上 viewWillDisappear 中的代码
使用这些行,我们的应用程序记录并播放视频和静音开关在 iOS8 和IOS 7!!!
Using Ambient mode for playing a video and PlayAndRecord mode for recording a video on camera screen, resolves the issue in our case.
The code in application:didFinishLaunchingWithOptions:
The code in viewWillAppear on cameraController, if you have to use camera or recording in your app
The code in viewWillDisappear on cameraController
Using these lines our Application records and plays a video and mute switch works perfectly under both iOS8 and iOS7!!!
下面的框架在设备中完美运行
https://github.com/akramhussein/Mute
只需使用 pod 安装或从 Git 下载
并使用如下代码
Below framework works perfectly in device
https://github.com/akramhussein/Mute
Just install using pod or download from Git
and use like below code