AVSpeechSynthesizer在iOS10上不起作用

lit*_*ium 22 ios avspeechsynthesizer swift

我的AVSpeechSynthesizer代码不能在设备上运行(iOS 10),但它可以在iOS 9.x上运行,现在它可以在模拟器中运行.

let str = self.audioOutput //just some string here, this string exists, and it's in english
let synth = AVSpeechSynthesizer()
let utterance = AVSpeechUtterance(string: str)
    utterance.rate = AVSpeechUtteranceDefaultSpeechRate
let lang = "en-US"

utterance.voice = AVSpeechSynthesisVoice(language: lang)
synth.speakUtterance(utterance)
Run Code Online (Sandbox Code Playgroud)

我收到这个错误:

MobileAssetError:1] Unable to copy asset attributes
Could not get attribute 'LocalURL': Error Domain=MobileAssetError Code=1 "Unable to copy asset attributes"
UserInfo={NSDescription=Unable to copy asset attributes}
0x1741495e0 Copy assets attributes reply: XPC_TYPE_DICTIONARY  <dictionary: 0x1741495e0> { count = 1, transaction: 0, voucher = 0x0, contents =
"Result" => <int64: 0x1744203a0>: 1}
Run Code Online (Sandbox Code Playgroud)

在此之前有类似的错误消息:

Unable to copy asset information from https://mesu.apple.com/assets/ for asset type
Run Code Online (Sandbox Code Playgroud)

有谁知道如何解决这个问题?我知道有一些解决方法(用户必须转到设置 - >常规并切换Speak Selection,例如)但我不认为这是一个真正的解决方案.

更新:我创建了一个新项目(XCode8/Swift3 /没有其他pod /框架等).它在模拟器中工作,但它在我的设备上给了我相同的错误.

更新2:它适用于设备.我有类似的错误消息(无法复制资产属性等),但它现在有效.我不知道它是什么.

小智 24

关闭静音模式(物理开关).它适用于我的情况.


Car*_*ten 6

我刚刚在iPad Mini 4上遇到过同样的问题.这个版本没有物理开关.但是如果你打开控制中心(向上滑动),就会出现一个静音按钮.关闭它,问题自行解决.

在此输入图像描述


Jit*_*odi 1

我已经尝试了您的代码,仅进行了一项更改即可正常工作。尝试一次

 synth.speak(utterance)
Run Code Online (Sandbox Code Playgroud)

我的整个代码就像

**`import  AVFoundation`**


        let str = "once" //just some string here, this string exists, and it's in english
        let synth = AVSpeechSynthesizer()
        let utterance = AVSpeechUtterance(string: str)
        utterance.rate = AVSpeechUtteranceDefaultSpeechRate
        let lang = "en-US"

        utterance.voice = AVSpeechSynthesisVoice(language: lang)
        synth.speak(utterance)
Run Code Online (Sandbox Code Playgroud)