首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift -语音转文本并保存音频文件

问Swift -语音转文本并保存音频文件
EN

Stack Overflow用户
提问于 2017-11-05 04:24:55
回答 1查看 2.1K关注 0票数 0

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

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

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

EN

回答 1

Stack Overflow用户

发布于 2017-11-05 05:20:45

您可以实现Saving Recorded Audio (Swift)中描述的技术。基本上,像这样设置一个AVAudioRecorder,然后将它与您返回的音频输出连接起来,以保存剪辑。

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47115271

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档