简体   繁体   English

Android处理程序中的TextToSpeech

[英]TextToSpeech in android handler

In my app i am displaying images 1 after other for 1 second.In that when first image comes i want use there texttospeech method to describe that image.i tried that.but null pointer exception is coming.here is the code - 在我的应用程序中,我将显示一个接一个的图像1秒钟,因为当第一个图像出现时,我想使用texttospeech方法来描述该图像。我尝试了一下,但是空指针异常来了。代码如下-

public class Shapes extends Activity {


    private TextToSpeech mTts;
     int flag=0;
     ImageView iv;
     int myData=1;
     Handler handler = new Handler();
     static int v[]={R.drawable.roundd,R.drawable.rectangle,R.drawable.bluesquare};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shapes);
         iv=(ImageView) findViewById(R.id.imageView1);
             handler.postDelayed(changeImage, 2000);
    }

     Runnable changeImage = new Runnable(){

         @Override
         public void run(){
             if(flag>2)
                handler.removeCallbacks(changeImage);
             else{
                 mTts.speak("hii", TextToSpeech.QUEUE_ADD, null);
                iv.setImageResource(v[flag++]);

              Intent checkIntent = new Intent();
                checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
                startActivityForResult(checkIntent,myData);   
             }
         }

     };

     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == myData) {
                if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                    // success, create the TTS instance
                    mTts = new TextToSpeech(this, (OnInitListener) this);
                }

                else {
                    // missing data, install it
                    Intent installIntent = new Intent();
                    installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                    startActivity(installIntent);
                }
            }

        }

    public void onInit(int status) {
        // TODO Auto-generated method stub
         if (status == TextToSpeech.SUCCESS) {
             // Toast.makeText(Abcd.this, "Text-To-Speech engine is initialized", Toast.LENGTH_LONG).show();
               }
        else if (status == TextToSpeech.ERROR) {
            Toast.makeText(this, "Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show();
                }

    }

logcat info- logcat信息-

11-29 10:21:07.118: E/AndroidRuntime(461): java.lang.NullPointerException
11-29 10:21:07.118: E/AndroidRuntime(461):  at com.my.KidsEasyLearning.Shapes$1.run(Shapes.java:37)
11-29 10:21:07.118: E/AndroidRuntime(461):  at android.os.Handler.handleCallback(Handler.java:587)
11-29 10:21:07.118: E/AndroidRuntime(461):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-29 10:21:07.118: E/AndroidRuntime(461):  at android.os.Looper.loop(Looper.java:123)
11-29 10:21:07.118: E/AndroidRuntime(461):  at android.app.ActivityThread.main(ActivityThread.java:3683)
11-29 10:21:07.118: E/AndroidRuntime(461):  at java.lang.reflect.Method.invokeNative(Native Method)
11-29 10:21:07.118: E/AndroidRuntime(461):  at java.lang.reflect.Method.invoke(Method.java:507)
11-29 10:21:07.118: E/AndroidRuntime(461):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-29 10:21:07.118: E/AndroidRuntime(461):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-29 10:21:07.118: E/AndroidRuntime(461):  at dalvik.system.NativeStart.main(Native Method)
11-29 10:21:09.899: I/Process(461): Sending signal. PID: 461 SIG: 9

The startup process for TextToSpeech can be a little trick. TextToSpeech的启动过程可能有些技巧。 I suggest using the build in code from this library 我建议使用此库中的内置代码

https://github.com/gast-lib/gast-lib/blob/master/library/src/root/gast/speech/SpeechRecognizingAndSpeakingActivity.java https://github.com/gast-lib/gast-lib/blob/master/library/src/root/gast/speech/SpeechRecognizingAndSpeakingActivity.java

Implement TextToSpeech in your adtivity class. 在您的adadivity类中实现TextToSpeech。

And declare variable as 并将变量声明为

mTts = new TextToSpeech(this, this);

These were visible mistakes. 这些都是明显的错误。 Refer to this tutorial for guidance 请参阅教程以获取指导

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM