2012-04-03 14 views
7

私は簡単な音声認識サービスを作成しました。この目的のために、私は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)); 
    } 
} 

最後に、私は(AndroidのSDKフォルダ内VoiceRecognitionサンプルを参照してください)AndroidManifest.xmlに私のサービスを宣言しました。

<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デバイス上でこのアプリケーションをインストールし、私はそれを起動します。 - 私はサービスを開始すると、それが正常に起動します。 - 停止すると正しく停止します。

しかし、別のアクティビティで次のコードを起動した場合、activitiesListには、デフォルトの音声認識プログラムである要素のみが含まれています。

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

なぜ私の音声認識装置はシステムに存在するものの中に返されませんか?より具体的なコードの場合、この

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

がプロジェクトを見ているよう

+1

これは 'Google Nowの' デフォルトRecognitionServiceを上書きするために私達を可能にしていますか?つまり、自分の拡張認定サービスをGoogleに引き継ぐことはできますか? –

+1

SimpleVoiceSearchサービスの開始時にonStartListeningが起動しましたか? @ enzom83 –

答えて

7

あなたがあなたの活動(VoiceServiceStarterActivity)をピックアップするqueryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0)をしたいなら、あなたは、この活動はRecognizerIntent.ACTION_RECOGNIZE_SPEECHを処理する、アプリのマニフェストで宣言する必要があり

  • ACTION_RECOGNIZE_を:それがカバーする、すなわち、本質的に音声認識をアンドロイドに設けられた介してインターフェースのオープンソース実装であるKõnelesource code) SPEECH
  • ACTION_WEB_SEARCH
  • RecognitionService

とオープンソースの音声認識サーバを使用しています。

+1

なぜ私は新しい活動を作成すべきか分かりません。現在、私は 'SpeechRecognizer'オブジェクトを通してデフォルトの音声認識器を扱うアクティビティ(' VoiceDemoActivity')を持っています。カスタム音声認識サービスを使用するためには、 'createSpeechRecognizer'メソッドで' ComponentName'オブジェクトを指定して新しい 'SpeechRecognizer'オブジェクトを作成する必要があります。この' ComponentName'はカスタム音声認識サービスを参照する必要があります。私は新しい 'RecognitionService'クラスを作成しました。'ACTION_RECOGNIZE_SPEECH'インテントを扱える別のアクティビティを実装する必要があるのはなぜですか? – enzom83

+2

私は自分の答えを少し改善した、多分それはもう少し明確にした。 – Kaarel

+0

@Kaarel私はあなたがKõneleでしたことが本当に好きです(私はエストニア語を知らないが)。私はgithubからソースコードをダウンロードして、自分自身(英語のみ)を実装する方法を学びましたが、それは箱から出ず、リリースされたアプリ自体(Google Playからダウンロードされました)は*「転記」に固執します。 .. "*。あなたのコードの構造(3つの異なるパッケージ)と*なぜそれが実装されているのかについてもっと知ることができますか?ありがとう! – ripopenid

0

はい、あなたはcreateSpeechRecognizer(コンテキストのコンテキスト、コンポーネント名serviceComponent)を使用する必要が