2011-01-22 5 views
3

私はアンドロイドのデバイスがマイクを持っている場合はそうのように、検出する方法を考え出していると信じて:アンドロイドで音声をテキストに変換する方法を検出するにはどうすればよいですか?

Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
     List<ResolveInfo> speechActivities = packageManager.queryIntentActivities(speechIntent, 0); 
     TextView micAvailView = (TextView) findViewById(R.id.mic_available_flag); 
     if (speechActivities.size() != 0) { //we have a microphone 

     } 
     else { //we do not have a microphones 

     } 

しかし、どのように1は、Androidデバイスが音声テキスト機能を持っているかどうかを検出しますか?または、それを検出するために上記を使用する必要がありますか?その場合、デバイスにマイクがあるかどうかをどのように検出しますか?

ご意見ありがとうございます。

答えて

7

アタッチコードは、実際の音声認識が利用可能であるかどうかを検出するために使用される[1]:MICが存在する場合

 
// Check to see if a recognition activity is present 
PackageManager pm = getPackageManager(); 
List 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"); 
} 

試験するために、単に[で[2]のコードとドキュメントに従います3]、時にマイクが利用できない場合は、IOExceptionが取得する必要があります()を準備呼び出す:[2] http://developer.android.com/guide/topics/media/index.html#capture
[1] http://developer.android.com/resources/articles/speech-input.html

 
recorder = new MediaRecorder(); 
recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
recorder.setOutputFile(path); 
recorder.prepare(); 

[3] http://developer.android.com/reference/android/media/MediaRecorder.AudioSource.html

+1

本当に録音を準備する必要はありますか?私は単にマイクがあるかどうかを見たいと思っています。私はこの段階では全く録音したくありません。 getMicrophoneDevices()メソッドなどはありませんか?そして、音声認識が利用可能であることを確認するためには、それはスピーチのテキストが利用可能であることと同等であるか?また、IOExceptionには、デバイスにマイクが実装されていない以外の理由がいくつかあります。 – Tom

+0

@Tomはい私はここで音声認識のための音声対話を意味しました。 ACTION-RECOGNIZE_SPEECHインテントを処理できるアクティビティが存在する場合、マイクが使用可能であることを保証するものではない場合、基礎となるシステムに尋ねるだけであることに注意してください。 –

+0

@Tom *編集:*ここを見てhttp://stackoverflow.com/questions/4607743/how-to-detect-if-a-microphone-is-present-in-android認識アクティビティが返されるのは、 (おそらくこれはRECORD_AUDIOの許可で暗示されているでしょう) –

1

グイドの答えを読んだ後、これは私が思いついたものです。私には非常にハックに見えますが、もっと良い方法があるといいですね。私はguidoの答えを受け入れるだろうが、良い方法があれば教えてください。

package; 

import java.io.File; 
import java.io.IOException; 
import java.util.List; 

import android.content.Context; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.content.pm.ResolveInfo; 
import android.media.MediaRecorder; 
import android.speech.RecognizerIntent; 

public class MediaUtil { 
    //returns whether a microphone exists 
    public boolean getMicrophoneExists(Context context) {   
      PackageManager packageManager = context.getPackageManager(); 
    return packageManager.hasSystemFeature(PackageManager.FEATURE_MICROPHONE); 
    } 

    //returns whether the microphone is available 
    public static boolean getMicrophoneAvailable(Context context) { 
     MediaRecorder recorder = new MediaRecorder(); 
      recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
     recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); 
     recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); 
     recorder.setOutputFile(new File(context.getCacheDir(), "MediaUtil#micAvailTestFile").getAbsolutePath()); 
     boolean available = true; 
     try { 
      recorder.prepare(); 
     } 
     catch (IOException exception) { 
      available = false; 
     } 
     recorder.release(); 
     return available; 
    } 

    //returns whether text to speech is available 
    public static boolean getTTSAvailable(Context context) { 
     PackageManager packageManager = context.getPackageManager(); 
     Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
     List<ResolveInfo> speechActivities = packageManager.queryIntentActivities(speechIntent, 0); 
     if (speechActivities.size() != 0) return true; 
     return false; 
    } 
} 
関連する問題