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

标签 ios audio avaudiosession

我似乎无法在 SDK 中找到如何以编程方式感知 iPhone 上的静音按钮/开关。当我的应用程序播放背景音乐时,它会正确响应音量按钮,而无需我执行任何代码,但当我使用静音开关时,它会一直播放。

如何测试静音的位置?

(注意:我的程序有自己的静音开关,但我希望物理开关能够覆盖它。)

最佳答案

谢谢,JPM。事实上,您提供的链接会导致正确的答案(最终。;)为了完整性(因为 S.O. 应该是快速答案的来源!)...

// "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 - 如何以编程方式感知 iPhone 静音开关?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/287543/

相关文章:

ios - 在聊天应用程序中拉伸(stretch)气泡图像

android - 是否可以通过 A2DP 同时向多个耳机广播?

ios - 带 SwiftUI 的播放/停止按钮

ios - iOS中的程序化无间隙音频序列

swift - 音频播放锁屏控件不显示

ios - AVAudioRecorder/AVAudioSession 与 Apple Airpods

android - 使用 HTML、Javascript 进行移动开发的最佳平台。除了 native 应用程序开发

ios - 如何在 block 中使用 NSTextView?

objective-c - AVAssetWriter 无效的写入 URL

ios - AVAudioSession 密码锁在 iOS8 上停止 AVAudioRecorder