0

我正在尝试在表面视图中进行语音识别。我遇到了一个小错误。
错误:

它不是绘制文本,而是绘制背景。

这一行(#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

代码:

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

我以为我的课是一个上下文?我究竟做错了什么。

4

1 回答 1

1

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

于 2013-01-18T12:52:17.057 回答