我在iOS 7下无法使AVSpeechSynthesizer
在我的iOS应用程序处于后台模式时工作。我已将"App plays audio"键添加到应用程序支持的后台模式中,但仍无法使其正常工作。
我还调查了创建一个带有AVSpeechSynthesizer
话语的AVMutableCompositionTrack
的可能性,然后以某种方式使用能够在后台运行的播放器进行播放,但没有成功。
是否有人在后台使用AVSpeechSynthesizer
方面比我更加成功?
我在iOS 7下无法使AVSpeechSynthesizer
在我的iOS应用程序处于后台模式时工作。我已将"App plays audio"键添加到应用程序支持的后台模式中,但仍无法使其正常工作。
我还调查了创建一个带有AVSpeechSynthesizer
话语的AVMutableCompositionTrack
的可能性,然后以某种方式使用能够在后台运行的播放器进行播放,但没有成功。
是否有人在后台使用AVSpeechSynthesizer
方面比我更加成功?
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
}
这段代码在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)