2017-05-02 8 views
0

Swiftで音声合成と音声認識を組み合わせようとすると、「LocalURL属性の取得に失敗しました:エラードメイン= MobileAssetErrorコード= 1」という結果が表示されます。 "UserInfo = {NSDescription =資産属性をコピーできません}"という資産属性をコピーすることができません。その後、テキストをスピーチすることはできますが、アプリケーションを再起動するまで、テキストが音声に変わります。音声認識装置が音声合成装置を破損する

let identifier = "\(Locale.current.languageCode!)_\(Locale.current.regionCode!)" // e.g. en-US 
speechRecognizer = SFSpeechRecognizer(locale: Locale.init(identifier: identifier))! 

if audioEngine.isRunning { 
    audioEngine.stop() // will also stop playing music. 
    recognitionRequest?.endAudio() 
    speechButton.isEnabled = false 
} else { 
    recordSpeech() // here we do steps 1 .. 12 
} 

// recordSpeech() : 

if recognitionTask != nil { // Step 1 
    recognitionTask?.cancel() 
    recognitionTask = nil 
} 

let audioSession = AVAudioSession.sharedInstance() // Step 2 
do { 
    try audioSession.setCategory(AVAudioSessionCategoryRecord) 
    try audioSession.setMode(AVAudioSessionModeMeasurement) 
    try audioSession.setActive(true, with: .notifyOthersOnDeactivation) 
} catch { 
    print("audioSession properties weren't set because of an error.") 
} 

recognitionRequest = SFSpeechAudioBufferRecognitionRequest() // Step 3 

guard let inputNode = audioEngine.inputNode else { 
    fatalError("Audio engine has no input node") 
} // Step 4 

guard let recognitionRequest = recognitionRequest else { 
    fatalError("Unable to create an SFSpeechAudioBufferRecognitionRequest object") 
} // Step 5 

recognitionRequest.shouldReportPartialResults = true // Step 6 

recognitionTask = speechRecognizer.recognitionTask(with: recognitionRequest, resultHandler: { (result, error) in // Step 7 

    var isFinal = false // Step 8 

    if result != nil { 

     print(result?.bestTranscription.formattedString as Any) 

     isFinal = (result?.isFinal)! 
     if (isFinal) { 
      if (result != nil) { 
       self.speechOutput.text = self.speechOutput.text + "\n" + (result?.bestTranscription.formattedString)! 
      } 
     } 
    } 

    if error != nil || isFinal { // Step 10 
     self.audioEngine.stop() 
     inputNode.removeTap(onBus: 0) 

     self.recognitionRequest = nil 
     self.recognitionTask = nil 
     self.speechButton.isEnabled = true 

    } 
}) 

let recordingFormat = inputNode.outputFormat(forBus: 0) // Step 11 
inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer, when) in 
    self.recognitionRequest?.append(buffer) 
} 

audioEngine.prepare() // Step 12 

do { 
    try audioEngine.start() 
} catch { 
    print("audioEngine couldn't start because of an error.") 
} 

私は時に私のコードをベースにこれチュートリアル使用:私が使用している場合、私はコンバインのであれば、それ自身のメソッドを「言う」ことが、うまく機能

http://www.appcoda.com/siri-speech-framework/

func say(_ something : String, lang : String) { 


    let synth = AVSpeechSynthesizer() 
    synth.delegate = self 


    print(something) // debug code, works fine 
    let identifier = "\(Locale.current.languageCode!)-\(Locale.current.regionCode!)" 
    let utterance = AVSpeechUtterance(string: something) 
    utterance.voice = AVSpeechSynthesisVoice(language: identifier) 

    synth.speak(utterance) 
} 

2つは、音声認識を行った後、シンセサイザはもはや動作しません。解決策の方向へのヒント?私は何かが正常に元の状態に復元されていないと思うが、私は何を理解するように見えることはできません。

答えて

0

grrrが...

これが解決され、十分に見ていないについては申し訳ありません、しかし私に多くの時間を負けまし。

func say(_ something : String, lang : String) { 

    let audioSession = AVAudioSession.sharedInstance() 
    do { 
     // this is the solution: 
     try audioSession.setCategory(AVAudioSessionCategoryPlayback) 
     try audioSession.setMode(AVAudioSessionModeDefault) 
     // the recognizer uses AVAudioSessionCategoryRecord 
     // so we want to set it to AVAudioSessionCategoryPlayback 
     // again before we can say something 
    } catch { 
     print("audioSession properties weren't set because of an error.") 
    } 

    synth = AVSpeechSynthesizer() 

    synth.delegate = self 


    print(something) 
    let identifier = "\(Locale.current.languageCode!)-\(Locale.current.regionCode!)" 
    let utterance = AVSpeechUtterance(string: something) 
    utterance.voice = AVSpeechSynthesisVoice(language: identifier) 

    synth.speak(utterance) 

}