SpeechRecognitionService具有单词识别功能

时间:2014-03-26 11:20:37

标签: java android android-service android-speech-api

标题我认为不清楚。在我的项目中,我想要一个在后台运行的服务,当用户说“hello phone”或某个单词/短语时,我的应用程序开始识别语音。实际上它“有效”但不是正确的方式......我有一项服务,这项服务可以检测到声音。

public class SpeechActivationService 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;

static String TAG = "Icaro";

static final int MSG_RECOGNIZER_START_LISTENING = 1;
static final int MSG_RECOGNIZER_CANCEL = 2;

private int mBindFlag;
private Messenger mServiceMessenger;

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

    //mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
}

protected static class IncomingHandler extends Handler
{
    private WeakReference<SpeechActivationService> mtarget;

    IncomingHandler(SpeechActivationService target)
    {
        mtarget = new WeakReference<SpeechActivationService>(target);
    }


    @Override
    public void handleMessage(Message msg)
    {
        final SpeechActivationService target = mtarget.get();

        switch (msg.what)
        {
            case MSG_RECOGNIZER_START_LISTENING:

                if (Build.VERSION.SDK_INT >= 16);//Build.VERSION_CODES.JELLY_BEAN)
                {
                    // turn off beep sound  
                    target.mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, 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:
                  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 int onStartCommand (Intent intent, int flags, int startId) 
{
    //mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
    try
    {
        Message msg = new Message();
        msg.what = MSG_RECOGNIZER_START_LISTENING; 
        mServerMessenger.send(msg);
    }
    catch (RemoteException e)
    {

    }
    return  START_NOT_STICKY;
}

@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)
    {
        String sTest = "";
    }

    @Override
    public void onEndOfSpeech()
    {
        Log.d("TESTING: SPEECH SERVICE", "onEndOfSpeech"); //$NON-NLS-1$
     }

    @Override
    public void onError(int error)
    {
        if (mIsCountDownOn)
        {
            mIsCountDownOn = false;
            mNoSpeechCountDown.cancel();
        }
         Message message = Message.obtain(null, MSG_RECOGNIZER_START_LISTENING);
         try
         {
             mIsListening = false;   
             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 >= 16);//Build.VERSION_CODES.JELLY_BEAN)
        {
            mIsCountDownOn = true;
            mNoSpeechCountDown.start();
            mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, false);
        }
        Log.d("TESTING: SPEECH SERVICE", "onReadyForSpeech"); //$NON-NLS-1$
    }

    @Override
    public void onResults(Bundle results)
    {
        ArrayList<String> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        Log.d(TAG, (String) data.get(0));

        //mSpeechRecognizer.startListening(mSpeechRecognizerIntent);

        mIsListening = false;
        Message message = Message.obtain(null, MSG_RECOGNIZER_START_LISTENING);
        try
        {
               mServerMessenger.send(message);
        }
        catch (RemoteException e)
        {

        }

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

    @Override
    public void onRmsChanged(float rmsdB)
    {

    }
}

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}
}

我在MainActivity中开始服务只是为了尝试:

Intent i = new Intent(context, SpeechActivationService.class);
startService(i);

它检测到语音输入......太多了!!!每当它检测到某些东西时,它就是“双字节”。太多的bips !!令人沮丧的是......我只想在我说“你好电话”或“开始”或特定字时开始!我试着看看这个https://github.com/gast-lib/gast-lib/blob/master/library/src/root/gast/speech/activation/WordActivator.java,但实际上我不知道如何使用这个库。我尝试看到这个问题onCreate of android service not called,但我不明白我必须做什么..无论如何,我已经导入了gast库..我只需要知道如何使用它。任何人都可以一步一步帮助我吗?感谢

1 个答案:

答案 0 :(得分:0)

使用setStreamSolo(AudioManager.STREAM_VOICE_CALL,true)代替setStreamMute。请记住在MSG_RECOGNIZER_CANCEL

的情况下添加setStreamSolo(AudioManager.STREAM_VOICE_CALL,false)