2017-11-04 30 views
0

iOSの音声テキスト機能を利用するアプリを開発中です。 私はリンゴから本当に素敵なサンプルアプリを見つけました: https://developer.apple.com/library/content/samplecode/SpeakToMe/Introduction/Intro.htmlSwift - 音声をテキストに変換して音声ファイルを保存する

このアプリは、ライブ使用している:SFSpeechAudioBufferRecognitionRequest機能を、自動的に音声をテキストに転写します。 また、オーディオファイルをテキストに転記するチュートリアルも多数見つかりました。

しかし私は別の何かが必要です、私は(上記のサンプルアプリケーションと同じように)音声機能にライブテキストをしたい、私はオーディオファイルを保存する必要があります。それは可能ですか?

答えて

0

Saving Recorded Audio (Swift)に記載されている手法を実装できます。基本的にはAVAudioRecorderをこのように設定して、返されたオーディオ出力に接続してクリップを保存します。

func setupRecorder() { 


    let recordSettings : [String : AnyObject] = 
    [ 
     AVFormatIDKey: NSNumber(unsignedInt: kAudioFormatMPEG4AAC), 
     AVEncoderAudioQualityKey : AVAudioQuality.Max.rawValue as NSNumber, 
     AVEncoderBitRateKey : 320000 as NSNumber, 
     AVNumberOfChannelsKey: 2 as NSNumber, 
     AVSampleRateKey : 44100.0 as NSNumber 
    ] 

    do { 

     try soundRecorder = AVAudioRecorder(URL: getFileURL(), settings: recordSettings) 
      soundRecorder.delegate = self 
      soundRecorder.prepareToRecord() 

    } catch { 

     print(error) 
    } 


} 

func getCacheDirectory() -> String { 

    let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) 

    return paths[0] 

} 

func getFileURL() -> NSURL{ 

    let path = getCacheDirectory().stringByAppendingPathComponent(fileName) 

    let filePath = NSURL(fileURLWithPath: path) 

    return filePath 
} 

func audioRecorderDidFinishRecording(recorder: AVAudioRecorder, successfully flag: Bool) { 
    PlayButton.enabled = true 
    recordedAudio = RecordedAudio() 
    recordedAudio.filePathUrl = recorder.url 
    recordedAudio.title = recorder.url.lastPathComponent 
    print(recordedAudio.title) 
} 
+0

このファイルでそれをフックアップする方法を提案することができます:https://developer.apple.com/library/content/samplecode/SpeakToMe/Listings/SpeakToMe_ViewController_swift.html#//apple_ref/doc/uid/ TP40017110-SpeakToMe_ViewController_swift-DontLinkElementID_6 – KirillC

関連する問題