开发者

How to detect if speech to text is available on android?

I believe I have figured out how to detect if an android device has a microphone, like so:

Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        List<ResolveInfo> speechActivities = packageManager.queryInte开发者_开发百科ntActivities(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

        }

However, how does one detect whether the android device has speech-to-text capability? Or should the above be used to detect that? If so, how does one detect if the device has a microphone?

Any feedback is appreciated, thanks.


The code you attached is indeed used to detect if audio recognition is available [1]:

// 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");
}

To test if a mic is present, just follow the code in [2] and the docs at [3], when calling prepare() you should get an IOException if the mic is not available:

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] http://developer.android.com/reference/android/media/MediaRecorder.AudioSource.html


After reading guido's answer, this is what I came up with. It looks very hackish to me, wish there would be a better way. I will accept guido's answer, but if there's a better way, please do tell me.

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;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜