ios 如何检测iphone处于静音模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/833304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:53:58  来源:igfitidea点击:

How to detect iphone is on silent mode

iosobjective-ciphonecocoa-touchaudio

提问by Jyotsna

I am developing an application. In that i want to detect through coding that "is iPhone on silent mode or not?". I am developing it by using cocoa with Objective-C.

我正在开发一个应用程序。我想通过编码检测“iPhone 是否处于静音模式?”。我正在通过使用可可和 Objective-C 来开发它。

If anyone knows it kindly reply.

如果有人知道请回复。

回答by Neil

The reason Pirripli's code does not work is that the simulator does not support the test and the code does not check for errors. Corrected code would look like:

Pirripli 的代码不工作的原因是模拟器不支持测试并且代码没有检查错误。更正后的代码如下所示:

CFStringRef state = nil;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
OSStatus status = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);

if (status == kAudioSessionNoError)
{
    return (CFStringGetLength(state) == 0);   // YES = silent
}
return NO;

回答by Alastair Stuart

It's possible by testing for a NULL audio route using AudioToolBox:

可以通过使用 AudioToolBox 测试 NULL 音频路由:

UInt32 routeSize = sizeof (CFStringRef);
CFStringRef route;

AudioSessionGetProperty (
                         kAudioSessionProperty_AudioRoute,
                         &routeSize,
                         &route
                        );

if (route == NULL) {
    NSLog(@"Silent switch is on");
}

If route is NULL then there's no available audio outputs. If it's "Headset" or "Headphones" then the silent ringer switch could still be on. However, it will never be on when it's set to "Speaker".

如果 route 为 NULL,则没有可用的音频输出。如果是“耳机”或“耳机”,则静音铃声开关可能仍处于打开状态。但是,当它设置为“扬声器”时,它永远不会打开。

You're probably best testing for this in your audio route change property listener, which is set below:

您可能最好在音频路由更改属性侦听器中对此进行测试,该侦听器设置如下:

AudioSessionAddPropertyListener (
                                 kAudioSessionProperty_AudioRouteChange,
                                 audioRouteChangeListenerCallback,
                                 self
                                 );

Note: If you're doing anything funky like overriding audio routes, then this answer may not apply.

注意:如果你正在做任何像覆盖音频路由这样的时髦事情,那么这个答案可能不适用。

Setting up and tearing down an audio session in its entirety is probably beyond the scope of this answer.

完整地设置和拆除音频会话可能超出了本答案的范围。

回答by Chris Ladd

For completeness, building off this linkfrom Dan Bon, I implement the following method to solve this problem in my apps. One thing to note is that the code checks for the iPhone simulator first - executing the below code will crash the simulator. Anyone know why?

为了完整起见,我从 Dan Bon建立了这个链接,我实现了以下方法来解决我的应用程序中的这个问题。需要注意的一件事是,代码首先检查 iPhone 模拟器 - 执行以下代码会使模拟器崩溃。有谁知道为什么?

-(BOOL)silenced {
     #if TARGET_IPHONE_SIMULATOR
         // return NO in simulator. Code causes crashes for some reason.
     return NO;
     #endif

    CFStringRef state;
    UInt32 propertySize = sizeof(CFStringRef);
    AudioSessionInitialize(NULL, NULL, NULL, NULL);
    AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
    if(CFStringGetLength(state) > 0)
        return NO;
    else
        return YES;

}

Declaring this right in the view controller, you'd simply check

在视图控制器中声明这一点,您只需检查

if ([self silenced]) {
     NSLog(@"silenced");

else {
     NSLog(@"not silenced");
}

Or, obviously, you could declare it in some kind of helper class. A more elegant solution might be a category addition on UIApplication or some such other class...

或者,显然,您可以在某种帮助类中声明它。更优雅的解决方案可能是在 UIApplication 或其他一些类上添加类别...

回答by giavac

You can use Audio Route property as suggested by the previous answers, but keep in mind that: - It works only if the Audio Category is AmbientSound - You should not initialize Audio Session more than once in your app (see Audio Session Programming Guide) - You should release those CFStringRef to avoid mem leaks

您可以按照前面的答案的建议使用 Audio Route 属性,但请记住: - 仅当音频类别为 AmbientSound 时才有效 - 您不应在应用程序中多次初始化音频会话(请参阅音频会话编程指南) -您应该释放那些 CFStringRef 以避免内存泄漏

In case the current audio category is not AmbientSound though, you can think of changing it temporarily, perform the check on Audio Route property, and then restoring the original Audio Category.

如果当前音频类别不是AmbientSound,您可以考虑临时更改它,检查Audio Route 属性,然后恢复原始音频类别。

Note that changing Audio Category will restore the default Audio Route for that category, given the current hardware configuration (i.e. whether there are headphones plugged in or not, etc).

请注意,根据当前的硬件配置(即是否插入耳机等),更改音频类别将恢复该类别的默认音频路由。