语音识别中的果冻豆蜂鸣声

我一直在研究不同的线程,但是没有发现任何对我遇到的问题有用的东西.我想摆脱语音识别开始时发出的“哔”声.我正在使用Jelly Bean 4.2.2,因此不确定在早期版本中是否存在相同的问题,如果我也想解决此问题.另外,我想知道是否有人对如何响应某些语音识别结果提出建议.请让我知道我的代码如何即兴包含这些功能.

package com.example.speech;

import java.util.ArrayList;

import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity;
import android.content.Intent;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.util.Log;

public class MainActivity extends Activity implements  OnClickListener {



ListView lv;
private SpeechRecognizer mSpeechRecognizer;
private Intent mSpeechRecognizerIntent; 
boolean reseter = false;
private AudioManager mAudioManager;
private volatile boolean mNoSpeechCountDownOn;


@Override
protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    boolean available = SpeechRecognizer.isRecognitionAvailable(this);
    Log.d("Speech", "available = " + available);
    mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
    mSpeechRecognizer.setRecognitionListener(new SpeechListener());
    mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
            this.getPackageName());
    mAudioManager = (AudioManager) getSystemService(this.AUDIO_SERVICE);
    commandA();



}

private CountDownTimer mNoSpeechCountDown = new CountDownTimer(5000, 5000)
{   
    @Override
    public void onTick(long millisUntilFinished)
    {

    }

    @SuppressWarnings("synthetic-access")
    @Override
    public void onFinish()
    {
        mNoSpeechCountDownOn = false;
        mSpeechRecognizer.cancel();
        mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
    }
};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


private class SpeechListener implements RecognitionListener {


    @Override
    public void onBeginningOfSpeech() {
        if (mNoSpeechCountDownOn)
        {
            mNoSpeechCountDownOn = false;
            mNoSpeechCountDown.cancel();
        }
        Log.d("Speech", "onBeginningOfSpeech");

    }

    @Override
    public void onBufferReceived(byte[] buffer) {
        Log.d("Speech", "onBufferReceived");
    }

    @Override
    public void onEndOfSpeech() {
        Log.d("Speech", "onEndOfSpeech");


    }

    @Override
    public void one rror(int error) {
        if (mNoSpeechCountDownOn)
        {
            mNoSpeechCountDownOn = false;
            mNoSpeechCountDown.cancel();
        }
        Log.d("Speech", "onError");
    }

    @Override
    public void onEvent(int eventType, Bundle params) {
        Log.d("Speech", "onEvent");
    }

    @Override
    public void onPartialResults(Bundle partialResults) {
        Log.d("Speech", "onPartialResults");
    }

    @Override
    public void onReadyForSpeech(Bundle params) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
        {
            mNoSpeechCountDownOn = true;
            mNoSpeechCountDown.start();
            mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, false);
        }
        Log.d("Speech", "onReadyForSpeech");
        try {
            Thread.sleep(4900);

        } catch (InterruptedException e) {
            mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
            mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
            Log.d("speech", "Mute on");

        }

    }


    @Override
    public void onResults(Bundle results) {
        Log.d("Speech", "results");


        ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

        // Do whatever you want here
        try {
            Thread.sleep(1);

        } catch (InterruptedException e) {
            mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
            mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
            Log.d("speech", "Mute on");

        }
    }

    @Override
    public void onRmsChanged(float rmsdB) {
        //Log.d("Speech", "onRmsChanged");
    }



}
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}
 }

解决方法:

public class MainActivity extends Activity implements OnClickListener {

ListView lv;
private SpeechRecognizer mSpeechRecognizer;
private Intent mSpeechRecognizerIntent; 
boolean reseter = false;
private AudioManager mAudioManager;

Override
protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView)findViewById(R.id.lvVoiceReturn);
Button b= (Button)findViewById(R.id.bVoice);
b.setOnClickListener(this);
boolean available = SpeechRecognizer.isRecognitionAvailable(this);
Log.d("Speech", "available = " + available);
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizer.setRecognitionListener(new SpeechListener());
mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
        this.getPackageName());
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);


}

private CountDownTimer mNoSpeechCountDown = new CountDownTimer(5000, 5000)
{   
    @Override
    public void onTick(long millisUntilFinished)
    {

    }

    @SuppressWarnings("synthetic-access")
    @Override
    public void onFinish()
    {
        mNoSpeechCountDownOn = false;
        mSpeechRecognizer.cancel();
        mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
    }
};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


@Override
public void onClick(View v) {
 // TODO Auto-generated method stub`
Log.d("speech", "button active");
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, true);

}
private class SpeechListener implements RecognitionListener {


@Override
public void onBeginningOfSpeech() {
    if (mNoSpeechCountDownOn)
    {
                mNoSpeechCountDownOn = false;
                mNoSpeechCountDown.cancel();
    }
    Log.d("Speech", "onBeginningOfSpeech");

}

@Override
public void onBufferReceived(byte[] buffer) {
    Log.d("Speech", "onBufferReceived");
}

@Override
public void onEndOfSpeech() {
    Log.d("Speech", "onEndOfSpeech");


}

@Override
public void one rror(int error) {
    if (mNoSpeechCountDownOn)
    {
                mNoSpeechCountDownOn = false;
                mNoSpeechCountDown.cancel();
    }
    Log.d("Speech", "onError");
}

@Override
public void onEvent(int eventType, Bundle params) {
    Log.d("Speech", "onEvent");
}

@Override
public void onPartialResults(Bundle partialResults) {
    Log.d("Speech", "onPartialResults");
}

@Override
public void onReadyForSpeech(Bundle params) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
    {
                mNoSpeechCountDownOn = true;
                mNoSpeechCountDown.start();
          mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, false);
    }
    Log.d("Speech", "onReadyForSpeech");

}


@Override
public void onResults(Bundle results) {
    Log.d("Speech", "results");
    ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

  // Do whatever you want here

}

@Override
public void onRmsChanged(float rmsdB) {
    //Log.d("Speech", "onRmsChanged");
}



}
}
上一篇:CF1515-C. Phoenix and Towers


下一篇:触摸屏驱动