2017-07-29 61 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