Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从音频会话环境模式切换到播放模式会停止其他应用程序;音频_Ios_Objective C_Avaudiosession - Fatal编程技术网

Ios 从音频会话环境模式切换到播放模式会停止其他应用程序;音频

Ios 从音频会话环境模式切换到播放模式会停止其他应用程序;音频,ios,objective-c,avaudiosession,Ios,Objective C,Avaudiosession,如果我使用以下选项将应用程序的音频会话模式设置为AVAudioSessionCategoryPlayback: AVAudioSessionCategoryOptionInterruptSpokenAudioAndMixWithOthers | AVAudioSessionCategoryOptionDuckOthers 一切都很好,我的应用程序在后台播放苹果音乐 但是,如果在更改为AVAudioSessionCategoryPlayback之前,我的应用程序的音频会话类别为AvaudioSe

如果我使用以下选项将应用程序的音频会话模式设置为AVAudioSessionCategoryPlayback:

AVAudioSessionCategoryOptionInterruptSpokenAudioAndMixWithOthers |
AVAudioSessionCategoryOptionDuckOthers
一切都很好,我的应用程序在后台播放苹果音乐

但是,如果在更改为AVAudioSessionCategoryPlayback之前,我的应用程序的音频会话类别为AvaudioSessionCategoryBient,则Apple music(和其他音乐应用程序)将停止播放

以下是我的代码(我检查错误和返回值,但为了清楚起见,我删除了代码段中的这些部分:

// If I comment out this call, my app will duck others... 
// If it is commented in, rather than ducking, my app stops all other audio
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient
                                 withOptions:0
                                       error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];

// Do other stuff in the app and at some point the following code gets called

theOptions = AVAudioSessionCategoryOptionInterruptSpokenAudioAndMixWithOthers |
                     AVAudioSessionCategoryOptionDuckOthers

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
                                 withOptions:theOptions
                                       error:nil];
我已尝试在更改之间停用会话,但这不会产生任何不同,也不会每次都激活它。我能找到的唯一方法是允许我的应用程序播放音频并避开其他应用程序,即不在我的应用程序中的任何点设置环境会话,但我需要在应用程序中的某些点播放环境音频,因此此解决方案无法通过ble.请帮助:)

(Swift 4答案)在iOS 11上测试-设置环境会话时,您应该使用.duckOthers或.mixWithOthers

func prepareSessionForPlayback() {
    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, with: [.duckOthers]) // .duckOthers or .mixWithOthers
        do {
            try AVAudioSession.sharedInstance().setActive(true)
        } catch let error as NSError {
            print (error)
        }
    } catch let error as NSError {
        print (error)
    }
}

func sessionDidFinish() {
    do { try AVAudioSession.sharedInstance().setActive(false) } catch { }
}

SessiondFinish完成后,可以设置其他选项。如果您使用的是TTS,那么在调用SessiondFinish之前,您还需要确保在最后一次发言中没有设置.postateranceDelay。使用AVSpeechSynthesizerDelegate方法捕捉话语何时真正完成。

您解决了这个问题吗?我也有同样的问题…不幸的是没有。我只是没有使用环境会话类别。注意,我没有在任何较新的操作系统版本(>10)上测试这一点,以查看行为是否有所不同。