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

痴心易碎 提交于 2019-12-14 01:09:04

问题


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)

回答1:


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 to 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 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.



来源:https://stackoverflow.com/questions/47329826/swift-how-to-convert-text-to-speech-generated-audio-to-mp3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!