如何存储文本到语音输出为WAV文件? [英] How to store text to speech output as WAV file?
问题描述
我使用下面的code存储文本到语音
为 WAV 文件在我的应用程序的输出。我不知道哪里出了错误,可以请你看看成,并建议我?
公共类MainActivity延伸活动{
按钮存储,播放;
的EditText输入;
字符串speakTextTxt;
TextToSpeech MTTS;
HashMap的<字符串,字符串> myHashRender =新的HashMap<字符串,字符串>();
字符串tempDestFile;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
店内=(按钮)findViewById(R.id.button1);
玩=(按钮)findViewById(R.id.button2);
输入=(EditText上)findViewById(R.id.editText1);
store.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
speakTextTxt =世界,你好你好世界世界,你好你好世界世界,你好你好世界世界,你好你好世界世界,你好世界,你好世界,你好;
HashMap的<字符串,字符串> myHashRender =新的HashMap<字符串,字符串>();
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,speakTextTxt);
字符串exStoragePath = Environment.getExternalStorageDirectory()getAbsolutePath()。
文件appTmpPath =新的文件(exStoragePath +/声音/);
appTmpPath.mkdirs();
字符串tempFilename =hello.mp3;
tempDestFile = appTmpPath.getAbsolutePath()+/+ tempFilename;
新MySpeech(speakTextTxt);
}
});
}
类MySpeech实现OnInitListener
{
字符串TTS;
公共MySpeech(字符串TTS)
{
this.tts = TTS;
MTTS =新TextToSpeech(MainActivity.this,这一点);
}
@覆盖
公共无效的OnInit(INT状态)
{
Log.v(日志,initi);
INT I = mTts.synthesizeToFile(speakTextTxt,myHashRender,tempDestFile);
如果(我== TextToSpeech.SUCCESS)
{
吐司面包= Toast.makeText(MainActivity.this,拯救+ I,
Toast.LENGTH_SHORT);
toast.show();
}
的System.out.println(结果:+ I);
}
}
}
指的是 泰德·霍普的在此<一个答案href="http://stackoverflow.com/questions/9713594/how-can-i-save-my-android-tts-output-in-a-wav-file">post :
最重要的方法是<一个href="http://developer.android.com/reference/android/speech/tts/TextToSpeech.html#synthesizeToFile%28java.lang.String,%20java.util.HashMap%3Cjava.lang.String,%20java.lang.String%3E,%20java.lang.String%29"相对=nofollow> synthesizeToFile 。它会写音频到您指定的设备上的文件。然后,您可以播放该文件用MediaPlayer的,也可以用ADB命令行工具,使用该命令关闭设备拉出到您的开发系统
编辑:
试试这个code,然后检查路径 SD卡/
如果它包含文件: test.wav
的HashMap&LT;字符串,字符串&GT; myHashRender =新的HashMap();
字符串textToConvert =这是一个演示用于保存WAV文件;
字符串目的地文件=/sdcard/test.wav;
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,textToConvert);
mTts.synthesizeToFile(textToConvert,myHashRender,目的地文件);
编辑2:
如果你试图波形文件保存到内存中(而不是 / SD卡/
文件夹),那么实现这一目标的唯一方法是创建一个可写
在内部存储器这样的目录:
context.getDir(soundfiles,Context.MODE_WORLD_WRITEABLE);
然后写入该目录。
修改3:
考虑看看到code之后,你刚刚的一些问题与创建目录和文件:在code应该是这样的:
speakTextTxt =世界,你好;
HashMap的&LT;字符串,字符串&GT; myHashRender =新的HashMap&LT;字符串,字符串&GT;();
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,speakTextTxt);
字符串exStoragePath = Environment.getExternalStorageDirectory()getAbsolutePath()。
Log.d(MainActivity,exStoragePath:+ exStoragePath);
文件appTmpPath =新的文件(exStoragePath +/声音/);
布尔isDirectoryCreated = appTmpPath.mkdirs();
Log.d(MainActivity,目录+ appTmpPath +创建+ isDirectoryCreated);
字符串tempFilename =tmpaudio.wav;
tempDestFile = appTmpPath.getAbsolutePath()+文件分割符+ tempFilename;
Log.d(MainActivity,tempDestFile:+ tempDestFile);
新MySpeech(speakTextTxt);
我已经在Android模拟器测试它,它工作得很好,但你需要指定使用设备管理器,编辑eumlator你的模拟器轮空的SD卡的大小,并指定你的SD卡的大小:例如512 MB。然后你会发现在该路径的wav文件: MNT / SD卡/声音/ tmpaudio.wav
测试它,只需打开 DDMS透视图
,文件浏览器
,然后将文件导出到您的计算机。
I am using the following code to store Text to Speech
output as wav file in my application. I am not sure where is the error, can you please take a look into that and suggest me?
public class MainActivity extends Activity {
Button store, play;
EditText input;
String speakTextTxt;
TextToSpeech mTts;
HashMap<String, String> myHashRender = new HashMap<String, String>();
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<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);
}
}
}
referring to the answer of Ted Hopp in this post :
The important method is synthesizeToFile. It will write the audio to a file on the device that you specify. You can then play that file with a MediaPlayer or you can pull it off the device onto your development system with the adb command-line tool using the command
EDIT :
try this code , and then check the path sdcard/
if it contains the file : 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);
EDIT 2 :
and if you are trying to save the wave file to the internal memory ( not the /sdcard/
folder) , then the only way to achieve this is to create a world writable
directory in the internal memory like this:
context.getDir("soundfiles", Context.MODE_WORLD_WRITEABLE);
And then write to this dir.
EDIT 3 :
after taking a look into your code , you've just some problems with creating directories and files : the code should be like this :
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);
i've tested it on android emulator , and it works fine , but you need to specify the size of the sdCard of your emulator bye using device manager, edit eumlator, and specify the size of your sdcard : example 512 Mb. and then you will find the wav file in the path : mnt/sdcard/sounds/tmpaudio.wav
to test it , just open the DDMS perspective
, File Explorer
, and then export the file to your PC .
这篇关于如何存储文本到语音输出为WAV文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!