2017-03-13 13 views
0

내 TTS (텍스트를 음성으로) 해제 (음성 텍스트로),하지만 난에 붙어 그것.시리 키트 내가 시리 키트</strong>을 텍스트로 <strong>음성과 함께 음성</strong> (AVSpeechSynthesizer)에 <strong>텍스트를 실행하기 위해 노력하고있어 아이폰 OS

내 TTS가 더 이상 작동하지 않으면 STT를 실행하는 코드를 실행할 때까지 내 TTS가 완벽하게 작동합니다. 코드를 디버깅하고 코드를 실행하는 동안 오류는 발생하지 않지만 내 텍스트는 음성으로 변환되지 않습니다. 어떻게 든 내 STT는 출력 마이크를 사용할 수 없게되는 것 같아 TTS가 텍스트를 더 이상 음성으로 변환하지 않는 이유가 바로 그 것입니다. Ops : TTS가 작동을 멈췄지만 STT는 완벽하게 작동합니다.

팁이 있습니까? 당신의 audiosession가 기록 모드에 있기 때문에, 당신은 먼저 시도의 audioSession.setCategory를 설정하는 것,이 개 솔루션이

@IBOutlet weak var microphoneButton: UIButton!

//text to speech 
let speechSynthesizer = AVSpeechSynthesizer() 

//speech to text 
private var speechRecognizer: SFSpeechRecognizer! 

private var recognitionRequest: SFSpeechAudioBufferRecognitionRequest? 
private var recognitionTask: SFSpeechRecognitionTask? 
private var audioEngine = AVAudioEngine() 

@IBAction func textToSpeech(_ sender: Any) { 

    if let word = wordTextField.text{ 

     if !speechSynthesizer.isSpeaking { 


      //get current dictionary 
      let dictionary = fetchSelectedDictionary() 

      //get current language 
      let language = languagesWithCodes[(dictionary?.language)!] 

      let speechUtterance = AVSpeechUtterance(string: word) 
       speechUtterance.voice = AVSpeechSynthesisVoice(language: language) 
       speechUtterance.rate = 0.4 
      //speechUtterance.pitchMultiplier = pitch 
      //speechUtterance.volume = volume 
       speechSynthesizer.speak(speechUtterance) 

     } 
     else{ 
      speechSynthesizer.continueSpeaking() 
     } 

    } 
} 

@IBAction func speechToText(_ sender: Any) { 

    if audioEngine.isRunning { 
     audioEngine.stop() 
     recognitionRequest?.endAudio() 
     microphoneButton.isEnabled = false 
     microphoneButton.setTitle("Start Recording", for: .normal) 
    } else { 
     startRecording() 
     microphoneButton.setTitle("Stop Recording", for: .normal) 
    } 

} 

func startRecording() { 

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

    let audioSession = AVAudioSession.sharedInstance() 
    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() 

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

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

    recognitionRequest.shouldReportPartialResults = true 

    recognitionTask = speechRecognizer?.recognitionTask(with: recognitionRequest, resultHandler: { (result, error) in 

     var isFinal = false 

     if result != nil { 

      self.wordTextField.text = result?.bestTranscription.formattedString 
      isFinal = (result?.isFinal)! 
     } 

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

      self.recognitionRequest = nil 
      self.recognitionTask = nil 

      self.microphoneButton.isEnabled = true 
     } 
    }) 

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

    audioEngine.prepare() 

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

    wordTextField.text = "Say something, I'm listening!" 
} 

} 아마

답변

1

:

여기 내의 ViewController의 코드입니다 (AVAudioSessionCategoryRecord)에서 AVAudioSessionCategoryPlayAndRecord로 복사 (이 작업은 가능)하지만 더 명확한 방법은 무언가를 말하고 AVAudioSessionCategory를 AVAudioSessionCategoryP로 설정하는 별도의 함수를 얻는 것입니다. layback

희망이 도움이되었습니다.

+0

형제, 방금 내 생명을 구했어! 정말 고마워요, 매력처럼 일 했어요! –

+0

형님, 그냥 다른 것 .. 내 아이폰 스피커를 일반 음량으로 되돌리려면 어떻게해야합니까? 내 텍스트를 연설하기 전에 내 iPhone의 음량이 높았지만 코드를 실행하고 나에게 보여준 것처럼 문제를 해결 한 후에 내 텍스트의 음성 볼륨이 매우 낮습니다. –

+0

넵이 할 일을하려고 나를 :) 도와 정말 감사합니다 { 시도의 AVAudioSession.sharedInstance(). overrideOutputAudioPort (AVAudioSessionPortOverride.speaker) } 캐치 _ { } –

0

이 줄 :

try audioSession.setMode(AVAudioSessionModeMeasurement) 

아마 이유입니다. 볼륨이 너무 낮게 조절되어 꺼진 것처럼 들릴 수 있습니다. 시도하십시오 :

try audioSession.setMode(AVAudioSessionModeDefault) 

및 작동하는지 확인하십시오.