【发布时间】:2013-01-18 23:09:19
【问题描述】:
我正在使用以下代码将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 ;
@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);
}
}
}
【问题讨论】:
-
@ChandraSekhar File appTmpPath = new File(exStoragePath + "/sekhar/"); ... tempDestFile = appTmpPath.getAbsolutePath() + "/"+ tempFilename;你的文件名里不是有两个'//'吗?
-
@sinisha,我错误地添加了多次。现在我删除并尝试了。我从子类中的 onInIt 方法得到了祝酒词。现在我如何检查它是否已保存。我将我的设备连接到我的机器并检查了 sekhar 文件夹。但是没有wav文件。你能建议我吗?我打印了“i”的值为 0。
-
MySpeachdid 实例说出了什么文本?? -
@Houcine,它应该说出我在 EditText 中输入的内容
-
@ChandraSekhar :但我在上面的代码中看不到任何指令告诉您的实例说出来自
EditText的文本
标签: android text-to-speech android-mediaplayer