我知道 Apple 没有提供一种方法来检测iOS 5mute/silence
中 iPhone开关的状态。但是,我尝试了其他地方提到的一种技术,通过播放音频文件并测量其运行时间来检测这一点。但是,即使我的 iPhone 已静音,音频文件仍会播放整个持续时间。我正在使用和计算调用之前的时间。您是否知道播放音频文件的技巧,如果手机静音,它将提前/立即完成?[AVAudioPlayer play]
audioPlayerDidFinishPlaying
问问题
8779 次
1 回答
4
更新:抱歉,下面发布的代码对我来说很好,但我使用的是 iOS4。对于 iOS5,这个答案将解决您的问题 -使用 AVAudioPlayer 检测 iPhone 的响铃/静音/静音开关不起作用?
这段代码就是你需要的。定义一个全局gAudioSessionInited
变量。此标志告诉您静音开关是打开还是关闭。
// "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.");
}
于 2011-11-16T20:23:28.107 回答