2

で同じコードを試してみましたAndroid 2.3。完全に機能します。ですでに使用したことを覚えていAndroid 4.0ます。Nexus 4Nexus 7Android 4.4.2onInit メソッドを使用してアプリケーションを実行しようとすると、呼び出されません。誰かがこの理由を知っていると教えてくれますか、または他の実装方法を提案しますか?

public class MyFragment extends Fragment implements TextToSpeech.OnInitListener{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_recognition, container, false);

        return v;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        // check for TTS data
        Intent checkTTSIntent = new Intent();
        checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        if(myTTS != null) {
            myTTS.stop();
            myTTS.shutdown();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == MY_DATA_CHECK_CODE) {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                // the user has the necessary data - create the TTS
                myTTS = new TextToSpeech(getActivity(), this);
            } else {
                //no data - install it now
                Intent installTTSIntent = new Intent();
                installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installTTSIntent);
            }
        }
    }

    // setup TTS
    public void onInit(int initStatus) {
        // check for successful instantiation
        // if (initStatus == TextToSpeech.SUCCESS) {
        // if(myTTS.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE)
        myTTS.setLanguage(Locale.ITALIAN);
        // }
        // else if (initStatus == TextToSpeech.ERROR) {
        // Toast.makeText(this, "Sorry! Text To Speech failed...",
        // Toast.LENGTH_LONG).show();
        // }
        speak("Sintesi Vocale Attiva");
    }

    private void speak(String speech) {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put(TextToSpeech.Engine.KEY_FEATURE_NETWORK_SYNTHESIS, "true");
        myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, hashMap);
    }
}
4

3 に答える 3

3

問題解決: 理由はわかりませんが、AsyncTask を開始したメソッドの呼び出しを削除すると、すべてうまくいきました。このメソッドは onCreateView で呼び出され、UDP データグラムを受信する必要があるため、TTS とは関係ありませんでした。

于 2014-01-21T08:20:27.033 に答える
-1

回避策として、タイマー タスクを開始して、テキスト読み上げが初期化されるまで非同期タスクの開始を遅らせることができます。タイマー タスクは、UI スレッドから非同期タスクを開始する実行可能ファイルを開始する必要があります。私の場合、1 秒の遅延は問題ありませんでした。

于 2015-11-05T13:17:22.600 に答える