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

I believe that I understood how to determine if an Android device has a microphone, for example:

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

        }

However, how do you determine if an Android device has voice connectivity? Or should this be used for detection? If so, how do you determine if a device has a microphone?

Any feedback is appreciated, thanks.

+3
source share
2 answers

The code you entered is really used to determine the availability of sound [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");
}

, , [2] [3] prepare() 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] http://developer.android.com/reference/android/media/MediaRecorder.AudioSource.html

+7

guido, , . , , . 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;
    }
}
+1

Source: https://habr.com/ru/post/1787204/


All Articles