Mute/Silence an iOS device programmatically?(以编程方式使 iOS 设备静音/静音?)
问题描述
我正在尝试从我的应用程序中将设备的铃声静音,但由于某种原因,使用 AVSystemController 就像在这个答案中一样( 如何禁用 iOS 系统声音 ) 不会让我将设备完全静音.. 它会将音量降至 1 bar,但并非完全静音.p>
我知道它可以做到,可能使用像 AVSystemController 这样的私有 API,并且我知道如果用户期望应用程序具有这种功能,Apple 仍然会批准该应用程序(因为我已经找到了 2 个应用程序在 App Store 中,它以编程方式使设备静音,无需越狱或类似的东西).
这些应用实际上做得更好——它们实际上切换了实际静音,而不仅仅是将音量降低到零.
有人知道这是怎么做的吗?
任何帮助将不胜感激!谢谢!
这对我有用,我有一个按钮可以在游戏中打开和关闭声音.当我想要声音时,我将浮动设置为 10,当我想要关闭声音时,我将浮动设置为 0.
浮点值 = 0.0f;AudioSessionSetProperty(kAudioSessionProperty_CurrentHardwareOutputVolume, sizeof(float), &value);
更新:
另一个仍然适用于 iOS 9.1 的选项
float vol = [[AVAudioSession sharedInstance] outputVolume];NSLog(@"输出音量:%1.2f dB", 20.f*log10f(vol+FLT_MIN));
对于斯威夫特:
让音量 = AVAudioSession.sharedInstance().outputVolumeprint("输出音量:(volume)")
更新答案的来源:Get System Volume iOS
I'm trying to mute the device's ringer from within my app, but for some reason using AVSystemController like in this answer ( How to disable iOS System Sounds ) won't let me silence the device ALL the way down.. it drops it to a volume of 1 bar, but not completely silent.
I know it can be done, probably with a private API like AVSystemController, and I know that Apple will still approve the app if the user expects this kind of functionality from the app (since there are already 2 apps I found in the App Store which mutes the device programmatically with no need of jailbreaking or anything like that).
Those apps actually do something better - they actually toggle the actual mute, not just decreasing the volume to zero.
Does anyone know the way this is being done?
Any help will be greatly appreciated! Thanks!
This worked for me, I have a button that toggles sound on and off in a game. I set the float to 10 when I want sound on and 0 when I want sound off.
float value = 0.0f;
AudioSessionSetProperty(kAudioSessionProperty_CurrentHardwareOutputVolume, sizeof(float), &value);
Update:
Another option that is still working with iOS 9.1
float vol = [[AVAudioSession sharedInstance] outputVolume];
NSLog(@"output volume: %1.2f dB", 20.f*log10f(vol+FLT_MIN));
For Swift:
let volume = AVAudioSession.sharedInstance().outputVolume
print("Output volume: (volume)")
Sources for updates answer: Get System Volume iOS
这篇关于以编程方式使 iOS 设备静音/静音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:以编程方式使 iOS 设备静音/静音?
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01