【问题标题】:Detecting iPhone mute switch in iOS 5在 iOS 5 中检测 iPhone 静音开关
【发布时间】:2011-11-16 20:17:16
【问题描述】:

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

【问题讨论】:

    标签: iphone objective-c ios mute


    【解决方案1】:

    更新:抱歉,下面发布的代码对我来说很好,但我使用的是 iOS4。对于 iOS5,这个答案将解决您的问题 - Detecting the iPhone's Ring / Silent / Mute switch using AVAudioPlayer not working?

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

    // "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.");
    }
    

    【讨论】:

    • 抱歉这个错误。它正在工作 ios5?抱歉耽搁了。
    猜你喜欢
    • 1970-01-01
    • 2011-10-17
    • 1970-01-01
    • 2015-01-19
    • 2017-08-11
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    • 2012-07-24
    相关资源
    最近更新 更多