简体   繁体   English

无法播放来自 iOS Swift Safari 设备扩展的音频/语音(OSStatus 错误 2003329396)

[英]Unable to play audio / speech from iOS Swift Safari Extension on device (OSStatus error 2003329396)

I'm currently trying to use AVSpeechSynthesizer to speak text from within an iOS Safari extension:我目前正在尝试使用AVSpeechSynthesizer从 iOS Safari 扩展中说出文本:

let synthesizer = AVSpeechSynthesizer()
...

let utterance = AVSpeechUtterance(string: self.text)
utterance.rate = 0.55;
self.synthesizer.speak(utterance)

On a simulator this works fine.在模拟器上这工作正常。 However, on a physical device, I get the following error (even when the device is unmuted/volume-up):但是,在物理设备上,我收到以下错误(即使设备未静音/音量增大):

NSURLConnection finished with error - code -1002
NSURLConnection finished with error - code -1002
NSURLConnection finished with error - code -1002
[AXTTSCommon] Failure starting audio queue alp!
[AXTTSCommon] Run loop timed out waiting for free audio buffer
[AXTTSCommon] _BeginSpeaking: speech cancelled error: Error Domain=TTSErrorDomain Code=-4001 "(null)"
[AXTTSCommon] _BeginSpeaking: couldn't begin playback

I have looked through quite a few SO and Apple Dev Forums threads and have tried many of the proposed solutions with no luck.我浏览了相当多的 SO 和 Apple Dev Forums 主题,并尝试了许多建议的解决方案,但都没有运气。 Here are the things I've tried:以下是我尝试过的事情:

  1. Linking AVFAudio.framework and AVFoundation.framework to the extension.AVFAudio.frameworkAVFoundation.framework链接到扩展。

  2. Starting an AVAudioSession prior to playing the utterance:在播放话语之前启动AVAudioSession

do {
    let session = AVAudioSession.sharedInstance()
    try session.setCategory(.playback, mode: .default, options: [.mixWithOthers, .allowAirPlay])
    try session.setActive(true, options: .notifyOthersOnDeactivation)
} catch let error {
    print("Error starting audio: \(error.localizedDescription)")
}

This actually results in another error being thrown right before the same errors above:这实际上导致在上述相同错误之前引发另一个错误:

Error starting audio: The operation couldn’t be completed. (OSStatus error 2003329396.)
  1. Playing a plain mp3 audio file:播放纯mp3音频文件:
guard let url = Bundle.main.url(forResource: "sample", withExtension: "mp3") else {
    print("Couldn't find file")
    return
}

do {
    self.player = try AVAudioPlayer(contentsOf: url)
    self.player.play()
    print("**playing sound")
} catch let error as NSError {
    print("Error playing sound: \(error.localizedDescription)")
}

This prints the following:这将打印以下内容:

**playing sound
[aqsrv]              AQServer.cpp:72    Exception caught in AudioQueueInternalNotifyRunning - error -66671
  1. Enabling Audio, AirPlay, and Picture in Picture in Background Modes for the main target app (not available for the extension).为主要目标应用程序启用Audio, AirPlay, and Picture in Picture背景模式(不适用于扩展程序)。

Any help would be appreciated.任何帮助,将不胜感激。

Adding the following to the extension's Info.plist allowed the audio to play as expected:将以下内容添加到扩展的Info.plist允许音频按预期播放:

<dict>
    ...
    <key>UIBackgroundModes</key>
    <array>
        <string>audio</string>
    </array>
    ...
</dict>

Interestingly, it actually shows the same errors in the console as before, but it does play the audio.有趣的是,它实际上在控制台中显示了与以前相同的错误,但它确实播放了音频。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM