求助!学习TTS,保存出错
aide吧
全部回复
仅看楼主
level 9
EIIE♬ 楼主
是什么问题?
朗读可以,保存就出错。
public class MainActivity extends Activity {
private EditText mEditText = null;
private Button readButton = null;
private Button saveButton = null;
private CheckBox mCheckBox = null;
private TextToSpeech mTextToSpeech=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mEditText = (EditText)this.findViewById(R.id.edittext);
readButton = (Button)this.findViewById(R.id.rbutton);
saveButton = (Button)this.findViewById(R.id.sbutton);
mCheckBox = (CheckBox)this.findViewById(R.id.checkbox);
//实例并初始化TTS对象
mTextToSpeech=new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status==TextToSpeech.SUCCESS) {
//设置朗读语言
int supported=mTextToSpeech.setLanguage(Locale.US);
if ((supported!=TextToSpeech.LANG_AVAILABLE)&&(supported!=TextToSpeech.LANG_COUNTRY_AVAILABLE)) {
Toast.makeText(MainActivity.this, "不支持当前语言!", 1).show();
}
}
}
});
//朗读监听按钮
readButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//朗读EditText里的内容
mTextToSpeech.speak(mEditText.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
}
});
//保存按钮监听
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//将EditText里的内容保存为语音文件
int r = mTextToSpeech.synthesizeToFile(mEditText.getText().toString(), null, "/mnt/sdcard/speak.wav");
if (r==TextToSpeech.SUCCESS) {
Toast.makeText(MainActivity.this, "保存成功!", 1).show();
}
}
});
//EditText内容变化监听
mEditText.addTextChangedListener(mTextWatcher);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mTextToSpeech!=null) {
mTextToSpeech.shutdown();//关闭TTS
}
}
private TextWatcher mTextWatcher = new TextWatcher(){
@Override
public void afterTextChanged(Editable s) {
//如果是边写边读
if(mCheckBox.isChecked()&&(s.length()!=0)){
//获得EditText的所有内容
String t = s.toString();
mTextToSpeech.speak(t.substring(s.length()-1), TextToSpeech.QUEUE_FLUSH, null);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
};
}
2016年03月25日 13点03分 1
level 9
EIIE♬ 楼主
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.HashMap.get(java.lang.Object)' on a null object reference
at android.speech.tts.TextToSpeech.synthesizeToFile(TextToSpeech.java:1868)
at me.eiie.atts.MainActivity$100000002$0$debug.onClick(MainActivity.java:56)
at me.eiie.atts.MainActivity$100000002.onClick(MainActivity.java)
at android.view.View.performClick(View.java:5207)
at android.view.View$PerformClick.run(View.java:21177)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5432)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:735)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
2016年03月25日 13点03分 2
看了api public intsynthesizeToFile(Stringtext,HashMap<String,String> params,Stringfilename) new一个hash就好,但是网络的为什么都是null。不过都是旧教程…
2016年05月06日 13点05分
level 9
EIIE♬ 楼主
怎么看log?
2016年03月25日 13点03分 3
level 11
空指针异常,检查有没有实例化对象
2016年03月28日 09点03分 5
是的,搜索了很多,保存都是那句,但就是保存不了,是什么问题?难道是TTS库的问题?现在用的是小米,讯飞的TTS
2016年04月03日 02点04分
@EIIE♬ 仔细检查,肯定有
2016年04月04日 04点04分
level 9
保存
是读取的文字保存声音吗?
2016年03月29日 07点03分 7
我怎么记得好像是一句代码的是呢?
2016年03月29日 07点03分
@看透时光流星 好像在疯狂安卓讲义
2016年03月29日 07点03分
是的,搜索了很多,保存都是那句,但就是保存不了,是什么问题?难道是TTS库的问题?现在用的是小米,讯飞的TTS
2016年04月03日 02点04分
tts.synthesizeToFile(editText.getText().toString(), null, "/mnt/sdcard/sound.wav"); Toast.makeText(Speech.this, "声音记录成功!", 50000).s
2016年04月04日 06点04分
level 12
用百度语音吧
2016年04月09日 00点04分 9
1