没有从自定义语音识别服务获得结果

时间:2014-11-04 20:34:30

标签: android service voice-recognition

我正在尝试运行RecognitionService并在onResults中启动一个活动(在此示例中为skype)。

从我的MainActivity我通过按钮启动服务,这导致调用我的服务类的OnCreate。然后我在手机的Android设置中选择我的服务.........

我不知道如何从这里开始。不调用service-class的onStartListening,也不调用RecognitionListener的onResults。

我是否需要告诉服务我想通过意图使用该服务?我试图这样做,但我猜它不正确,因为错误号码8的onError被调用。如果这样,我是如何正确地做到的?

这是我的服务级别:

public class MyVoiceService extends RecognitionService
{

    private SpeechRecognizer    m_EngineSR;

    @Override
    public void onCreate()
    {
        super.onCreate();

    }

    @Override
    public void onDestroy()
    {

        super.onDestroy();

    }

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

    @Override
    protected void onStartListening(Intent recognizerIntent, Callback listener)
    {
        if (m_EngineSR == null)
        {
            m_EngineSR = SpeechRecognizer.createSpeechRecognizer(this);
            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)
        {
            Log.w("MyVoiceService", "VoiceResultsListener called");

            m_UserSpecifiedListener = userSpecifiedListener;
        }

        @Override
        public void onBeginningOfSpeech()
        {
            Log.w("MyVoiceService", "onBeginningOfSpeech called");

            try
            {
                m_UserSpecifiedListener.beginningOfSpeech();
            }
            catch (RemoteException e)
            {
                e.printStackTrace();
            }
        }

        @Override
        public void onBufferReceived(byte[] buffer)
        {
            Log.w("MyVoiceService", "onBufferReceived called");

            try
            {
                m_UserSpecifiedListener.bufferReceived(buffer);
            }
            catch (RemoteException e)
            {
                e.printStackTrace();
            }
        }

        @Override
        public void onEndOfSpeech()
        {
            Log.w("MyVoiceService", "onEndOfSpeech called");

            try
            {
                m_UserSpecifiedListener.endOfSpeech();
            }
            catch (RemoteException e)
            {
                e.printStackTrace();
            }
        }

        @Override
        public void onError(int error)
        {
            Log.w("MyVoiceService", "onError called");

            try
            {
                m_UserSpecifiedListener.error(error);
            }
            catch (RemoteException e)
            {
                e.printStackTrace();
            }
        }

        @Override
        public void onEvent(int eventType, Bundle params)
        {
            Log.w("MyVoiceService", "onEvent called");
        }

        @Override
        public void onPartialResults(Bundle partialResults)
        {
            Log.w("MyVoiceService", "onPartialResults called");

            try
            {
                m_UserSpecifiedListener.partialResults(partialResults);
            }
            catch (RemoteException e)
            {
                e.printStackTrace();
            }
        }

        @Override
        public void onReadyForSpeech(Bundle params)
        {
            Log.w("MyVoiceService", "onReadyForSpeech called");

            try
            {
                m_UserSpecifiedListener.readyForSpeech(params);
            }
            catch (RemoteException e)
            {
                e.printStackTrace();
            }
        }

        @Override
        public void onResults(Bundle results)
        {
            Log.w("MyVoiceService", "onResults called");


            //Start skype:
            String name = "com.skype.raider.Main";
            String packageName = "com.skype.raider";

            ComponentName componentName = new ComponentName(packageName, name);
            Intent intent = new Intent(Intent.ACTION_MAIN);

            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            intent.setComponent(componentName);
            int tmp = 0;
            startActivity(intent);

            try
            {
                m_UserSpecifiedListener.results(results);
            }
            catch (RemoteException e)
            {
                e.printStackTrace();
            }
        }

        @Override
        public void onRmsChanged(float rmsdB)
        {
            Log.w("MyVoiceService", "onRmsChanged called");
            try
            {
                m_UserSpecifiedListener.rmsChanged(rmsdB);
            }
            catch (RemoteException e)
            {
                e.printStackTrace();
            }
        }
    }

}

My Manifest看起来像这样:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.voicerecognitiondemo2"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.RECORD_AUDIO" />

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.voicerecognitiondemo2.MainActivity"
            android:label="@string/app_name" >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

        </activity>
        <activity android:name="Activity1" >
        </activity>
        <activity android:name="Activity2" >
        </activity>

        <service
            android:name="MyVoiceService"
            android:label="@string/service_name" 
             android:permission="android.permission.RECORD_AUDIO" >
            <intent-filter>
                <action android:name="android.speech.RecognitionService" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </service>
    </application>

</manifest>

在我的MainActivity中,我这样做:

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


}

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

开始/停止服务。

提前谢谢大家!!

0 个答案:

没有答案