2017-07-29 84 views
0

我有识别来自Android操作系统的人的声音和语音转换成文本的应用程序的想法。我在前台完成了这个过程。但是我需要在背景中实现它。Android的语音识别中的背景,并转换为文本

对于这一点,我想办法。我的方法是获取活动背景状态,然后从那里调用服务。在那之后,每当应用程序将在每次调用服务时都处于后台。 我不知道如何实现这一点。

我的前景源代码作为

private void promptSpeechInput(){ 
       //----takes user input----// 
       Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
       //----which language users will speak 
       intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
         RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
       intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault()); 
       //----- 
       intent.putExtra(RecognizerIntent.EXTRA_PROMPT, 
         getString(R.string.speech_prompt)); 
       try { 
        startActivityForResult(intent, REQ_CODE_SPEECH_INPUT); 
       } catch (ActivityNotFoundException a) { 
        Toast.makeText(getApplicationContext(), 
          getString(R.string.speech_not_supported), 
          Toast.LENGTH_SHORT).show(); 
       } 
      } 
    /** 
    * Receiving speech input 
    * */ 

@Override 保护无效onActivityResult(INT requestCode,INT发送resultCode,意图数据){ super.onActivityResult(requestCode,resultCode为,数据);

switch (requestCode) { 
     case REQ_CODE_SPEECH_INPUT: { 
      if (resultCode == RESULT_OK && null != data) { 
      ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
        txtSpeechInput.setText(result.get(0)); 
      } 
      break; 
     } 

    } 
} 

调用从活动相关的服务

@Override 
     protected void onPause(){ 
      super.onPause(); 
      Log.e("Application background", "Background"); 
      //-----Create Service From here and identify the state of background 
      Intent serviceIntent = new Intent(this,BackgroundService.class); 
      startService(serviceIntent); 
    } 

BackgroundService.java

public class BackgroundService extends Service { 
    private SpeechRecognizer speech; 


    @Nullable 
    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 
    @Override 
    public void onCreate() { 
     Log.e("servicecreate", "service"); 
     // TODO Auto-generated method stub 
     super.onCreate(); 
     Toast.makeText(this, "OnCreate()", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onDestroy() { 
     Log.e("servicedestroy", "service"); 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
     Toast.makeText(this, "OnDestroy()", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Log.e("servicecstart", "service"); 
    } 
} 

回答

0

cmusphinx是一个非常不错的选择

+0

Link是在上述网站本身提供的,反正这里是直接链接https://github.com/cmusphinx/pocketsphinx-android-demo – Mohan

+0

HT TPS://cmusphinx.github.io/wiki/tutorialandroid/此链接包含了应用https://github.com/cmusphinx/pocketsphinx-android-demo – Mohan

+0

难道ü运行代码的教程一步一步 – Mohan