android - 文字转语音 - Android

标签 android text-to-speech

我正在开发一个简单的游戏。第一轮的想法是,第一个 Activity 有 26 个按钮(图像)- 英文字母表,我想让它在单击按钮时发音。例如,当我按下“a”按钮时,它必须说“a”。我怎样才能得到每个按钮的 ID,然后是它的字符串并将它作为参数传递给 speak() 方法?提前致谢!

代码在这里:

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;

import java.util.Locale;
import java.util.Random;


import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

public class FirstActivity extends Activity {

    /** Called when the activity is first created. */

    TextToSpeech ttx;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.firstact);

        ttx = new TextToSpeech(
                FirstActivity.this, 
                new TextToSpeech.OnInitListener() {

            public void onInit(int status) {
                // TODO Auto-generated method stub
                if(status !=TextToSpeech.ERROR){
                    ttx.setLanguage(Locale.US);                 
                }

            }
        });


        TableLayout table = (TableLayout) findViewById(R.id.table);

        String alphabet = "abcdefghijqlmnopqrstuvwxyz";

        int pos = 0;
        for (int i = 0; i < 7; i++) {
            TableRow row = new TableRow(this);
            for (int j = 0; j < 4; j++) {
                if (pos == 26) {
                    break;
                }
                Button but = new Button(this);
                /*but.setOnClickListener(btnListener);*/
                but.setText(Character.toString(alphabet.charAt(pos++)));
                but.setHeight(80);
                but.setWidth(70);
                but.setBackgroundResource(R.drawable.burti);
                row.addView(but);
                but.setId(pos);

                 but.setOnClickListener((OnClickListener) this);


            table.addView(row);

        }}



    }
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        if(ttx != null){
            ttx.stop();
            ttx.shutdown();     
        }
        super.onPause();
    }

    public void onClick(View arg0) {
        // TODO Auto-generated method stub


        String pressed = but.getText().toString(); //error here....
        ttx.speak(pressed, TextToSpeech.QUEUE_FLUSH, null);

    }

}

最佳答案

我做了一个工作示例:http://frank.anemaet.nl/android/TalkingButtons.zip 这是工作代码的 fragment :

for (int i = 0; i < alphabet.length(); i++)
    {
        TableRow row = new TableRow(this);
        Button but;
        but = new Button(this);
        but.setText(Character.toString(alphabet.charAt(i)));
        but.setHeight(button_width);
        but.setWidth(button_height);
        but.setId(i);

        but.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
              performClick(v);
            }
          });

        row.addView(but);
        table.addView(row);
    }

...

public void performClick(View arg0){

    Button tv = (Button)findViewById(arg0.getId());
    String pressed = tv.getText().toString(); 
    ttx.speak(pressed, TextToSpeech.QUEUE_FLUSH, null);

}

关于android - 文字转语音 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10969525/

相关文章:

Android主题: Can I just set alpha value to an existing color?

java - 为什么 UtteranceProgress Listener 不会在 Text to Speech 上被调用?

unity-game-engine - (Unity 5.6) (Watson SDK) 我的 'text to speech' 小部件不会解析/播放关联的字符串

c - IRC(Twitch 聊天)文本转语音

azure - 电影 : importing audio from text-to-speech in memory

android - ListView 显示排序的数据而不是它在数据库中的存储方式

java - 如何使用 Exchangeratesapi.io 和 okhttp 更改汇率基础货币,并在微调器中选择所有基础货币?

java - Json 类型不匹配

Android:应用程序的生命周期

c++ - 如何获取Microsoft TTS语音的耗时?