2017-02-12 19 views
0

私は基本的に私が構築しているアプリケーションに音声認識を組み込もうとしています。マイクボタンを押して音を鳴らし、録音を開始して音声を認識できるようにしたい。問題は、ボタンを押すと音が鳴らないことです。また、実際のiPhoneでこのアプリを実行すると、コントロールパネルのサウンドスライダーが消えます。誰でも助けることができますか?ここでスウィフト3の音声は再生されません

は私のコードです:

class VoiceViewController: UIViewController, SFSpeechRecognizerDelegate, UITextViewDelegate { 

private let speechRecognizer = SFSpeechRecognizer(locale: Locale(identifier: "en-US"))! 
private var speechRecognitionRequest: SFSpeechAudioBufferRecognitionRequest? 
private var speechRecognitionTask: SFSpeechRecognitionTask? 
private let audioEngine = AVAudioEngine() 

var audioPlayer: AVAudioPlayer = AVAudioPlayer() 
var url: URL? 
var recording: Bool = false 

let myTextView = UITextView() 

func startSession() throws { 

    if let recognitionTask = speechRecognitionTask { 
     recognitionTask.cancel() 
     self.speechRecognitionTask = nil 
    } 

    let audioSession = AVAudioSession.sharedInstance() 
    try audioSession.setCategory(AVAudioSessionCategoryRecord) 

    speechRecognitionRequest = SFSpeechAudioBufferRecognitionRequest() 

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

    speechRecognitionRequest?.shouldReportPartialResults = true 

    speechRecognitionTask = speechRecognizer.recognitionTask(with: speechRecognitionRequest!) { result, error in 

     var finished = false 

     if let result = result { 
      print(result.bestTranscription.formattedString) 
      finished = result.isFinal 
     } 

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

      self.speechRecognitionRequest = nil 
      self.speechRecognitionTask = nil 
     } 
    } 

    let recordingFormat = inputNode.outputFormat(forBus: 0) 
    inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer: AVAudioPCMBuffer, when: AVAudioTime) in 

     self.speechRecognitionRequest?.append(buffer) 
    } 

    audioEngine.prepare() 
    try audioEngine.start() 
} 

func stopTranscribing() { 
    if audioEngine.isRunning { 
     audioEngine.stop() 
     speechRecognitionRequest?.endAudio() 
    } 
} 

func btn_pressed() { 
    print("pressed") 

    if recording { 
     url = URL(fileURLWithPath: Bundle.main.path(forResource: "tweet", ofType: "mp3")!) 
    } else { 
     url = URL(fileURLWithPath: Bundle.main.path(forResource: "gesture", ofType: "mp3")!) 
    } 

    do { 
     try(audioPlayer = AVAudioPlayer(contentsOf: url!)) 
    } catch let err { 
     print(err) 
    } 
    audioPlayer.play() 
    recording = (recording == false) 
    if recording == false { 
     stopTranscribing() 
    } else { 
     try! startSession() 
    } 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    let button = UIButton() 
    button.setTitle("push me", for: UIControlState()) 
    button.frame = CGRect(x: 10, y: 30, width: 80, height: 30) 
    button.addTarget(self, action: #selector(btn_pressed), for: .touchUpInside) 
    self.view.addSubview(button) 

    myTextView.frame = CGRect(x: 60, y: 100, width: 300, height: 200) 
    self.view.addSubview(myTextView) 


    // Do any additional setup after loading the view. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 


/* 
// MARK: - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before navigation 
override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) { 
    // Get the new view controller using segue.destinationViewController. 
    // Pass the selected object to the new view controller. 
} 
*/ 

} 

答えて

1

ドキュメントは言う:

AVAudioSessionCategoryRecordが オーディオを記録するためのカテゴリ。このカテゴリは再生オーディオを消音します。

これは、使用している唯一のオーディオセッションのカテゴリであり、AVAudioPlayerの再生を開始するとすぐに設定されるため、自然に何も聞こえません。あなたは、あなたのオーディオセッションで軽快で正しいことをもっと考える必要があります。サウンドを再生して録音を開始する場合は、再生カテゴリを使用してサウンドを再生し、AVAudioPlayerDelegateを介してサウンドが終了するまで録音を開始しないでください。

+0

2つの別々のオーディオセッションカテゴリを使用できますか?オーディオが再生されているときに録音できるようにします。 –

+0

それはAVAudioSessionCategoryPlayAndRecordの目的ではありませんか?しかし、あなたのユースケースではそれは悪い考えかもしれないと思います。余分な音を鳴らしている間、コンピュータはどのようにして音声を認識できますか? – matt

+0

ただ素早く1秒の音を再生しようとしています –

関連する問題