2016-05-29 6 views
1

こんにちはアンドロイドデベロッパーANDROID.Googleポップアップを表示せずにテキストに音声入力する方法

私はアンドロイド開発の初心者です。この問題に固執しました -

私は音声入力をしたいですが、私はGoogleポップアップを表示したくありません。私は音声入力がない複数のアプリを見てきました。だから多分誰かがそれを理解するのを助けることができますか?

ここでここでポップアップenter image description here

のスクリーンショットは、MainActivity

の私のコードです

パブリッククラスMainActivityはAppCompatActivity {

private TextView resultTV; 

/** 
* ATTENTION: This was auto-generated to implement the App Indexing API. 
* See https://g.co/AppIndexing/AndroidStudio for more information. 
*/ 
private GoogleApiClient client; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    // ATTENTION: This was auto-generated to implement the App Indexing API. 
    // See https://g.co/AppIndexing/AndroidStudio for more information. 
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
    resultTV = (TextView) findViewById(R.id.resultTV); 
} 


public void onButtonClick(View view) { 

    if (view.getId() == R.id.imageButton) { 

     promtSpeechInput(); 

    } 


} 

public void promtSpeechInput() { 

    Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
     i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
     i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault()); 
     i.putExtra(RecognizerIntent.EXTRA_PROMPT, "SAY SOMETHING"); 
     i.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true); 



    try { 

     startActivityForResult(i, 100); 
     Toast.makeText(MainActivity.this, "Say something kiddo", Toast.LENGTH_LONG).show(); 



    } catch (Exception e) { 
     Toast.makeText(MainActivity.this, "Sorry, your device not support speech inputs", Toast.LENGTH_LONG).show(); 
     e.printStackTrace(); 
    } 


} 


public void onActivityResult(int request_code, int result_code, Intent i){ 


     super.onActivityResult(request_code,result_code,i); 

     switch (request_code) 
     { 

      case 100: if(result_code == RESULT_OK && i != null){ 
       ArrayList<String> result = i.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 

       resultTV.setText(result.get(0)); 

      } 
      break; 

     } 

答えて

1

このコードを試してみてください拡張します私が作成したアクティビティは、いつ切り替えるのですか?トンはYOUR INこの権限を追加すること忘れないRmsChanged()メソッドを使用してプログレスバーに表示され、認識されたテキストが

public class MainActivity extends Activity implements RecognitionListener 
{ 
private TextView returnedText; 
private ToggleButton toggleButton; 
private ProgressBar progressBar; 
private SpeechRecognizer speech = null; 
private Intent recognizerIntent; 
private String LOG_TAG = "VoiceRecognitionActivity"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    returnedText = (TextView) findViewById(R.id.textView1); 
    progressBar = (ProgressBar) findViewById(R.id.progressBar1); 
    toggleButton = (ToggleButton) findViewById(R.id.toggleButton1); 
    Button recordbtn = (Button) findViewById(R.id.mainButton); 


    progressBar.setVisibility(View.INVISIBLE); 
    speech = SpeechRecognizer.createSpeechRecognizer(this); 
    speech.setRecognitionListener(this); 
    recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, 
           "en"); 
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, 
           this.getPackageName()); 
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
           RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH); 
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true); 
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 3000); 

    toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, 
             boolean isChecked) { 
       if (isChecked) { 
        progressBar.setVisibility(View.VISIBLE); 
        progressBar.setIndeterminate(true); 
        speech.startListening(recognizerIntent); 
       } else { 
        progressBar.setIndeterminate(false); 
        progressBar.setVisibility(View.INVISIBLE); 
        speech.stopListening(); 
       } 
      } 
     }); 






} 

@Override 
public void onResume() { 
    super.onResume(); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    if (speech != null) { 
     speech.destroy(); 
     Log.i(LOG_TAG, "destroy"); 
    } 

} 

@Override 
public void onBeginningOfSpeech() { 
    Log.i(LOG_TAG, "onBeginningOfSpeech"); 
    progressBar.setIndeterminate(false); 
    progressBar.setMax(10); 
} 

@Override 
public void onBufferReceived(byte[] buffer) { 
    Log.i(LOG_TAG, "onBufferReceived: " + buffer); 
} 

@Override 
public void onEndOfSpeech() { 
    Log.i(LOG_TAG, "onEndOfSpeech"); 
    progressBar.setIndeterminate(true); 
    toggleButton.setChecked(false); 
} 

@Override 
public void onError(int errorCode) { 
    String errorMessage = getErrorText(errorCode); 
    Log.d(LOG_TAG, "FAILED " + errorMessage); 
    returnedText.setText(errorMessage); 
    toggleButton.setChecked(false); 
} 

@Override 
public void onEvent(int arg0, Bundle arg1) { 
    Log.i(LOG_TAG, "onEvent"); 
} 

@Override 
public void onPartialResults(Bundle arg0) { 
    Log.i(LOG_TAG, "onPartialResults"); 
ArrayList<String> matches = arg0 
     .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); 
    String text = ""; 
    for (String result : matches) 
     text += result + "\n"; 

    returnedText.setText(text); 


} 

@Override 
public void onReadyForSpeech(Bundle arg0) { 
    Log.i(LOG_TAG, "onReadyForSpeech"); 
} 

@Override 
public void onResults(Bundle results) { 
    Log.i(LOG_TAG, "onResults"); 

} 

@Override 
public void onRmsChanged(float rmsdB) { 
    Log.i(LOG_TAG, "onRmsChanged: " + rmsdB); 
    progressBar.setProgress((int) rmsdB); 

} 

public static String getErrorText(int errorCode) { 
    String message; 
    switch (errorCode) { 
     case SpeechRecognizer.ERROR_AUDIO: 
      message = "Audio recording error"; 
      break; 
     case SpeechRecognizer.ERROR_CLIENT: 
      message = "Client side error"; 
      break; 
     case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS: 
      message = "Insufficient permissions"; 
      break; 
     case SpeechRecognizer.ERROR_NETWORK: 
      message = "Network error"; 
      break; 
     case SpeechRecognizer.ERROR_NETWORK_TIMEOUT: 
      message = "Network timeout"; 
      break; 
     case SpeechRecognizer.ERROR_NO_MATCH: 
      message = "No match"; 
      break; 
     case SpeechRecognizer.ERROR_RECOGNIZER_BUSY: 
      message = "RecognitionService busy"; 
      break; 
     case SpeechRecognizer.ERROR_SERVER: 
      message = "error from server"; 
      break; 
     case SpeechRecognizer.ERROR_SPEECH_TIMEOUT: 
      message = "No speech input"; 
      break; 
     default: 
      message = "Didn't understand, please try again."; 
      break; 
    } 
    return message; 
} 




    } 

のTextViewに示されている音声のピッチで記録を開始し、変動に切り替えられましたMANIFEST

<uses-permission android:name="android.permission.RECORD_AUDIO" /> 
+0

ありがとうございます。まだ動作していない、あなたはアンドロイドのマニフェストパーミッションを共有していただけますか? :) –

+0

@VaclovasRekašiusJr。私はちょうど編集して、今答えの必要な許可を追加しました,,,あなたはそれを確認することができます – user5894647

+0

@VaclovasRekašiusJr。そして、私の仕事が – user5894647

1

はこれを使用してみてください

SpeechRecognizer recognize=SpeechRecognizer.createSpeechRecognizer(this); 
    recognize.setRecognitionListener(new RecognitionListener() { 
     @Override 
     public void onReadyForSpeech(Bundle bundle) { 

     } 

     @Override 
     public void onBeginningOfSpeech() { 

     } 

     @Override 
     public void onRmsChanged(float v) { 

     } 

     @Override 
     public void onBufferReceived(byte[] bytes) { 

     } 

     @Override 
     public void onEndOfSpeech() { 

     } 

     @Override 
     public void onError(int i) { 
      Toast.makeText(getApplicationContext(),"ERROR",Toast.LENGTH_LONG).show(); 
     } 

     @Override 
     public void onResults(Bundle bundle) { 
      String output = bundle.getString(SpeechRecognizer.RESULTS_RECOGNITION); 
      Toast.makeText(getApplicationContext(),output,Toast.LENGTH_LONG).show(); 
     } 

     @Override 
     public void onPartialResults(Bundle bundle) { 

     } 

     @Override 
     public void onEvent(int i, Bundle bundle) { 

     } 
    }); 
recognize.startListening(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)); 
+0

このコードはどこに置く必要がありますか教えてください。 –

+1

promtSpeechInput()メソッド内に記述してください –

関連する問題