来自字符串资源的文本到语音

时间:2014-05-06 15:58:15

标签: android text-to-speech

我似乎无法获得TextToSpeech示例来从字符串资源中获取文本。我只需按一下按钮就可以通过textView / textEdit使其正常工作,但我所追求的是用户按下按钮,texttospeech说明与该按钮的@String资源相关联的内容。这可能吗?

即。 - 如果我想在地图中添加自定义标记,我会使用:

.setIcon(BitmapDescriptorFactory.fromResource(getResources().getIdentifier(icon, "drawable", getPackageName())));

我找不到任何有关将@string用于文字转换语言的信息

由于

2 个答案:

答案 0 :(得分:0)

如何从strings.xml获取字符串:

"getResources().getString(R.string.name_of_variable_in_stringsxmlfile);" 

见:

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);

    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 tts!
    if (tts != null) {
        tts.stop();
        tts.shutdown();
    }
    super.onDestroy();
}

@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 {
            btnSpeak.setEnabled(true);
            speakOut();
        }

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

}

private void speakOut() {

    String text = txtText.getText().toString();

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

来源(为我工作):here

答案 1 :(得分:0)

对于来自字符串资源的文本到语音,请尝试以下方法:

String me =“嗨”;

    Tts.speak(me,TextToSpeech.QUEUE_FLUSH,  // Drop all pending entries in the playback queue.
            null);

希望有所帮助