使用 AVAudioPlayer 检测 iPhone 的铃声/静音/静音开关不起作用? [英] Detecting the iPhone's Ring / Silent / Mute switch using AVAudioPlayer not working?

查看:60
本文介绍了使用 AVAudioPlayer 检测 iPhone 的铃声/静音/静音开关不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已尝试使用这些方法来尝试检测响铃/静音开关是否处于活动状态:

I've tried using these methods in an attempt to detect that the Ring/Silent switch is active or not:

如何以编程方式感知 iPhone 静音开关?

AVAudioSession 类别不符合文档规定

但在我的 iPhone 4 上,state"值始终为Speaker"(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 安装一个完成程序).
如果完成过程很快被调用(允许 some 阈值) - 这意味着静音开关已打开.
这个技巧有很多警告,最大的一个是它不适用于所有音频类别.
如果您的应用在后台播放音频 - 请确保您在后台停止此测试,否则您的应用将永远在后台运行(也会被苹果拒绝).

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 的铃声/静音/静音开关不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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