3

我一直在寻找不同的线程,但没有找到任何对我遇到的问题有用的东西。我想摆脱语音识别开始时发出的“哔”声。我正在使用 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 onError(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

}
 }
4

3 回答 3

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



}
}
于 2013-05-04T04:25:49.757 回答
0

@Hoan Nguyen:我发现在 SpeechRecognizer 开始收听时静音“哔”的唯一方法是使用以下内容:

mAudioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true);

这是一个令人讨厌的 hack,因为我只是想静音 SpeechRecognizer 播放的那种“系统声音”,但另一种解决方案,使用mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, true)无效。

于 2014-06-26T08:43:44.320 回答
-1

Try to use this code:

mAudioManager.setStreamMute(AudioManager.VIBRATE_TYPE_NOTIFICATION, true);
于 2013-12-10T08:13:26.887 回答