문제

I'm trying a text to speech proof of concept on Android with no success. Any suggestion on this, here how I'm proceeding:

This is happening in a fragment:

public class PhotoDetailsFragment extends Fragment implements TextToSpeech.OnInitListener{
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //initialising text to speech to speak the common names
        tts = new TextToSpeech(getActivity(), this);

    }

@Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            int result = tts.setLanguage(Locale.US);

            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            } else {
                speakOut();
            }
        } else {
            Log.e("TTS", "Initilization Failed!");
        }
    }

private void speakOut() {
        System.out.println("Speaking");
        tts.speak("Animal", TextToSpeech.QUEUE_FLUSH, null);
    }
..................
}
도움이 되었습니까?

해결책

I have had bad luck using tts.setLanguage(Locale.US). Here's the code I use instead:

    if (mSpeaker.isLanguageAvailable(Locale.UK) == TextToSpeech.LANG_AVAILABLE) {
        Log.i(TAG, "Whee!  We have UK English");
        mSpeaker.setLanguage(Locale.UK);

    } else if (mSpeaker.isLanguageAvailable(Locale.US) == TextToSpeech.LANG_AVAILABLE) {
        Log.i(TAG, "We have US English");
        mSpeaker.setLanguage(Locale.US);
    } else {
        Log.i(TAG, "Using Speech engine default Locale");

    }

When this code runs on my Samsung S4 phone, it uses the default locale instead of Locale.UK or Locale.US.

Try removing the tts.setLanguage(Locale.US) and the tests for result.

Another possibility is that tts.speak() must be called from the UI thread (I'm just guessing that - I don't know for sure). I've noticed many Android APIs don't specify what calls must be made from the UI thread. You might try the following code instead of the call to speakOut():

    getActivity().runOnUiThread(new Runnable() {

        @Override
        public void run() {
                speakOut();

        }

    });
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top