Swift - 语音到文本并保存音频文件

时间:2017-11-04 20:24:55

标签: ios iphone swift xcode swift4

我正在开发利用iOS语音功能的应用程序。 我从苹果发现了非常好的示例应用: https://developer.apple.com/library/content/samplecode/SpeakToMe/Introduction/Intro.html

此应用正在使用live:SFSpeechAudioBufferRecognitionRequest函数,并自动将语音转录为文本。 另外,我发现了许多将音频文件转录为文本的教程。

但我需要一些不同的东西,我想要实时文本到语音功能(就像上面的示例应用程序一样),我需要保存音频文件。那可能吗?

1 个答案:

答案 0 :(得分: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)
}