2017-04-26 59 views
1

我編碼的簡單語音識別應用程序無法按預期工作。基本上,日誌上沒有可見的錯誤,編譯也很好,但是當我點擊按鈕時,語音識別功能彈出並開始錄製,但錄製完成後,沒有任何應用程序屏幕上顯示listview應顯示的內容無論記錄(以文本格式)Android上的簡單語音識別應用程序無法按預期工作

這裏是Java代碼

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.speech.RecognizerIntent; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 

import java.util.ArrayList; 


public class Voice extends Activity implements View.OnClickListener { 

    ListView lv; 
    final static int check = 1111; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.voice); 
     lv = (ListView) findViewById(R.id.lvVoiceReturn); 
     Button b = (Button) findViewById(R.id.bVoice); 
     b.setOnClickListener(this); 
    } 



    @Override 
    public void onClick(View v) { 
     Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);  // set speech recognizer intent 
     i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); //set language 
     i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak up son!");    // set prompt to user 
     startActivityForResult(i, check); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (requestCode == check && resultCode == RESULT_OK) { 
      // create an empty array list and link it to the recognizer intent 
      ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
      // link the listview from my layout to the arraylist I created just now with the results from voice recognition 
      lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results)); 
     } 

    } 
} 

這裏是XML佈局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <Button 
     android:id="@+id/bVoice" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Click to Speak" /> 

    <ListView 
     android:id="@+id/lvVoiceReturn" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" /> 
</LinearLayout> 

謝謝!

回答

0

@Kalid可以嘗試設置爲intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,「en-US」)的語言集;並使用調試或日誌檢查,如果應用程序控制去onActivityResult

+0

愚蠢的錯誤。我沒有連接到互聯網。該應用程序現在可以工作,並且我認爲語音識別需要連接。但是,感謝Aditi! – Kalid