iOS 9检测静音模式 [英] iOS 9 detect silent mode
问题描述
我已经寻找了很长时间,在放弃之前将其发布在这里作为最后的尝试. 我想检测我当前是否处于静音模式.
I've been looking for a long time, posting here as a final try before giving up. I want to detect if I'm currently on Silent mode or not.
我发现了一种变通办法(播放假声音并检查完成情况),但仅当我不在AVAudioSessionCategoryPlayAndRecord
模式时,该变通办法才能正常工作.
正是在屏幕上,我可以在其中录制想要实现的音频和视频,从而知道是否应该播放UI声音.
I found a workaround (playing a fake sound and checking the completion) that works fine but only when I'm not in AVAudioSessionCategoryPlayAndRecord
mode.
This is precisely on a screen where I can record audio and video that I want to achieve this in order to know whether I should play UI sounds or not.
总而言之,我试图找到一种在AVAudioSessionCategoryPlayAndRecord
模式下检测静默模式的方法.
To sum up, I trying to find a way to detect silent mode when on AVAudioSessionCategoryPlayAndRecord
mode.
推荐答案
这里是一种尝试通过将音频会话类别短暂切换到SoloAmbient(确实尊重无声开关的类别)来尝试读取开关的解决方案-读取开关-然后切换回去.这可能是最适合您的方法.
Here is a solution that attempts reading the switch by switching the audio session category briefly to SoloAmbient (a category that does respect the silent switch) - reading the switch - and then switching back. this might be the best approach for you.
如果交换音频会话类别会干扰您的应用,我建议也许在播放音频之前进行检查,并使用您检测到的值,然后对静音开关进行反应.这是一个变通方法,但它应该为您提供一些信息.
if swapping audio session category would interfere with your app, i would suggest perhaps conduct a check before playing audio and use the value you detect then react to silent switch on/off. It is a work around but it should give you some information.
切换到环境类别,确定静音开关是否打开,然后将会话切换回我需要的音频设置.在确定开关是否打开之后,您将需要确定所需的音频会话类别,并切换到该类别.
switch to ambient category, determine if the silent switch is on, then switch session back to the audio settings I require. You will need to work out what audio session category you need and swap to that after you determine if the switch is on.
-(BOOL)muteSwitchEnabled {
#if TARGET_IPHONE_SIMULATOR
// set to NO in simulator. Code causes crashes for some reason.
return NO;
#endif
// switch to Ambient to detect the switch
AVAudioSession* sharedSession = [AVAudioSession sharedInstance];
[sharedSession setCategory:AVAudioSessionCategoryAmbient error:nil];
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
BOOL muteSwitch = (CFStringGetLength(state) <= 0);
NSLog(@"Mute switch: %d",muteSwitch);
// code below here is just restoring my own audio state, YMMV
_hasMicrophone = [sharedSession inputIsAvailable];
NSError* setCategoryError = nil;
if (_hasMicrophone) {
[sharedSession setCategory: AVAudioSessionCategoryPlayAndRecord error: &setCategoryError];
// By default PlayAndRecord plays out over the internal speaker. We want the external speakers, thanks.
UInt32 ASRoute = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,
sizeof (ASRoute),m&ASRoute);
} else {
// Devices with no mic don't support PlayAndRecord - we don't get playback, so use just playback as we don't have a microphone anyway
[sharedSession setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];
if (setCategoryError) {
NSLog(@"Error setting audio category! %@", setCategoryError);
}
return muteSwitch;
}
}
这篇关于iOS 9检测静音模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!