1

我对从编辑文本中说出单词的代码感到困惑,我使用的代码没有说出来,但它正确地获取了值、字符数等。

我使用的代码是..

 public class AndroidTextToSpeechActivity extends Activity implements
    TextToSpeech.OnInitListener {
/** Called when the activity is first created. */

private TextToSpeech tts;
private Button btnSpeak;
private EditText txtText;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    System.out.println("Entered to the Activity");
    tts = new TextToSpeech(this, this);

    btnSpeak = (Button) findViewById(R.id.btnSpeak);

    txtText = (EditText) findViewById(R.id.txtText);

    // button on click event
    btnSpeak.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            speakOut();
        }

    });
}

@Override
public void onDestroy() {
    // Don't forget to shutdown!
    if (tts != null) {
        System.out.println("Entered  to OnDestroy");
        tts.stop();
        tts.shutdown();
    }
    super.onDestroy();
}

@Override
public void onInit(int status) {
    // TODO Auto-generated method stub

    System.out.println("Enterd init Function");
    if (status == TextToSpeech.SUCCESS) {

        int result = tts.setLanguage(Locale.ENGLISH);

        // tts.setPitch(5); // set pitch level

        // tts.setSpeechRate(2); // set speech speed rate

        if (result == TextToSpeech.LANG_MISSING_DATA
                || result == TextToSpeech.LANG_NOT_SUPPORTED) {
            Log.e("TTS", "Language is not supported");
        } else {
            btnSpeak.setEnabled(true);
            speakOut();
        }

    } else {
        Log.e("TTS", "Initilization Failed");
    }

}

private void speakOut() {
     System.out.println("Entered Speakout");
    String text = txtText.getText().toString();

    tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}

它返回一条不支持语言的消息。我该如何纠正它

4

2 回答 2

6

试试这个可能对你有帮助。

我认为您的设备中未安装语音合成器,因此请使用这些。如果未安装,它将重定向到 google play 来安装它。

 public void onInit(int status) {
    if (status == TextToSpeech.SUCCESS) {
        int result = talker.setLanguage(Locale.US);
        if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
            Log.e("TTS", "This Language is not supported");
            Intent installIntent = new Intent();
            installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(installIntent);
        } else {
            btnSpeak.setEnabled(true);
            speakOut();
        }
    } else {
        Log.e("TTS", "Initilization Failed!");
    }
}
于 2013-01-31T09:26:22.013 回答
1

试试下面的代码。它对我来说很好,我希望它也能帮助你。

public class TexttoSpeechActivity extends Activity implements OnClickListener,
    OnInitListener {
    private Button m_btnSpech;
    private EditText m_etText;
    // TTS object
    private TextToSpeech m_tts;
    // status check code
    private final int m_data = 0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        m_btnSpech = (Button) findViewById(R.id.mbtnSpeak);
        m_etText = (EditText) findViewById(R.id.metTextValues);
        // listen for clicks
        m_btnSpech.setOnClickListener(this);        
        // check for TTS data intent to check if a TTS engine is installed
        Intent m_intent = new Intent();
        m_intent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(m_intent, m_data);
    }
    // respond to button click.
    @Override
    public void onClick(View p_v) {
        // get the text entered.
        String m_word = m_etText.getText().toString();
        speakWord(m_word);
    }
    /**
     * Executed when a new TTS is instantiated. Some text is spoken via TTS
     * here.
     * 
     * @param p_word
     *            -contains the word entered into the edittext.
     */
    private void speakWord(String p_word) {
        // speak straight away
        m_tts.speak(p_word, TextToSpeech.QUEUE_FLUSH, null);
    }
    /**
     * This is the callback from the TTS engine check, if a TTS is installed we
     * create a new TTS instance (which in turn calls onInit), if not then we
     * will create an intent to go off and install a TTS engine
     * 
     * @param p_requestCode
     *            int Request code returned from the check for TTS engine.
     * @param p_resultCode
     *            int Result code returned from the check for TTS engine.
     * @param p_data
     *            Intent Intent returned from the TTS check.
     */
    @Override
    protected void onActivityResult(int p_requestcode, int p_resultcode,
            Intent p_data) {
        if (p_requestcode == m_data) {
            if (p_resultcode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                // the user has the necessary data - create the TTS
                m_tts = new TextToSpeech(this, this);
            } else {
                // no data - install it now
                Intent m_intnt = new Intent();
                m_intnt.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(m_intnt);
            }
        }
    }
    // setup TTS
    @Override
    public void onInit(int p_status) {
        // check for successful instantiation
        if (p_status == TextToSpeech.SUCCESS) {
            if (m_tts.isLanguageAvailable(Locale.US) == TextToSpeech.LANG_AVAILABLE) {
                m_tts.setLanguage(Locale.US);
                Toast.makeText(this, "Text To Speech ", Toast.LENGTH_LONG)
                        .show();
            }
            Toast.makeText(TexttoSpeechActivity.this,
                    "Text-To-Speech engine is initialized", Toast.LENGTH_LONG).show();
        } else if (p_status == TextToSpeech.ERROR) {
            Toast.makeText(this, "Sorry! Text To Speech failed...",
                    Toast.LENGTH_LONG).show();
        }
    }
    /**
     * Be kind, once you've finished with the TTS engine, shut it down so other
     * applications can use it without us interfering with it.
     */
    @Override
    public void onDestroy() {
        // Don't forget to shutdown!
        if (m_tts != null) {
            m_tts.stop();
            m_tts.shutdown();
        }
        super.onDestroy();
    }
}
于 2013-01-31T09:27:19.907 回答