简体   繁体   中英

Speech Recognition in Surface View [android]

I am trying to make speech recognition in a surface view work. I am getting one small error.
Error:

It's not drawing the text but its drawing the background.

This line(#64):

c.drawARGB(255, 0, 0, 255); //works
p.setTextSize(50);
p.setColor(Color.WHITE);
c.drawText("mText: "+mText, 500, 500, p);  //does not work

Code:

package com.l3g3nds.virtual_dog;

import java.util.ArrayList;
import java.util.Random;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

@TargetApi(8)
public class Main extends SurfaceView implements Runnable, OnTouchListener{

    Thread t;
    boolean ok, started;
    SurfaceHolder holder;

    private String mText;
    private SpeechRecognizer sr;
    private static final String TAG = "MyStt3Activity";

    public Main(Context context) {
        super(context);

        holder = getHolder();
        setOnTouchListener(this);

        sr = SpeechRecognizer.createSpeechRecognizer(context);    
        sr.setRecognitionListener(new listener());

        mText = "nothing";

        ok = false;
        t = null;
    }

    public void run() {
        while(ok == true)
        {    if(!(holder.getSurface().isValid()))
                continue;
            Paint p = new Paint();
            Canvas c = holder.lockCanvas();  

            c.drawARGB(255, 0, 0, 255);
            p.setTextSize(50);
            p.setColor(Color.WHITE);
            c.drawText("mText: "+mText, 500, 500, p);

            holder.unlockCanvasAndPost(c);
        }
    }

    public void pause() {
        ok = false;
        while(true){    
            try{
                t.join();
            }catch(Exception e){ e.printStackTrace();    }
            break;
        }
        t = null;
    }

    public void resume(){
        ok = true;
        t = new Thread(this);
        t.start();
    }

    @SuppressWarnings("deprecation")
    public boolean onTouch(View v, MotionEvent me) {

        int action = me.getAction();
        if(action == MotionEvent.ACTION_DOWN){
            Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);        
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
            intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test");

            intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5); 
                 sr.startListening(intent);
                 Log.i("111111","11111111");
        }
        else if(action == MotionEvent.ACTION_UP){

        }
        else if(action == MotionEvent.ACTION_MOVE){

        }
        return true;
    }

    class listener implements RecognitionListener          
    {
             public void onReadyForSpeech(Bundle params)
             {
                      Log.d(TAG, "onReadyForSpeech");
             }
             public void onBeginningOfSpeech()
             {
                      Log.d(TAG, "onBeginningOfSpeech");
             }
             public void onRmsChanged(float rmsdB)
             {
                      Log.d(TAG, "onRmsChanged");
             }
             public void onBufferReceived(byte[] buffer)
             {
                      Log.d(TAG, "onBufferReceived");
             }
             public void onEndOfSpeech()
             {
                      Log.d(TAG, "onEndofSpeech");
             }
             public void onError(int error)
             {
                      Log.d(TAG,  "error " +  error);
                      mText.equals("error " + error);
             }
             public void onResults(Bundle results)                   
             {
                      String str = new String();
                      Log.d(TAG, "onResults " + results);
                      ArrayList<String> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
                      for (int i = 0; i < data.size(); i++)
                      {
                                Log.d(TAG, "result " + data.get(i));
                                str += data.get(i);
                      }
                      //mText.setText("results: "+String.valueOf(data.size())); 
                      if(data.size() > 0)
                         mText.equals(String.valueOf(data.get(0)));

             }
             public void onPartialResults(Bundle partialResults)
             {
                      Log.d(TAG, "onPartialResults");
             }
             public void onEvent(int eventType, Bundle params)
             {
                      Log.d(TAG, "onEvent " + eventType);
             }
    }
}

I thought my class was a context? What am I doing wrong.

如果要将Context传递给构造函数,则应使用: sr = SpeechRecognizer.createSpeechRecognizer(context);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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