ios - iOS 上的语音合成在加载时出现奇怪的错误,并且没有并发

标签 ios swift avfoundation text-to-speech speech-synthesis

我正在使用 AVFoundation 中的语音合成器,创建一个这样的语音实例:

import AVFoundation

class CanSpeak {

    let voices = AVSpeechSynthesisVoice.speechVoices()
    let voiceSynth = AVSpeechSynthesizer()
    var voiceToUse: AVSpeechSynthesisVoice?

    init(){
        for voice in voices {
            if voice.name == "Arthur"
            {
                voiceToUse = voice
            }
        }
    }

    func sayThis(_ phrase: String){
        let utterance = AVSpeechUtterance(string: phrase)
        utterance.voice = voiceToUse
        utterance.rate = 0.5
        voiceSynth.speak(utterance)
    }
}

我有两个问题。

  1. 没有并发。多次调用此函数会导致要说话的字符串排队。我不想要那个。如果多次调用此函数,并靠近在一起,我希望声音立即开始说话,即使这意味着自己说话。我该如何做到这一点?

2 - 一些我在加载时无法理解的奇怪错误:

2016-11-18 03:03:07.103349 mySKtest[687:87489] 0x17415ee50 Copy matching assets reply: XPC_TYPE_DICTIONARY { count = 2, transaction: 0, voucher = 0x0, contents = "Assets" => : { length = 3620 bytes, contents = 0x62706c6973743030d4000100020003000400050006012d01... } "Result" => : 0 } 2016-11-18 03:03:07.109254 mySKtest[687:87489] 0x17015e610 Copy assets attributes reply: XPC_TYPE_DICTIONARY { count = 1, transaction: 0, voucher = 0x0, contents = "Result" => : 1 } 2016-11-18 03:03:07.109547 mySKtest[687:87489] [MobileAssetError:1] Unable to copy asset attributes 2016-11-18 03:03:07.110080 mySKtest[687:87489] Could not get attribute 'LocalURL': Error Domain=MobileAssetError Code=1 "Unable to copy asset attributes" UserInfo={NSDescription=Unable to copy asset attributes} 2016-11-18 03:03:07.112341 mySKtest[687:87489] 0x17015e610 Copy assets attributes reply: XPC_TYPE_DICTIONARY { count = 1, transaction: 0, voucher = 0x0, contents = "Result" => : 1 } 2016-11-18 03:03:07.112416 mySKtest[687:87489] [MobileAssetError:1] Unable to copy asset attributes 2016-11-18 03:03:07.112523 mySKtest[687:87489] Could not get attribute 'LocalURL': Error Domain=MobileAssetError Code=1 "Unable to copy asset attributes" UserInfo={NSDescription=Unable to copy asset attributes} 2016-11-18 03:03:07.145658 mySKtest[687:87489] 0x174341c30 Copy matching assets reply: XPC_TYPE_DICTIONARY { count = 2, transaction: 0, voucher = 0x0, contents = "Assets" => : { length = 4198 bytes, contents = 0x62706c6973743030d4000100020003000400050006016f01... } "Result" => : 0 } 2016-11-18 03:03:07.148403 mySKtest[687:87489] 0x17015e610 Copy assets attributes reply: XPC_TYPE_DICTIONARY { count = 3, transaction: 0, voucher = 0x0, contents = "Attributes" => : { length = 526 bytes, contents = 0x62706c6973743030d4010203040506232458247665727369... } "Result" => : 0 "SandboxExtension" => { length = 269, contents = "b72954a376beb759be03a6411c3e2649f9845fd1;00000000;00000000;0000000000000015;com.apple.assets.read;00000001;01000003;00000000000ca4fc;/private/var/MobileAsset/Assets/com_apple_MobileAsset_VoiceServices_CustomVoice/54ffb86ce0ecd2c5bf871303b5690d327a571428.asset/AssetData" } } 2016-11-18 03:03:07.149858 mySKtest[687:87489] 0x1743414a0 Copy assets attributes reply: XPC_TYPE_DICTIONARY { count = 2, transaction: 0, voucher = 0x0, contents = "Attributes" => : { length = 526 bytes, contents = 0x62706c6973743030d4010203040506232458247665727369... } "Result" => : 0 }

最佳答案

至于#1,它可能不会发生。语音合成器是系统共享资源,因此作为 API 的客户端,系统如何处理调度多个请求是我们无法控制的。 (请注意,如果您重复使用同一个合成器,它会将额外的话语排队,但如果您创建多个合成器,它就无法说出在另一个合成器说话时请求的话语。)

不知道#2,抱歉。看起来像诊断文本,不一定是错误。大概值filing a bug大约,因为他们可能不想在没有实际问题时记录诊断。

奖励答案:您可以使用函数式编程来缩短语音选择:

let voice = AVSpeechSynthesisVoice.speechVoices().first(where: { $0.name == "Arthur" })

关于ios - iOS 上的语音合成在加载时出现奇怪的错误,并且没有并发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40663130/

相关文章:

iphone - 是否可以取消远程 AVPlayerItem 的下载?

ios - 检查 iPhone Swift 4 中的耳机端口是否可用

ios - 关于 initWithNavigationBarClass 的困惑——如何使用(新的 instanceType 方法)

javascript - 如何在 React Native 中重建 iOS Picker 动画

ios - UITextField 子类,下方有栏 : bar not going all the way

swift - 需要点击两次才能取消选中表格单元格

swift - 使用 URLSession 发布数据

ios - SearchBar显示过滤,但未显示正确的数据

ios - iOS 8 中的 SevenSwitch UI 背景颜色

iphone - 通过 AVAssetReader 读取音频样本