tts for android应用程序

时间:2012-02-11 10:09:13

标签: android text-to-speech

我创建了一个Android应用程序,它在向其添加音频接口之前运行良好。对于tts教程,我遵循了this link

教程代码工作正常。但是当我在我的代码中使用它时,我得到Null pointer exception at tts.speak(s, TextToSpeech.QUEUE_FLUSH, null);行。请帮我解决这个问题。我确信问题不在于字符串s,我已经检查了。

public class TimerActivity extends Activity implements OnInitListener {

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

    Handler mhandler = new Handler();

    Button mybtn[] = new Button[12];

    Button select_btn, home_btn;

    EditText et;

    Bundle b;

    int var = 0;

    private int MY_DATA_CHECKCODE = 0;

    private TextToSpeech tts;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        select_btn = (Button) findViewById(R.id.ok);
        home_btn = (Button) findViewById(R.id.home);
        et = (EditText) findViewById(R.id.entry);
        et.setText("", TextView.BufferType.EDITABLE);
        b = new Bundle();

        for (int i = 0; i < mybtn.length; i++) {
            String btnid = "btn" + i;
            int resid = getResources().getIdentifier(btnid, "id",
                    getPackageName());
            mybtn[i] = (Button) findViewById(resid);
        }

        mybtn[var].requestFocus();
        Intent checkin = new Intent();
        checkin.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkin, MY_DATA_CHECKCODE);

        select_btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click

                Button bt;
                bt = (Button) getCurrentFocus();

                if (bt.equals(mybtn[10])) {
                    String s = et.getText().toString();
                    int l = s.length();

                    if (l > 0) {
                        String t;
                        if (l > 1)
                            t = s.substring(0, l - 1);
                        else
                            t = "";

                        et.setText(t, TextView.BufferType.EDITABLE);
                    }

                }

                else if (bt.equals(mybtn[11])) {

                    Intent in = new Intent(TimerActivity.this, Calling.class);
                    String s = et.getText().toString().trim();
                    b.putString("number", s);
                    in.putExtras(b);
                    if (s != null && s.length() > 0) {
                        tts.speak(s, TextToSpeech.QUEUE_ADD, null);
                        startActivity(in);
                        finish();
                    }

                    // else say enter a valid number
                }
                // CharSequence cs= bt.getText();
                // if(cs.equals(0))
                // et.setText("121");
                // else
                // if(!(bt.equals(0)))
                else
                    et.setText(et.getText() + "" + bt.getText(),
                            TextView.BufferType.EDITABLE);
                // else
                // et.setText("google",TextView.BufferType.EDITABLE);
            }
        });

        home_btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click

                finish();
            }
        });

        final Runnable mUpdateTimeTask = new Runnable() {
            public void run() {
                // final long start= System.currentTimeMillis();
                // mhandler.removeCallbacks(this);
                // et.setText("i am the man");

                mybtn[var].requestFocus();
                String s = mybtn[var].getText().toString();
                s = "abcd";
                if (s != null && s.length() > 0)
                    tts.speak(s, TextToSpeech.QUEUE_FLUSH, null);
                var = (var + 1) % 12;
                // mybtn[var].requestFocus();

                mhandler.postDelayed(this, 2000);

            }
        };

        mhandler.postDelayed(mUpdateTimeTask, 100);

    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == MY_DATA_CHECKCODE) {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                // success, create the TTS instance
                tts = new TextToSpeech(this, this);
                tts.setLanguage(Locale.US);
            } else {
                // missing data, install it
                Intent installIntent = new Intent();
                installIntent
                        .setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installIntent);
            }
        }

    }

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

        if (status == TextToSpeech.SUCCESS) {
            Toast.makeText(TimerActivity.this,
                    "Text-To-Speech engine is initialized", Toast.LENGTH_LONG)
                    .show();
        } else if (status == TextToSpeech.ERROR) {
            Toast.makeText(TimerActivity.this,
                    "Error occurred while initializing Text-To-Speech engine",
                    Toast.LENGTH_LONG).show();
        }

    }

}

1 个答案:

答案 0 :(得分:0)

在你调用finish()之前,你需要调用tts.shutdown()来告诉Android你不再使用它了。您应该调用onDestroy()。一旦应用程序关闭,执行此操作将停止扬声器。