我相信我已经知道了如何检测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
}然而,如何检测android设备是否具有语音转文本功能?还是应该使用上面的内容来检测这一点?如果有,如何检测设备是否有麦克风?
任何反馈都很感谢,谢谢。
发布于 2011-01-23 22:00:24
在读完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;
}
}https://stackoverflow.com/questions/4770835
复制相似问题