android 4.2 google 语音解锁,Android语音识别系统在Android 4.1&4.2上的应用

这是Android版本4.1.1的工作。public class MyService extends Service{

protected AudioManager mAudioManager;

protected SpeechRecognizer mSpeechRecognizer;

protected Intent mSpeechRecognizerIntent;

protected final Messenger mServerMessenger = new Messenger(new IncomingHandler(this));

protected boolean mIsListening;

protected volatile boolean mIsCountDownOn;

private boolean mIsStreamSolo;

static final int MSG_RECOGNIZER_START_LISTENING = 1;

static final int MSG_RECOGNIZER_CANCEL = 2;

@Override

public void onCreate()

{

super.onCreate();

mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);

mSpeechRecognizer.setRecognitionListener(new SpeechRecognitionListener());

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

}

protected static class IncomingHandler extends Handler

{

private WeakReference mtarget;

IncomingHandler(MyService target)

{

mtarget = new WeakReference(target);

}

@Override

public void handleMessage(Message msg)

{

final MyService target = mtarget.get();

switch (msg.what)

{

case MSG_RECOGNIZER_START_LISTENING:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)

{

// turn off beep sound

if (!mIsStreamSolo)

{

mAudioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true);

mIsStreamSolo = true;

}

}

if (!target.mIsListening)

{

target.mSpeechRecognizer.startListening(target.mSpeechRecognizerIntent);

target.mIsListening = true;

//Log.d(TAG, "message start listening"); //$NON-NLS-1$

}

break;

case MSG_RECOGNIZER_CANCEL:

if (mIsStreamSolo)

{

mAudioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, false);

mIsStreamSolo = false;

}

target.mSpeechRecognizer.cancel();

target.mIsListening = false;

//Log.d(TAG, "message canceled recognizer"); //$NON-NLS-1$

break;

}

}

}

// Count down timer for Jelly Bean work around

protected CountDownTimer mNoSpeechCountDown = new CountDownTimer(5000, 5000)

{

@Override

public void onTick(long millisUntilFinished)

{

// TODO Auto-generated method stub

}

@Override

public void onFinish()

{

mIsCountDownOn = false;

Message message = Message.obtain(null, MSG_RECOGNIZER_CANCEL);

try

{

mServerMessenger.send(message);

message = Message.obtain(null, MSG_RECOGNIZER_START_LISTENING);

mServerMessenger.send(message);

}

catch (RemoteException e)

{

}

}

};

@Override

public void onDestroy()

{

super.onDestroy();

if (mIsCountDownOn)

{

mNoSpeechCountDown.cancel();

}

if (mSpeechRecognizer != null)

{

mSpeechRecognizer.destroy();

}

}

protected class SpeechRecognitionListener implements RecognitionListener

{

@Override

public void onBeginningOfSpeech()

{

// speech input will be processed, so there is no need for count down anymore

if (mIsCountDownOn)

{

mIsCountDownOn = false;

mNoSpeechCountDown.cancel();

}

//Log.d(TAG, "onBeginingOfSpeech"); //$NON-NLS-1$

}

@Override

public void onBufferReceived(byte[] buffer)

{

}

@Override

public void onEndOfSpeech()

{

//Log.d(TAG, "onEndOfSpeech"); //$NON-NLS-1$

}

@Override

public void onError(int error)

{

if (mIsCountDownOn)

{

mIsCountDownOn = false;

mNoSpeechCountDown.cancel();

}

mIsListening = false;

Message message = Message.obtain(null, MSG_RECOGNIZER_START_LISTENING);

try

{

mServerMessenger.send(message);

}

catch (RemoteException e)

{

}

//Log.d(TAG, "error = " + error); //$NON-NLS-1$

}

@Override

public void onEvent(int eventType, Bundle params)

{

}

@Override

public void onPartialResults(Bundle partialResults)

{

}

@Override

public void onReadyForSpeech(Bundle params)

{

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)

{

mIsCountDownOn = true;

mNoSpeechCountDown.start();

}

Log.d(TAG, "onReadyForSpeech"); //$NON-NLS-1$

}

@Override

public void onResults(Bundle results)

{

//Log.d(TAG, "onResults"); //$NON-NLS-1$

}

@Override

public void onRmsChanged(float rmsdB)

{

}

}}

如果您在应用程序中使用文本语音,请确保关闭onResults中的Solo流。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值