use*_*ion 17 objective-c avfoundation ios ios7
AVSpeechSynthesizer当我的iOS应用程序处于后台模式时,我无法让iOS 7 正常工作.我已将" App播放音频 "键添加到应用程序支持的后台模式,但我仍然无法使其工作.
我还研究了创造一个AVMutableCompositionTrack有AVSpeechSynthesizer话语的可能性,然后以一种能够在后台运行的播放器以某种方式播放它 - 但没有运气.
AVSpeechSynthesizer在后台使用时,有没有人比我好运?
imi*_*aly 38
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
}
Run Code Online (Sandbox Code Playgroud)
This code works for me in 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)
Run Code Online (Sandbox Code Playgroud)
According to https://forums.developer.apple.com/thread/38917
AVAudioSessionCategoryOptionMixWithOthers is added along with the Playback Category when the session is mixable there are some internal checks as to the "why" an application is awake before it is allowed to play audio.
对于swift 3,导入AVKit(或AVFoundation)然后添加
try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
Run Code Online (Sandbox Code Playgroud)
to viewWillAppear().无论静音开关状态如何,都可以播放音频,屏幕关闭时可以在后台播放音频.
*编辑:AVAudioSession在AVFoundation中定义,也可用于AVKit
*编辑2:自动完成的屏幕截图,显示AVAudioSession在AVKit中可用
| 归档时间: |
|
| 查看次数: |
6675 次 |
| 最近记录: |