【问题标题】:How Do I implement voice recognition to launch application?如何实现语音识别来启动应用程序?
【发布时间】:2014-10-27 21:21:45
【问题描述】:

我是 android 开发的菜鸟,我正在尝试找出一种使用语音识别来启动我的应用程序的方法。我想在用户首次登录后启动一个在后台运行的 RecognitionService。我希望服务持续运行并允许用户使用关键字启动应用程序。我将允许用户在设置活动中停止服务。目前,我能够启动服务并使其在后台运行,但我无法弄清楚如何在 RecognitionService 中调用 onStartListening 方法。非常感谢任何解决此问题的帮助。

我的代码

 public class VoiceRecognitionService extends RecognitionService {

private SpeechRecognizer m_EngineSR;

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

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

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

@Override
protected void onStartListening(Intent recognizerIntent, Callback listener) { //<--How Do I call this method?
    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);
            Log.e("SimpleVoiceService Results:", "Service stopped");

            ArrayList<String> result = results.getStringArrayList("results_recognition");

            Log.e("SimpleVoiceService Results:", result.get(0));
            if (result.isEmpty() == false) {
                for (int i = 0; i<(result.size()-1);i++) {
                    if(result.get(i).equals("shoot back")||result.get(i).equals("Launch App")){
                        Intent intent2 = new Intent("com.bmoney.testapp.MAIN_ACTIVITY");
                        startActivity(intent2);
                        break;

                    }
                }
            }



        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

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

}

清单

  <service android:name="com.bmoney.testapp.VoiceRecognitionService" android:exported="true" android:enabled="true">
        <intent-filter>
            <action android:name="android.speech.RecognitionService" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </service>

【问题讨论】:

    标签: android service listener speech-recognition voice-recognition


    【解决方案1】:

    虽然我没有机会对此进行测试,但我认为答案在于this android 开发者博客文章。
    这个实现虽然使用“Ok Google”(我不知道这是否会打扰你)。

    简而言之,您必须实现 seachable activity 并将其添加到您的 AndroidManifest 中:

    <activity android:name=".SearchableActivity">
        <intent-filter>
            <action android:name="com.google.android.gms.actions.SEARCH_ACTION"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>
    

    【讨论】:

    • 这对我不起作用。我已经对所有内容进行了三重检查,但无法将此搜索查询传递到我的应用程序。有没有人让这个工作?
    • 嗯...看起来该应用程序需要在 Play 商店上发布:plus.google.com/+AndroidDevelopers/posts/afSRdDQiy1N - 寻找 Jarek Wilkiewicz 的 cmets。接下来要试试这个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-13
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多