2011-08-31 70 views
0

我是很新的Android開發,我想創建一個基本的應用程序,它利用了VoiceRecognition設施,以顯示所說的文本...無法實現谷歌API樣品(VoiceRecognition)

package com.example.android.apis.app; 

import com.example.android.apis.R; 

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

import java.util.ArrayList; 
import java.util.List; 

/** 
* Sample code that invokes the speech recognition intent API. 
*/ 
public class VoiceRecognition extends Activity implements OnClickListener { 

    private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234; 

    private ListView mList; 

    /** 
    * Called with the activity is first created. 
    */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // Inflate our UI from its XML layout description. 
     setContentView(R.layout.voice_recognition); 

     // Get display items for later interaction 
     Button speakButton = (Button) findViewById(R.id.btn_speak); 

     mList = (ListView) findViewById(R.id.list); 

     // Check to see if a recognition activity is present 
     PackageManager pm = getPackageManager(); 
     List<ResolveInfo> activities = pm.queryIntentActivities(
       new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0); 
     if (activities.size() != 0) { 
      speakButton.setOnClickListener(this); 
     } else { 
      speakButton.setEnabled(false); 
      speakButton.setText("Recognizer not present"); 
     } 
    } 

    /** 
    * Handle the click on the start recognition button. 
    */ 
    public void onClick(View v) { 
     if (v.getId() == R.id.btn_speak) { 
      startVoiceRecognitionActivity(); 
     } 
    } 

    /** 
    * Fire an intent to start the speech recognition activity. 
    */ 
    private void startVoiceRecognitionActivity() { 
     Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
     intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
       RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
     intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo"); 
     startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE); 
    } 

    /** 
    * Handle the results from the recognition activity. 
    */ 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) { 
      // Fill the list view with the strings the recognizer thought it could have heard 
      ArrayList<String> matches = data.getStringArrayListExtra(
        RecognizerIntent.EXTRA_RESULTS); 
      mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 
        matches)); 
     } 

     super.onActivityResult(requestCode, resultCode, data); 
    } 
} 

我創建了谷歌提供的VoiceRecognition代碼的類:

它經歷了千百年來編譯,因爲它不停地抱怨「com.example.android.apis.R」是無法得到解決......我進口在API中提供相關的XML文件並將其放入main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
... 
--> 

<!-- This activity displays UI for launching voice recognition --> 

<LinearLayout xmlns:android="......." 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingBottom="4dip" 
     android:text="@string/voice_recognition_prompt" /> 

    <Button android:id="@+id/btn_speak" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/speak_button" /> 

    <ListView android:id="@+id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" /> 

</LinearLayout> 

然後我將voice_recognition_prompt和speak_button字符串添加到值文件中(這對我來說都是新的,我不確定是否按照正確的順序完成了這些操作)。

最後,該應用程序編譯,但它只是給出了強制關閉錯誤信息。有沒有我可能錯過的特定步驟?一些特定的項目配置使用這個Google API示例?

我真的很感謝你的幫助。

+0

你好亞歷克斯,檢查我的答案,如果它是真的,那麼馬克是正確的。 –

回答

0

你好亞歷克斯請使用下面的XML文件: -

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:paddingBottom="4dip" 
     android:text="prompt" /> 

    <Button android:id="@+id/btn_speak" android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:text="speak" /> 

    <ListView android:id="@+id/list" android:layout_width="fill_parent" 
     android:layout_height="0dip" android:layout_weight="1" /> 
</LinearLayout> 

再檢查,你的部隊密切錯誤就解決了。

+0

您的XML文件的確解決了這個問題。非常感謝你。該應用程序在模擬器上無法正常工作,說「識別器不存在」,但我聽到其他人提到這一點....但如果部署在安裝了Google語音搜索的手機上,它確實工作並打印出口語。 ...再次感謝Dipak! – Alex