Don't get results from custom voice - RecognitionService

I am trying to start RecognitionService and get started with onResults (skype in this example).

In my MainActivity, I start a service with a button, which results in a call to OnCreate of my service class. Then I choose my service in Android settings on my phone .........

I don't know how it goes. onStartListening service-class is not called and onResults of RecognitionListener is not called either.

Do I need to inform the service that I want to use the service through intent? I tried to do this, but I think it was wrong because it raised onError with error number 8. How, if so, am I doing it right?

this is my service class:

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 looks like this:

    <?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>

      

in my MainActivity I am doing this:

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


}

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

      

to start / stop the service.

Thanks everyone in advance!

+3


source to share





All Articles