在 iOS 5 中检测 iPhone 静音开关

发布于 2024-12-16 12:27:54 字数 288 浏览 5 评论 0原文

我知道 Apple 没有提供检测 iOS 5 中 iPhone 静音/静音 开关状态的方法。但是,我尝试了其他地方提到的一种技术,通过播放音频文件并测量其运行时间来检测这一点。然而,即使我的 iPhone 被静音,音频文件仍然在整个播放过程中播放。我正在使用 [AVAudioPlayer play] 并计算调用 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

他不在意 2024-12-23 12:27:55

更新:抱歉,下面发布的代码对我来说工作正常,但我使用的是 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.");
}

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?

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.");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文