Swift:如何将文本到语音生成的音频转换为.mp3? [英] Swift: How to Convert Text-to-Speech Generated Audio to .mp3?

查看:303
本文介绍了Swift:如何将文本到语音生成的音频转换为.mp3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用AVSpeechSynthesizer将文本转换为语音.单击播放"时,将播放转换后的语音.我不知道此音频文件的扩展名.
我的目标是将此创建的音频文件转换为.mp3.我已经在Google中搜索过,但没有找到任何东西.任何帮助表示赞赏.

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)

推荐答案

一种简单的策略是先将输出从AVSpeechSynthesizer保存到目录,然后更改其格式.
但是,据我所知AVSpeechSynthesizer不允许您保存输出. 我建议您检查以下帖子:将音频流保存到mp3文件(iOS )
引用 IanStallings :

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:

不幸的是,没有可用的公共API来捕获扬声器的输出,并查看AVSpeechSynthesizer和相关类的文档,但我找不到从中捕获任何音频的方法.您可能想看看第三方库来帮助解决这个问题.

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.

还有另一个: AVSpeechSynthesizer输出为文件吗?
同样,引用 Bh​​umit Mehta :

到目前为止,AVSpeechSynthesizer不支持此功能.绝对无法使用AVSpeechSynthesizer获得音频文件.几周前,我为自己的一个应用程序进行了尝试,结果发现这是不可能的,iOS 8中的AVSpeechSynthesizer也没有任何改变.

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.

这篇关于Swift:如何将文本到语音生成的音频转换为.mp3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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