AVSpeechSynthesizer output as file?
As of now AVSpeechSynthesizer
does not support this . There in no way get the audio file using AVSpeechSynthesizer
. I tried this few weeks ago for one of my apps and found out that it is not possible , Also nothing has changed for AVSpeechSynthesizer
in iOS 8.
I too thought of recording the sound as it is being played , but there are so many flaws with that approach like user might be using headphones, the system sound might be low or mute , it might catch other external sound, so its not advisable to go with that approach.
How to store a text-to-speech audio in an audio file (IOS)
The error simply shows An AVSpeechUtterance shall not be enqueued twice
.
So stop making it speak and write in the same time .
I used your code and comment out synthesizer.SpeakUtterance(su);
, error gone .
Update
Based on my test , it does not allow to create extra subfolder , so remove the TTS/
part , just leave the file name alone .
string filePath = Path.Combine(Path.GetTempPath(), 1 + ".caf");
OBJC AVSpeechUtterance writeUtterance how?
Referring to the potential answer in Swift, this would be the Objective-C implementation
AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc] init];
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:@"test 123"];
AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
[utterance setVoice:voice];
__block AVAudioFile *output = nil;
[synthesizer writeUtterance:utterance
toBufferCallback:^(AVAudioBuffer * _Nonnull buffer) {
AVAudioPCMBuffer *pcmBuffer = (AVAudioPCMBuffer*)buffer;
if (!pcmBuffer) {
NSLog(@"Error");
return;
}
if (pcmBuffer.frameLength != 0) {
//append buffer to file
if (output == nil) {
output = [[AVAudioFile alloc] initForWriting:[NSURL fileURLWithPath:@"test.caf"]
settings:pcmBuffer.format.settings
commonFormat:AVAudioPCMFormatInt16
interleaved:NO error:nil];
}
[output writeFromBuffer:pcmBuffer error:nil];
}
}];
Swift: How to Convert Text-to-Speech Generated Audio to .mp3?
2021-02-20 Update:
This is possible now. Please see the thread: AVSpeechSynthesizer output as file?
2017 Answer (outdated):
A simple strategy would be saving your output from AVSpeechSynthesizer
to a directory first, then changing its format.
But, as far as I know, AVSpeechSynthesizer
does not let you save your outputs.
I would suggest you check the following post: Save audio stream to mp3 file (iOS)
Quoting from IanStallings:
Unfortunately no, there is no public API available to capture the speaker output, and looking over the docs for AVSpeechSynthesizer and related classes I don't see a way to capture any audio from it. You may want to look at 3rd party libraries to help with this.
And another one: AVSpeechSynthesizer output as file?
Again, quoting from Bhumit Mehta:
As of now AVSpeechSynthesizer does not support this. There is no way to get the audio file using AVSpeechSynthesizer. I tried this a few weeks ago for one of my apps and found out that it is not possible, Also nothing has changed for AVSpeechSynthesizer in iOS 8.
Related Topics
Could Not Cast Value of Type 'Uitableviewcell' to '(Appname).(Customcellname)'
Calculate All Permutations of a String in Swift
Can't Submit Apps to Appstore: Error Itms-90534: "Invalid Toolchain
How to Get Memory Usage in Swift
Is There a Simple Way to Edit/Modify a Uilocalnotification
How to Edit Uialertaction Title Font Size and Style
Memory Leaks in the Swift Playground/Deinit{} Not Called Consistently
iOS Web Page Errors Over Cellular Data But Not Over Wifi? Recent Change to At&T Cellular Network
Build an iOS App Without Owning a MAC
How to Copy Text to Clipboard/Pasteboard with Swift
Constant Movement in Spritekit
Differencebetween Int and Int32 in Swift
How to Properly Group a List Fetched from Coredata by Date
Flutter on iOS: Fatal Error: Module 'Cloud_Firestore' Not Found
Increment Uislider by 1 in Range 1 to 100
Uicolor Colorwithred:Green:Blue:Alpha: Always Returns White Unless One Argument Is 0