在iOS 5中检测iPhone静音开关 [英] Detecting iPhone mute switch in iOS 5

查看:142
本文介绍了在iOS 5中检测iPhone静音开关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Apple没有提供在 iOS 5 中检测iPhone 静音/静音切换状态的方法。但是,我尝试了其他地方提到的技术,通过播放音频文件并测量其运行时来检测这一点。然而,即使我的iPhone静音,音频文件仍然在整个持续时间内播放。我正在使用 [AVAudioPlayer播放] 并计算调用 audioPlayerDidFinishPlaying 之前的时间。你知道播放音频文件的技巧吗?如果电话被静音,它会提前/立即完成吗?

I know that Apple does not provide a way to detect the state of the iPhone mute/silence switch in iOS 5. However, I have I tried a technique mentioned elsewhere to detect this by playing an audio file and measuring its runtime. However, even though my iPhone was muted, the audio file still played the entire duration. I'm using [AVAudioPlayer play] and computing the time before audioPlayerDidFinishPlaying is called. Do you know of a trick to play an audio file, which will complete early/immediately if the phone is muted?

推荐答案

更新:对不起,下面发布的代码对我来说很好,但我使用的是iOS4。对于iOS5,这个答案将解决您的问题 - 使用AVAudioPlayer检测iPhone的响铃/静音/静音开关不起作用?

UPDATE: Sorry the below code posted works fine for me but I am using iOS4. For iOS5 this answer is what will solve your problem - Detecting the iPhone's Ring / Silent / Mute switch using AVAudioPlayer not working?

这段代码就是您所需要的。定义全局 gAudioSessionInited var。此标志会告诉您静音开关是打开还是关闭。

This piece of code is what you need. Define a global gAudioSessionInited var. This flag tells you whether your mute switch is on or off.

// "Ambient" makes it respect the mute switch. Must call this once to init session
if (!gAudioSessionInited)
{
    AudioSessionInterruptionListener inInterruptionListener = NULL;
    OSStatus error;
    if ((error = AudioSessionInitialize (NULL, NULL, inInterruptionListener, NULL)))
        NSLog(@"*** Error *** error in AudioSessionInitialize: %d.", error);
    else
        gAudioSessionInited = YES;
}

SInt32  ambient = kAudioSessionCategory_AmbientSound;
if (AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (ambient), &ambient))
{
    NSLog(@"*** Error *** could not set Session property to ambient.");
}

这篇关于在iOS 5中检测iPhone静音开关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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