android– 如何将文本到语音输出存储为WAV文件?

android– 如何将文本到语音输出存储为WAV文件?,第1张

概述我使用以下代码将TexttoSpeech输出存储为我的应用程序中的wav文件.我不确定错误在哪里,你能看一下并建议我吗?publicclassMainActivityextendsActivity{Buttonstore,play;EditTextinput;StringspeakTextTxt;TextToSpeechmTts;HashMap<String,String>myHashRen

我使用以下代码将Text to Speech输出存储为我的应用程序中的wav文件.我不确定错误在哪里,你能看一下并建议我吗?

public class MainActivity extends Activity {button store, play;EditText input;String speakTextTxt;TextToSpeech mTts;HashMap<String, String> myHashRender = new HashMap<String, String>();String tempDestfile ;@OverrIDepublic voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    store = (button) findVIEwByID(R.ID.button1);    play = (button) findVIEwByID(R.ID.button2);    input = (EditText) findVIEwByID(R.ID.editText1);    store.setonClickListener(new OnClickListener()  {        public voID onClick(VIEw v) {            speakTextTxt = "Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world";            HashMap<String, String> myHashRender = new HashMap<String, String>();            myHashRender.put(TextToSpeech.Engine.KEY_ParaM_UTteraNCE_ID, speakTextTxt);            String exStoragePath = Environment.getExternalStorageDirectory().getabsolutePath();            file appTmpPath = new file(exStoragePath + "/sounds/");            appTmpPath.mkdirs();            String tempfilename = "hello.mp3";            tempDestfile = appTmpPath.getabsolutePath() + "/" + tempfilename;            new MySpeech(speakTextTxt);        }    });}class MySpeech implements OnInitListener{            String tts;    public MySpeech(String tts)    {        this.tts = tts;        mTts = new TextToSpeech(MainActivity.this, this);    }    @OverrIDe    public voID onInit(int status)     {        Log.v("log", "initi");        int i = mTts.synthesizetofile(speakTextTxt, myHashRender, tempDestfile);        if(i == TextToSpeech.SUCCESS)        {          Toast toast = Toast.makeText(MainActivity.this, "Saved "+i,                Toast.LENGTH_SHORT);          toast.show();           }        System.out.println("Result : " + i);    }  } }

解决方法:

在这篇post :中提到Ted Hopp的答案

重要的方法是synthesizeToFile.它会将音频写入您指定的设备上的文件.然后,您可以使用MediaPlayer播放该文件,或者使用adb命令行工具使用命令将其从设备上移到开发系统上

编辑:

尝试此代码,然后检查路径sdcard /它是否包含文件:test.wav

HashMap<String, String> myHashRender = new HashMap();String textToConvert = "this is a demo for saving a WAV file";String destinationfilename = "/sdcard/test.wav";myHashRender.put(TextToSpeech.Engine.KEY_ParaM_UTteraNCE_ID, textToConvert);mTts.synthesizetofile(textToConvert, myHashRender, destinationfilename);

编辑2:

如果你试图将波形文件保存到内部存储器(而不是/ sdcard /文件夹),那么实现这一目标的唯一方法是创建一个可写的世界
内部存储器中的目录如下:

context.getDir("soundfiles", Context.MODE_WORLD_WRITEABLE);

然后写信给这个目录.

编辑3:

在查看代码之后,您只需要创建目录和文件的一些问题:代码应该是这样的:

speakTextTxt = "Hello world";HashMap<String, String> myHashRender = new HashMap<String, String>();myHashRender.put(TextToSpeech.Engine.KEY_ParaM_UTteraNCE_ID, speakTextTxt);String exStoragePath = Environment.getExternalStorageDirectory().getabsolutePath();Log.d("MainActivity", "exStoragePath : "+exStoragePath);file appTmpPath = new file(exStoragePath + "/sounds/");boolean isDirectoryCreated = appTmpPath.mkdirs();Log.d("MainActivity", "directory "+appTmpPath+" is created : "+isDirectoryCreated);String tempfilename = "tmpaudio.wav";tempDestfile = appTmpPath.getabsolutePath() + file.separator + tempfilename;Log.d("MainActivity", "tempDestfile : "+tempDestfile);new MySpeech(speakTextTxt);

我已经在AndroID模拟器上测试了它,它工作正常,但你需要使用设备管理器指定模拟器的sdCard的大小,编辑eumlator,并指定你的SD卡的大小:例如512 Mb.然后你会在路径中找到wav文件:mnt / sdcard / sounds / tmpaudio.wav
要测试它,只需打开DDMS透视图,文件资源管理器,然后将文件导出到您的PC.

总结

以上是内存溢出为你收集整理的android – 如何将文本语音输出存储为WAV文件?全部内容,希望文章能够帮你解决android – 如何将文本到语音输出存储为WAV文件?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1115995.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存