我正在为我的 android 应用程序使用 CMU Flite TTS 引擎。我已经使用引擎名称初始化了 android TTS 对象,并且还在 onDestroy 中调用了它的 shupdown 方法,但我仍然收到服务连接泄漏错误。
03-29 15:15:37.286 21406-21406/com.example.admin.rnew E/ActivityThread: Activity com.example.admin.rnew.MainActivity has leaked ServiceConnection android.speech.tts.TextToSpeech$Connection@dc017d2 that was originally bound here
android.app.ServiceConnectionLeaked: Activity com.example.admin.rnew.MainActivity has leaked ServiceConnection android.speech.tts.TextToSpeech$Connection@dc017d2 that was originally bound here
at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:1102)
at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:996)
at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1313)
at android.app.ContextImpl.bindService(ContextImpl.java:1296)
at android.content.ContextWrapper.bindService(ContextWrapper.java:614)
at android.speech.tts.TextToSpeech.connectToEngine(TextToSpeech.java:800)
at android.speech.tts.TextToSpeech.initTts(TextToSpeech.java:751)
at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:723)
at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:702)
at com.example.admin.rnew.MainActivity.onCreate(MainActivity.java:119)
at android.app.Activity.performCreate(Activity.java:6357)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2408)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2515)
at android.app.ActivityThread.access$1000(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1379)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5571)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)
请任何人告诉我我错在哪里。这是我的代码
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mProgressDialog = new ProgressDialog(this);
mMediaPlayer = new MediaPlayer();
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setMessage("Please wait ...");
usertext = (EditText) findViewById(R.id.editText);
play = (Button) findViewById(R.id.button);
voiceempty = engine.initialize_engine(getApplicationContext());
Toast.makeText(this,"Engine initialized",Toast.LENGTH_SHORT).show();
if (voiceempty) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Flite voices not installed. Please add voices in order to run the application");
builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("edu.cmu.cs.speech.tts.flite", "edu.cmu.cs.speech.tts.flite.DownloadVoiceData"));
startActivity(intent);
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
} else {
// Initialize the TTS
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
mTts = new TextToSpeech(this, this, "edu.cmu.cs.speech.tts.flite");
} else {
mTts = new TextToSpeech(this, this);
}
}
}
onDestroy 方法的代码是
@Override
public void onDestroy()
{
super.onDestroy();
mTts.stop();
mTts.shutdown();
mMediaPlayer.stop();
mMediaPlayer.release();
String fileName = Environment.getExternalStorageDirectory().getAbsolutePath() + FILENAME;
File file = new File(fileName);
if(file.exists())
{
file.delete();
Log.d("File_delete",fileName+" file deleted successfully");
}
}
我什至在 onBackPressed 方法中尝试过这个,但仍然得到同样的错误。