Android 应用程序的 tts

发布于 2025-01-04 18:08:09 字数 5440 浏览 0 评论 0原文

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

教程代码运行良好。但是当我在代码中使用它时,我在 tts.speak(s, TextToSpeech.QUEUE_FLUSH, null); line 处收到 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();
        }

    }

}

i have created an android application which is working well before adding the audio interface to it. For the tts tutorial i have followed this link

The tutorial code is working fine. but when i use it in my code i am getting Null pointer exception at tts.speak(s, TextToSpeech.QUEUE_FLUSH, null); line . please help me out to sort out this problem.and i am sure that problem is not with the string s, i have checked it.

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

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白昼 2025-01-11 18:08:09

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

Before you call finish(), you need to call tts.shutdown() to tell Android you're not using it any more. You should probably call in onDestroy(). Doing this will stop the speaker once the application is closed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文