4

APPでTTSを使用しています。onPuaseメソッドの実行中に、次のコードを使用してエンジンを停止します。問題は、ホームボタンを使用してアクティビティを一時停止すると、logcatセクションに貼り付けたときにエラーが発生することです。ログ猫(アクティビティcom.example.DragDrop.PlaySeriesがServiceConnectionをリークしました)から、問題は何らかのリークが原因であることがわかりました。ただし、問題はアプリを停止したり、機能を妨害したりすることではありません。しかし、私はこの問題の深刻さとこれを回避する方法を知りたいですか?

助けてください

protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();   
    Log.i("PlaySeries", "In pause play series");
    if(tts !=null){
        Log.i("PlaySeries", "In pause play series");
        tts.stop(); 
    //  tts.shutdown();
    }
}

@Override
protected void onDestroy() {  
    //Close the Text to Speech Library
    if(tts !=null){
        Log.i("PlaySeries", "In pause play series");
        tts.stop(); 
        tts.shutdown();
    }
    super.onDestroy();
}

とonCreateで

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.series);

    tts = new TextToSpeech(this,this);
}

LogCat

 03-22 10:46:45.205: E/ActivityThread(3881): Activity com.example.DragDrop.PlaySeries has leaked ServiceConnection android.speech.tts.TextToSpeech$Connection@2bbe5960 that was originally bound here03-22 10:46:45.205: E/ActivityThread(3881): android.app.ServiceConnectionLeaked: Activity com.example.DragDrop.PlaySeries has leaked ServiceConnection android.speech.tts.TextToSpeech$Connection@2bbe5960 that was originally bound here03-22 10:46:45.205: E/ActivityThread(3881): 
 03-22 10:46:45.205: E/ActivityThread(3881): android.app.ServiceConnectionLeaked: Activity com.example.DragDrop.PlaySeries has leaked ServiceConnection android.speech.tts.TextToSpeech$Connection@2bbe5960 that was originally bound here03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 
 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 
 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 
 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 
 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 
 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 
 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 
 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 
 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 
 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 
 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 
 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 
 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 
 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 
 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 
 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 
 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 
 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 
 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 
 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 
 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 
 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 
 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 
 03-22 10:46:45.205: E/ActivityThread(3881): 03-22 10:46:45.205: E/ActivityThread(3881): 
 03-22 10:46:45.205: E/ActivityThread(3881): 
4

3 に答える 3

13

このコードを持っているだけonDestroy

@Override
protected void onDestroy()
{
    super.onDestroy();

    tts.shutdown();
}
于 2013-03-22T06:09:12.843 に答える
10

TTSオブジェクトを静的にしたときにも同様のエラーが発生しました(そのため、デバイスを回転させてもTTSは読み上げを続けます)。デバイスを回転させたときにリークがトリガーされ、画面が縦向きモードと横向きモードの間で変化しました。

エラーが以前のバージョンのAndroidを実行している一部の古いデバイスでのみ報告されたため、しばらくの間私は困惑しました。ただし、修正を発見すると、問題はすべてのデバイスとバージョンに共通していると思われますが、新しいモデルではマスクされている可能性があります。

修正は、現在の(アクティビティ)コンテキストではなく、アプリケーションコンテキストを使用してTTSオブジェクトを初期化することでした。

これが私のコードです tts = new TextToSpeech(getApplicationContext(), this);

完全な差分は次の場所で入手できます: http: //code.google.com/p/android-tts-experiments/source/detail?r = b879fd1042151cbebc736b89a9fb660c895081ea

于 2013-12-31T22:48:45.397 に答える
1

これが、アクティビティの外部/サービス内からのTTS用のTurnKeyソリューションです...

onDestroyを使用できない場合は、読み上げが終了したときにリッスンしてから、シャットダウンしてTTSを停止します。アプリケーションコンテキストを使用して、リークを防ぎます。どうぞ。

    import android.content.Context;
import android.os.Build;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import android.util.Log;

import java.util.Locale;

/**
 * Created by acopp
 * Date: 1/11/2017.
 * Time: 6:11 AM
 * You have permission to use this file for any reason that is not for evil doing
 */

class SpeakThis{
    private Context mContext;
    private TextToSpeech mTextToSpeech;
    private String mTextToSpeak = "";
    private boolean mSpeechAvailable = false;

    SpeakThis(String text,Context context){
        mTextToSpeak = text;
        mContext = context.getApplicationContext();//THIS WILL FIX THE LEAK  THANKS TO JulianHarty

        mTextToSpeech = new TextToSpeech(mContext, mTextToSpeechListener);
        mTextToSpeech.setOnUtteranceProgressListener(mUtteranceListener);//NO ON DESTROY NEEDED JUST LISTEN FOR UTTERANCE STATUS
    }

    void talk(String talkThis){
        mTextToSpeak = talkThis;
        if(mSpeechAvailable){
            speak();
        }else{
            mTextToSpeech = new TextToSpeech(mContext, mTextToSpeechListener);
            mTextToSpeech.setOnUtteranceProgressListener(mUtteranceListener);
            mTextToSpeech.setSpeechRate(0.75f);
            mTextToSpeech.setPitch(1.2f);
        }
    }

    private TextToSpeech.OnInitListener mTextToSpeechListener = new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if (status == TextToSpeech.SUCCESS) {
                int result = mTextToSpeech.setLanguage(Locale.US);
                if (result == TextToSpeech.LANG_MISSING_DATA ||
                        result == TextToSpeech.LANG_NOT_SUPPORTED) {
                    Log.e("TTS","SPEECH NOT AVAILALBE");
                    mSpeechAvailable = false;

                } else {
                    if(mTextToSpeak.length() > 2){
                        speak();
                    }
                    mSpeechAvailable = true;
                }
            } else {
                Log.e("TTS", "Initilization Failed!");
                mSpeechAvailable = false;
            }

        }
    };

    private void speak() {

        if (mTextToSpeak.length() > 0) {
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    mTextToSpeech.speak(mTextToSpeak, TextToSpeech.QUEUE_ADD,null,"toastText");
                } else {
                    //noinspection deprecation
                    mTextToSpeech.speak(mTextToSpeak, TextToSpeech.QUEUE_ADD, null);
                }
            } catch (Exception ignore) {

            }
        }
    }

    private UtteranceProgressListener mUtteranceListener = new UtteranceProgressListener() {
        @Override
        public void onStart(String s) {
        }

        @Override
        public void onDone(String s) {//STOP AND SHUTDOWN TTS WHEN COMPLETED TALKING
            try {
                mTextToSpeech.stop();
                mTextToSpeech.shutdown();
            } catch (Exception ignore) {
            }
        }

        @Override
        public void onError(String s) {

        }
    };
}
于 2017-01-11T12:15:08.287 に答える