2011-01-22 4 views
3
내가 알아 낸 생각

방법과 같이, 감지 :android에서 텍스트를 음성으로 사용할 수 있는지 확인하는 방법은 무엇입니까? 안드로이드 장치가 마이크가있는 경우

그러나
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] 사용할 수있는 경우

답변

7

당신이 부착 된 코드는 실제로 감지하는 데 사용됩니다 :

 
// 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를 얻어야한다 :

 
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(); 

[1] http://developer.android.com/resources/articles/speech-input.html
[2] http://developer.android.com/guide/topics/media/index.html#capture
[3]

+1

녹음을 준비해야합니까? 마이크가 있는지보고 싶을뿐입니다.이 단계에서 녹음하고 싶지 않습니다. getMicrophoneDevices() 메소드 나 다른 것이 없습니까? 확인을 위해 오디오 인식이 가능하다면 그것은 스피치와 텍스트가 사용 가능하다는 것과 같습니다. 또한, IOException은 장치에 마이크가 구현되지 않은 것 외에도 여러 가지 이유가있을 수 있습니다. – Tom

+0

@ 톰 그렇습니다. 음성 인식을위한 음성 - 텍스트를 의미합니다. 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; 
    } 
} 
관련 문제