2012-04-03 67 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服务(见Android 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> 

然后我安装在Android设备上这个应用程序,我启动它: - 当我启动该服务,它正常启动; - 当我停下来时,它会正常停止。

但是,如果我在另一活动中启动以下代码,则activitiesList仅包含一个元素,它是默认的语音识别器。

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

为什么我的语音识别器不能在系统中存在的人之间返回?

+1

这是否让我们重写“谷歌现在”默认RecognitionService?我的意思是,我现在可以将我自己的扩展识别服务挂钩到Google吗? – 2014-12-09 18:51:09

+1

当您启动SimpleVoiceSearch服务时,您的onStartListening是否已开始工作? @ enzom83 – 2016-10-09 07:34:46

回答

7

如果你想queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0)拿起你的活动(VoiceServiceStarterActivity),那么你必须在你的应用清单声明,此活动处理RecognizerIntent.ACTION_RECOGNIZE_SPEECH,这样

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

对于更具体的代码看一下项目Kõnelesource code),其本质上是经由该语音识别设置Android上的接口的一个开源实现,即,它涵盖:

  • ACTION_RECOGNIZE_语音
  • ACTION_WEB_SEARCH
  • RecognitionService

,并使用开源的语音识别服务器。

+1

我不明白为什么我应该创建一个新的活动。目前我有一个活动('VoiceDemoActivity'),通过'SpeechRecognizer'对象处理默认语音识别器。为了使用自定义语音识别服务,我必须在'createSpeechRecognizer'方法中创建一个新的'SpeechRecognizer'对象来指定一个'ComponentName'对象:我想这个'ComponentName'应该引用自定义语音识别器服务,所以我创建了一个新的'RecognitionService'类。为什么我需要实现另一个可以处理'ACTION_RECOGNIZE_SPEECH'意图的活动? – enzom83 2012-04-05 13:49:40

+2

我改进了一点我的答案,也许它更清晰一点。 – Kaarel 2012-04-05 17:07:47

+0

@Kaarel我真的很喜欢你在Kõnele做的事(尽管我不知道爱沙尼亚语)。我从github下载了源代码,以便通过示例学习如何实现我自己的(仅限英文版),但它不会构建开箱即用,并且发布的应用程序本身(从Google Play下载)陷入*“转录”。 ..“*。我在哪里可以了解更多关于代码结构的知识(3种不同的软件包)和*为什么*它是按照它的方式实现的?谢谢! – ripopenid 2013-11-08 01:46:47

0

是的,你需要使用createSpeechRecognizer(上下文的背景下,组件名称的ServiceComponent)