如何注册自定义语音识别服务?

时间:2012-04-03 16:42:52

标签: android android-service

我创建了一个简单的语音识别服务:为此,我创建了一个android.speech.RecognitionService的子类,并创建了一个活动来启动和停止此服务。

我的自定义语音识别服务通常使用默认语音识别器,因为我的目标只是了解RecognitionServiceRecognitionService.Callback类的工作原理。

public class SimpleVoiceService extends RecognitionService {

    private SpeechRecognizer m_EngineSR;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i("SimpleVoiceService", "Service started");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i("SimpleVoiceService", "Service stopped");
    }

    @Override
    protected void onCancel(Callback listener) {
        m_EngineSR.cancel();
    }

    @Override
    protected void onStartListening(Intent recognizerIntent, Callback listener) {
        m_EngineSR.setRecognitionListener(new VoiceResultsListener(listener));
        m_EngineSR.startListening(recognizerIntent);
    }

    @Override
    protected void onStopListening(Callback listener) {
        m_EngineSR.stopListening();
    }


    /**
     * 
     */
    private class VoiceResultsListener implements RecognitionListener {

        private Callback m_UserSpecifiedListener;

        /**
         * 
         * @param userSpecifiedListener
         */
        public VoiceResultsListener(Callback userSpecifiedListener) {
            m_UserSpecifiedListener = userSpecifiedListener;
        }

        @Override
        public void onBeginningOfSpeech() {
            try {
                m_UserSpecifiedListener.beginningOfSpeech();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onBufferReceived(byte[] buffer) {
            try {
                m_UserSpecifiedListener.bufferReceived(buffer);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onEndOfSpeech() {
            try {
                m_UserSpecifiedListener.endOfSpeech();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onError(int error) {
            try {
                m_UserSpecifiedListener.error(error);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onEvent(int eventType, Bundle params) { ; }

        @Override
        public void onPartialResults(Bundle partialResults) {
            try {
                m_UserSpecifiedListener.partialResults(partialResults);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onReadyForSpeech(Bundle params) {
            try {
                m_UserSpecifiedListener.readyForSpeech(params);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onResults(Bundle results) {
            try {
                m_UserSpecifiedListener.results(results);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onRmsChanged(float rmsdB) {
            try {
                m_UserSpecifiedListener.rmsChanged(rmsdB);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

}

我使用以下活动启动和停止服务。

public class VoiceServiceStarterActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Button startButton = new Button(this);
        startButton.setText("Start the service");
        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { startVoiceService(); }
        });
        Button stopButton = new Button(this);
        stopButton.setText("Stop the service");
        stopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { stopVoiceService(); }
        });
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.addView(startButton);
        layout.addView(stopButton);
        setContentView(layout);
    }

    private void startVoiceService() {
        startService(new Intent(this, SimpleVoiceService.class));
    }

    private void stopVoiceService() {
        stopService(new Intent(this, SimpleVoiceService.class));
    }
}

最后,我在AndroidManifest.xml上声明了我的服务(请参阅Android SDK文件夹中的VoiceRecognition示例)。

<service android:name="SimpleVoiceService"
         android:label="@string/service_name" >

    <intent-filter>
        <action android:name="android.speech.RecognitionService" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</service>

然后我在Android设备上安装了此应用程序并启动它:   - 当我启动服务时,它正常启动;   - 当我停下来时,它会正常停止。

但是如果我在另一个活动中启动以下代码,activities List只包含一个元素,这是默认的语音识别器。

PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(
            new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);

为什么我的语音识别器不会在系统中的人员中返回?

2 个答案:

答案 0 :(得分:7)

如果您希望queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0)提取您的活动(VoiceServiceStarterActivity),那么您必须在应用的清单中声明此活动处理RecognizerIntent.ACTION_RECOGNIZE_SPEECH,就像这样

<activity android:name="VoiceServiceStarterActivity">
  <intent-filter>
    <action android:name="android.speech.action.RECOGNIZE_SPEECH" />
    <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
  ...
</activity>

对于更具体的代码,请查看项目Kõnelesource code),它本质上是一个开源的接口实现,通过它在Android上提供语音识别,即它涵盖:

  • ACTION_RECOGNIZE_SPEECH
  • ACTION_WEB_SEARCH
  • RecognitionService

并使用开源语音识别服务器。

答案 1 :(得分:0)

是的,你需要使用 createSpeechRecognizer(Context context,ComponentName serviceComponent)