2012-04-03 3 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)); 
    } 
} 

마지막으로 나는 AndroidManifest.xml에 내 서비스 (안드로이드 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> 

은 그럼 안드로이드 장치에 대한이 응용 프로그램을 설치하고 나는 그것을 시작 : 를 - 내가 서비스를 시작할 때 제대로 시작; - 중지하면 제대로 중지됩니다.

그러나 다른 활동에서 다음 코드를 실행하면 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 확장 인식 서비스를 지금 사용할 수 있습니까? –

+1

SimpleVoiceSearch 서비스를 시작할 때 onStartListening이 시작 되었습니까? @ enzom83 –

답변

7

당신이 당신의 활동 (VoiceServiceStarterActivity)을 데리러 queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0)을 원하는 경우에 당신은,이 활동은 RecognizerIntent.ACTION_RECOGNIZE_SPEECH를 처리하는 앱의 매니페스트에 선언해야

  • ACTION_RECOGNIZE_을 : 본질적으로 음성 인식이 안드로이드 통해 제공되는 인터페이스의 오픈 소스 구현 Kõnele (source code)는이를 커버 즉 연설
  • 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에서 다운로드)는 "Transcribing"에 머물러 있습니다. .. "*. 코드의 구조 (3 개의 다른 패키지)에 대해 더 자세히 알 수 있고 * 왜 * 그것이 구현되는 방식입니까? 감사! – ripopenid

0

예,이 '이제 구글을'기본 RecognitionService를 오버라이드 (override) 할 수있게 하는가 createSpeechRecognizer (컨텍스트 컨텍스트, ComponentName serviceComponent)를