热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

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

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

public class MainActivity extends Activity {
Button store, play;
EditText input;
String speakTextTxt;
TextToSpeech mTts;
HashMap myHashRender = new HashMap();
String tempDestFile ;
@Override
public 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 myHashRender = new HashMap();
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 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 myHashRender = new HashMap();
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.


推荐阅读
author-avatar
2702934635_941
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有