AVSpeechSynthesizer发音音频文件 [英] AVSpeechSynthesizer utterance to audio file

查看:178
本文介绍了AVSpeechSynthesizer发音音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用AVSpeechSynthesizer,我希望将语音文本保存到音频文件或AVAsset。我浏览了Apple的文档并没有看到任何内容,但我发现了一个问题以确定。以下是我目前的代码。

I'm using AVSpeechSynthesizer in my app and I'm looking to save the spoken text to an audio file or AVAsset. I went through Apple's docs and didn't see anything but figured I'd post a question to make sure. Below is my current code.

AVSpeechUtterance * utterance = [AVSpeechUtterance speechUtteranceWithString:textView.text];
float rate = [speedSlider value]/1.5;
utterance.rate =  rate;
[speechSynthesizer speakUtterance:utterance];


推荐答案

可以录制生成的音频您的应用(不是来自其他应用)。虽然 AVSpeech 不提供用于保存生成的音频的API,但Apple还有其他API可以完成这项工作。解决方案可能不像你想的那样干净,但它应该可以工作。

It is possible to record audio generated by your app (not from other apps). Although AVSpeech does not provide an API to save the generated audio, Apple has other APIs that can do the job. The solution is probably not as clean as you would like, but it should work.

Apple提供了一个名为音频单元框架,用于管理高级音频处理和录制。这是iOS SDK(据我所知)中唯一可以同时播放和录制音频的框架。 音频单元托管指南看起来很有希望,音频混音器示例应用程序

Apple provides a framework called the Audio Unit Framework to manage advanced audio processing and recording. This is the only Framework in the iOS SDK (to my knowledge) that can play and record audio simultaneously. The Audio Unit Hosting Guide looks promising, and so does the Audio Mixer Sample App.

注意:我没有尝试将音频单元框架与AVSpeechSynthesizer一起使用(它可能也可能不起作用)。但是,考虑到AVSpeechSynthesizer与CoreAudio的搭配很好,那么它很可能适用于AudioUnits。

Note: I have not tried using the Audio Unit Framework with AVSpeechSynthesizer (it may or may not work). However, considering AVSpeechSynthesizer plays nice with CoreAudio then it is more than likely it will work with AudioUnits.

如果上述解决方案不起作用,然后一个简单的解决方法可以做到这一点。 AVSpeechSynthesizer 不需要任何网络连接即可正常运行,因此在许多情况下,您可能不需要 来保存音频。相反,您可以使用NSFileManager保存文本以供日后使用:

If the above solution does not work, then a simple workaround may do the trick. AVSpeechSynthesizer does not require any network connection to properly function, so in many cases you may not need to save the audio. Instead you could save the text for later using NSFileManager:

NSString *textToSynthesize = @"Just what do you think you are doing, Dave?";

NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths firstObject];

[textToSynthesize writeToFile:[documentsDirectory stringByAppendingPathComponent:@"synthText.txt"] atomically:YES encoding:NSUTF8StringEncoding error:&error];

当您准备合成文本时,只需从文件中读取文本并将其重新插入 AVSpeechSynthesizer 。我确实意识到这个解决方案在所有情况下都不起作用或适用(例如,如果你需要将音频文件发送给某人)。

When you are ready to synthesize the text, just read it from the file and plug it back into the AVSpeechSynthesizer. I do realize that this solution will not work or apply in all cases (e.g. if you need to send the audio file to someone).

这些只是解决该问题的几种可能的解决方案,这两种解决方案都是解决方法,根据您的具体情况,可能会也可能不会起作用。因人而异。祝你好运!

Those are just a few possible solutions for the issue, both of which are workarounds and may or may not work depending on your specific scenario. YMMV. Good luck!

这篇关于AVSpeechSynthesizer发音音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆