2012-05-07 8 views
2

Googleが提供するアクティビティvoicerecognition.javaを使用して、ユーザーが言語を選択し、ハンドセットに向かって話し、彼らが言ったことを見ることができます。それは私がすべての言語定数を取り出すときに機能しますが、言語オプションを入れるとエラーになります。たとえば は:認識器の目的は定数を定義できません

  • 方法getVoiceDetailsIntent(VoiceRecognition)は
  • EXTRA_SUPPORTED_LANGUAGESを解決できないタイプのために未定義であるか、またはEXTRA_LANGUAGE_PREFERENCEを解決できない
  • フィールドないか、またはフィールド
  • 方法ではありません新しい型のrun()Runnable(){}はスーパークラスメソッドをオーバーライドする必要があります

私はSDKやそれ以外のものを更新する必要があるかもしれないと思っていました。すべて最新です。私は、提供されたリンクとまったく同じようにアクティビティをコピーしてから、何が間違っているのか分かりません。

package com.sample.voicerecog; 

import android.app.Activity; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.content.pm.ResolveInfo; 
import android.os.Bundle; 
import android.os.Handler; 
import android.speech.RecognizerIntent; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.Spinner; 
import android.widget.SpinnerAdapter; 
import android.widget.TextView; 
import android.widget.Toast; 
import java.util.ArrayList; 
import java.util.List; 

public class VoiceRecognition extends Activity implements OnClickListener {  
private static final String TAG = "VoiceRecognition";  
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;  
private ListView mList;  
private Handler mHandler;  
private Spinner mSupportedLanguageView;  

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

    // 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);   
    mSupportedLanguageView = (Spinner) findViewById(R.id.supported_languages);   
    // 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"); 
    }  

    // Most of the applications do not have to handle the voice settings. If the application  
    // does not require a recognition in a specific language (i.e., different from the system  
    // locale), the application does not need to read the voice settings.  
    refreshVoiceSettings(); 
} 

/** 
* 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);   

    // Display an hint to the user about what he should say.  
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");  

    // Given an hint to the recognizer about what the user is going to say  
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,    
      RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);  

    // Specify how many results you want to receive. The results will be sorted 
    // where the first result is the one with higher confidence.  
    intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5); 

    // Specify the recognition language. This parameter has to be specified only if the  
    // recognition has to be done in a specific language and not the default one (i.e., the 
    // system locale). Most of the applications do not have to set this parameter. 
    if (!mSupportedLanguageView.getSelectedItem().toString().equals("Default")) {  
     intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,     
       mSupportedLanguageView.getSelectedItem().toString()); 
    }  
    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); 
} 

private void refreshVoiceSettings() {  
    Log.i(TAG, "Sending broadcast");  
    sendOrderedBroadcast(RecognizerIntent.getVoiceDetailsIntent(this), null, 
      new SupportedLanguageBroadcastReceiver(), null, Activity.RESULT_OK, null, null); 
} 

private void updateSupportedLanguages(List<String> languages) {  
    // We add "Default" at the beginning of the list to simulate default language. 
    languages.add(0, "Default"); 

    SpinnerAdapter adapter = new ArrayAdapter<CharSequence>(this,  
      android.R.layout.simple_spinner_item, languages.toArray( 
        new String[languages.size()])); 
    mSupportedLanguageView.setAdapter(adapter); 
} 

private void updateLanguagePreference(String language) {  
    TextView textView = (TextView) findViewById(R.id.language_preference);  
    textView.setText(language); 
} 

/**  
* Handles the response of the broadcast request about the recognizer supported languages.  
*  
* The receiver is required only if the application wants to do recognition in a specific language.  
*/ 

private class SupportedLanguageBroadcastReceiver extends BroadcastReceiver {  
    @Override 
    public void onReceive(Context context, final Intent intent) {  
     Log.i(TAG, "Receiving broadcast " + intent); 
     final Bundle extra = getResultExtras(false); 
     if (getResultCode() != Activity.RESULT_OK) {  
      mHandler.post(new Runnable() {  
       @Override   
       public void run() {    
        showToast("Error code:" + getResultCode());  
       }  
      }); 
     } 
     if (extra == null) {   
      mHandler.post(new Runnable() {   
       @Override    
       public void run() { 
        showToast("No extra");  
       } 
      }); 
     }  
     if (extra.containsKey(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES)) {   
      mHandler.post(new Runnable() {  
       @Override  
       public void run() {   
        updateSupportedLanguages(extra.getStringArrayList(  
          RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES));  
       } 
      }); 
     }  
     if (extra.containsKey(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE)) {  
      mHandler.post(new Runnable() {   
       @Override  
       public void run() {   
        updateLanguagePreference(  
          extra.getString(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE));  
       } 
      }); 
     } 
    } 

    private void showToast(String text) {  
     Toast.makeText(VoiceRecognition.this, text, 1000).show(); 
    } 
} 
} 

すべてのヘルプは

+0

ここにコードを表示できますか? – MByD

+0

どのようなAPIレベルですか? Android Tools - >プロジェクトプロパティを修正しようとしましたか?あなたが言及した機能は、APIレベル8からのみ利用可能です。あなたのプロジェクトがこのAPIレベルにあることを確認してください。 – Jeshurun

+0

レベル4を使用しています。プロジェクトのプロパティを修正しようとしました。 – user1255273

答えて

1

を高く評価している右、プロジェクトを右クリックし、プロパティをクリックして、あなたのプロジェクトはEclipseを使用している場合は、この問題を修正するにはレベル8より低いAPIレベルであることが表示されます。左側のメニューからAndroidを選択し、別のプロジェクトビルドターゲットを選択します。レベル8以上で見つからない場合は、ウィンドウ - > Android SDKマネージャーをクリックしてインストールしてください。 RecognizerIntentのどの機能がさまざまなAPIレベルhereで利用可能であるかを確認できます。

+0

ありがとう!しかし、すべてのrun()はまだエラーを表示しています。 Eclipseは、オーバーライドを削除することを提案しています。それを行うべきか、それとも別の解決策がありますか?また、ハンドセットがレベル8以上で言語オプションが使用可能な場合はAPIレベル8を使用し、ハンドセットが8未満で言語選択を提供しない場合はAPIレベル8未満を使用する方法がありますか? (文章を実行して申し訳ありません) – user1255273

+0

[this](http://groups.google.com/group/android-qt/browse_thread/thread/fc9c3830e3582db8)のリンクで、オーバーライドアノテーションのコンパイラエラーを参照してください。はい、Build.VERSIONクラスを使用してランタイム時のレベルを判断し、それに応じてUIを変更することができます。詳細については、[this](http://stackoverflow.com/questions/2768806/how-to-obtain-the-android-api-level-of-a-vice)リンクを参照してください。 – Jeshurun

+0

JRE 1.7を使用しています。解決策は1.5から1.6になりました。私は1.6にしようとしたが、うまくいかなかった。 1.7に戻ってもまだ動作しません。 – user1255273

関連する問題