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

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

问题描述

我尝试使用这些方法试图检测Ring / Silent开关是否处于活动状态:

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库。

没有为我工作(当你开始实际使用音频时不起作用)。

我在思考他是如何做到的,然后意识到当我们沉默时声音开始播放时,音频完成呼叫几乎被呼叫。

更具体一点:

使用<$播放系统声音c $ c> AudioServicesPlaySystemSound 一旦开始就会完成播放。

当然,这只适用于尊重si的音频类别借出开关(默认 AVAudioSessionCategoryAmbient 尊重它)。

所以诀窍是创建一个系统声音,最好是一个无声的声音,并继续播放它一遍又一遍地检查从播放到完成所花费的时间(使用 AudioServicesAddSystemSoundCompletion 安装完成程序)。

如果完成proc很快就会被调用(允许一些阈值) - 这意味着静音开关打开。

这个技巧有很多警告,最大的一个是它不会适用于所有音频类别。

如果您的应用在后台播放音频 - 确保您在后台停止此测试,或者您的应用将在后台永久运行(并将被苹果拒绝,也是)。

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天全站免登陆