Pergunta

I tried the same code on Android 2.3.it works perfectly. I remember that I already used with Android 4.0. Now trying the application on a Nexus 4 and a Nexus 7 with Android 4.4.2 onInit method is not called. Someone tell me would know the reasons for this, or suggest other methods of implementation?

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);
    }
}
Foi útil?

Solução 2

Problem Solved: I do not know why, but eliminating a call to a method that initiated a AsyncTask, it all worked out. The method was called in onCreateView, and had nothing to do with the TTS, as needed to receive UDP datagrams.

Outras dicas

It seems that there is some issue related to the AsyncTask processing when you are initializing the TTS engine in Android 4.4.2.

If someone else face this issue, I suggest to wait until the onInit method is called with some loop that check out this condition, and then continue with the launching of the AsyncTask processes (even if they are not related to the TTS). That worked for me at least...

As a work arround, you can start a timer task to delay the start of your Async Task until after the text to speech is initialized. The timer task should start a runnable which does start your Async Task from UI thread. In my case a delay of one second was fine.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top