我正在开发利用语音转iOS文本功能的应用程序。
我从苹果那里找到了非常不错的示例应用程序:
https://developer.apple.com/library/content/samplecode/SpeakToMe/Introduction/Intro.html
该应用程序正在使用实时:SFSpeechAudioBufferRecognitionRequest函数,并自动将语音转录为文本。
另外,我发现了许多将音频文件转录为文本的教程。
但是我需要一些不同的东西,我需要实时文本转语音功能(就像上面的示例应用程序一样),并且我需要保存音频文件。那可能吗?
最佳答案
您可以实现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)
}