Kevin Baldha
Kevin Baldha

Reputation: 87

Swift: How to Convert Text-to-Speech Generated Audio to .mp3?

I have converted text to speech using the AVSpeechSynthesizer. When clicked on Play, converted speech plays. I don't know the extension of this audio file.
My goal is to convert this created audio file to .mp3. I have searched in Google but I didn't find anything. Any help is appreciated.

My code is here: Convert text to speech

text = UserDefaults.standard.string(forKey: "TextString")!
voice = selectCountryDic["Clanguage"]!
let speechText = AVSpeechUtterance(string:text)
speechText.voice = AVSpeechSynthesisVoice(language:voice)

let synthesizer = AVSpeechSynthesizer()
synthesizer.speak(speechText)

Upvotes: 6

Views: 5737

Answers (1)

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.

Upvotes: 5

Related Questions