问题
I'm using the speech synth in AVFoundation, creating an instance of a voice like this:
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)
}
}
I have two problems.
- There's no concurrency. Calling this function multiple times results in a queuing of strings to speak. I don't want that. If there's multiple calls to this function, close together, I'd like the voice to immediately begin speaking, even if that means speaking over itself. How do I make that happen?
2 - some odd errors that I don't understand on loading:
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:
As for #1, it's probably not gonna happen. The speech synthesizer is a system shared resource, so how the system handles scheduling multiple requests is out of our control as clients of the API. (Note that if you reuse the same synthesizer, it queues up extra utterances, but if you create multiple synthesizers, it fails to speak utterances that are requested while another synthesizer is speaking.)
Dunno about #2, sorry. Looks like diagnostic text, not necessarily an error. Probably worth filing a bug about, since they probably don't want to be logging diagnostics when there's no actual problem.
Bonus answer: You can use functional programming to make the selection of voice a bit shorter:
let voice = AVSpeechSynthesisVoice.speechVoices().first(where: { $0.name == "Arthur" })
来源:https://stackoverflow.com/questions/40663130/speech-synthesis-on-ios-weird-errors-on-loading-and-no-concurrency