我尝试使用这些方法来检测响铃/静音开关是否处于活动状态:
I've tried using these methods in an attempt to detect that the Ring/Silent switch is active or not:
如何以编程方式感应 iPhone 静音开关?
AVAudioSession 类别不按文档要求工作
但在我的 iPhone 4 上,状态"值始终为扬声器"(CFStringGetLength(state) 返回的长度值始终为 7).有没有人成功使用过这个方法?如果是,在什么样的设备和 SDK 版本上?
But on my iPhone 4, the "state" value is always "Speaker" (and the length value returned by CFStringGetLength(state) is always 7). Has anyone used this method successfully? If so, on what kind of device and SDK version?
我这样称呼它:
- (BOOL)deviceIsSilenced {
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
OSStatus audioStatus = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
if (audioStatus == kAudioSessionNoError) {
NSLog(@"audio route: %@", state) // "Speaker" regardless of silent switch setting, but "Headphone" when my headphones are plugged in
return (CFStringGetLength(state) <= 0);
}
return NO;
}
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
audioSession.delegate = self;
[audioSession setCategory:AVAudioSessionCategoryAmbient error:nil];
[audioSession setActive:YES error:nil];
NSLog(@"muted? %i", [self deviceIsSilenced]);
...
}
我在想,当手机上的物理开关被切换时,可能会触发其他一些(更准确的)kAudioSessionProperty 事件.有人有什么想法吗?
I was thinking maybe some other (more accurate) kAudioSessionProperty event is fired when the physical switch on the phone is ... switched. Anyone have any ideas?
顺便说一句,我将 AVAudioSessionCategoryAmbient 类别与我的 [AVAudioSession sharedInstance] 一起使用.
By the way, I'm using the AVAudioSessionCategoryAmbient category with my [AVAudioSession sharedInstance].
更新:我也尝试过使用不同的音频类别和一些其他音频会话属性,在静音/取消静音开关时似乎没有触发.:(
Update: I've also tried using different audio categories, and a handful of other audio session properties, none seem to fire when muting/unmuting the switch. :(
一月.2014 年 1 月 1 日更新: 这有点骇人听闻,我在 iPhone 5S 上使用它进行多任务处理时遇到了崩溃,但是 SoundSwitch 库链接在新接受的答案中,如果您想检测静音开关,您可以这样做.它甚至适用于 iOS 7.
Jan. 1, 2014 Update: It's a bit of a hack, and I encountered a crash while multitasking w/ it on my iPhone 5S, but the SoundSwitch library linked in the new accepted answer is the way to go if you want to detect the silent switch. It even works in iOS 7.
我浏览了这个 VSSilentSwitch 库.
对我不起作用(当您开始实际使用音频时不起作用).
我在想他是怎么做到的,然后意识到,当我们沉默时,几乎在声音开始播放时,音频完成调用就被调用了.
再具体一点:
使用 AudioServicesPlaySystemSound
播放的系统声音将在开始后立即完成播放.
当然,这只适用于尊重静音开关的音频类别(默认 AVAudioSessionCategoryAmbient
尊重它).
所以诀窍是创建一个系统声音,最好是无声的声音,并不断地反复播放,同时检查从播放到完成所用的时间(使用 AudioServicesAddSystemSoundCompletion
安装完成过程).
如果很快就会调用完成过程(允许 一些 阈值) - 这意味着静音开关已打开.
这个技巧有很多注意事项,最大的一个是它不适用于所有音频类别.
如果您的应用在后台播放音频 - 请确保您在后台停止此测试,否则您的应用将永远在后台运行(也会被苹果拒绝).
I went through this VSSilentSwitch library.
Didn't work for me (doesn't work when you start actually using audio).
I was thinking on how he did it, and then realised that the audio completion call is being called almost as soon as the sound begins playing when we're silent.
To be a bit more specific:
System sounds being played using AudioServicesPlaySystemSound
will complete playback as soon as it started.
Of course, this will only work on audio categories that respect the silent switch (the default AVAudioSessionCategoryAmbient
respects it).
So the trick is to create a system sound, preferably of a silent sound, and keep playing it over and over again, while checking the time it took from playback to completion (install a completion procedure using AudioServicesAddSystemSoundCompletion
).
If the completion proc is called very soon (allow some threshold) - it means the silent switch is on.
This trick has many caveats, the biggest one being the fact that it won't work on all audio categories.
If your app plays audio in the background - make sure you stop this test while in the background or your app will run forever in the background (and will be rejected by apple, too).
这篇关于使用 AVAudioPlayer 检测 iPhone 的响铃/静音/静音开关不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!