以编程方式静音/静音iOS设备? [英] Mute/Silence an iOS device programmatically?

查看:584
本文介绍了以编程方式静音/静音iOS设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图在我的应用程序中静音设备的铃声,但出于某些原因使用AVSystemController就像在这个答案中一样(如何禁用iOS系统声音)不会让我让设备完全静音...它将它降低到1 bar的音量,但不是完全静音。

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.

我知道可以使用像AVSystemController这样的私有API,我知道如果用户期望从应用程序中获得这种功能,Apple仍会批准该应用程序(因为我已经在App Store 中找到了2个应用程序,它们以编程方式将设备静音,不需要越狱或类似的东西。)

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!

推荐答案

这对我有用,我有一个按钮可以在游戏中打开和关闭声音。我希望声音开启时将浮点数设置为10,当我想关闭声音时将浮点设置为0.

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);

更新:

另一个仍在使用iOS 9.1的选项

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));

对于Swift:

let volume = AVAudioSession.sharedInstance().outputVolume   
print("Output volume: \(volume)")

更新来源答案:获取系统卷iOS

这篇关于以编程方式静音/静音iOS设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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