目前,我正在尝试使用AVSpeechSynthesizer
在iOS Safari扩展中使用文本:
let synthesizer = AVSpeechSynthesizer()
...
let utterance = AVSpeechUtterance(string: self.text)
utterance.rate = 0.55;
self.synthesizer.speak(utterance)
在模拟器上--这很好用。但是,在物理设备上,我得到以下错误(即使设备是非静音/卷-升):
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
我已经浏览了相当多的SO和Apple论坛的线程,并尝试了许多提出的解决方案,但都没有成功。以下是我尝试过的事情:
AVFAudio.framework
和AVFoundation.framework
链接到扩展。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)")
}
这实际上会导致在上述相同错误之前抛出另一个错误:
Error starting audio: The operation couldn’t be completed. (OSStatus error 2003329396.)
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)")
}
这将打印以下内容:
**playing sound
[aqsrv] AQServer.cpp:72 Exception caught in AudioQueueInternalNotifyRunning - error -66671
Audio, AirPlay, and Picture in Picture
(扩展程序不可用)。任何帮助都将不胜感激。
发布于 2022-09-12 18:13:15
编辑:
下面的解决方案在提交到时会因为验证错误而被拒绝。
我向苹果提交了一个技术支持事件,这是他们的回应:
Safari扩展非常短,因此不适合音频播放或语音合成.无法用手动添加的plist条目验证Xcode中的应用程序扩展是设计的行为。一般建议使用JavaScript与网络语音API相结合来合成语音。
TLDR:在Safari扩展中使用文本到语音的Web语音API,而不是AVSpeechSynthesizer
.
原始答案:
将以下内容添加到扩展的 Info.plist
中,可以按预期播放音频:
<dict>
...
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>
...
</dict>
有趣的是,它实际上在控制台中显示了与以前相同的错误,但它确实播放了音频。
https://stackoverflow.com/questions/73693209
复制相似问题