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];
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 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.
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.
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];
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With