当我的iOS应用程序处于后台模式时,我无法让AVSpeechSynthesizer 7工作。我已经添加了“应用程序播放音频”的关键应用程序的支持背景模式,但我仍然无法使它工作。
我还调查了创建一个AVMutableCompositionTrack的可能性,用AVSpeechSynthesizer的话,然后以某种方式与能够在后台运行的玩家一起玩,但没有运气。
在后台使用AVSpeechSynthesizer的人比我运气好吗?
发布于 2013-10-05 16:44:53
NSError *error = NULL;
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:&error];
if(error) {
// Do some error handling
}
[session setActive:YES error:&error];
if (error) {
// Do some error handling
}发布于 2017-01-16 02:49:45
对于Swid3,导入AVKit (或AVFoundation),然后添加
try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)到viewWillAppear()。这允许音频播放,而不考虑静音开关状态,并在后台与屏幕关闭。
*编辑: AVAudioSession是在AVFoundation中定义的,AVKit也可以使用
*编辑2:自动完成显示AVAudioSession在AVKit中可用的截图

发布于 2019-07-16 23:53:04
这段代码在Swift 5中适用于我
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: AVAudioSession.CategoryOptions.mixWithOthers)
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print(error)
}
let utterance = AVSpeechUtterance(string: voiceOutdata)
let synth = AVSpeechSynthesizer()
synth.speak(utterance)根据https://forums.developer.apple.com/thread/38917
AVAudioSessionCategoryOptionMixWithOthers与回放类别一起添加,当会话是可混合的时,在允许播放音频之前,会对应用程序“为什么”处于清醒状态进行一些内部检查。
https://stackoverflow.com/questions/19183591
复制相似问题