Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何检测android上是否提供语音到文本转换?_Java_Android_Media_Speech Recognition_Microphone - Fatal编程技术网

Java 如何检测android上是否提供语音到文本转换?

Java 如何检测android上是否提供语音到文本转换?,java,android,media,speech-recognition,microphone,Java,Android,Media,Speech Recognition,Microphone,我相信我已经找到了检测android设备是否有麦克风的方法,比如: Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); List<ResolveInfo> speechActivities = packageManager.queryIntentActivities(speechIntent, 0); TextView micAvailView = (T

我相信我已经找到了检测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

        }
Intent speechIntent=新意图(RecognizerIntent.ACTION\u RECOGNIZE\u SPEECH);
List speechActivities=packageManager.QueryInputActivities(speechIntent,0);
TextView micAvailView=(TextView)findViewById(R.id.mic\u available\u标志);
如果(speechActivities.size()!=0){//我们有一个麦克风
}
否则{//我们没有麦克风
}
然而,如何检测android设备是否具有语音到文本的功能?或者应该使用上述方法来检测这种情况?如果是,如何检测设备是否有麦克风


感谢您的反馈。

您附加的代码确实用于检测音频识别是否可用[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"); } //检查是否存在识别活动 PackageManager pm=getPackageManager(); 列出活动=pm.querytentActivities( 新意图(识别者意图、行动、识别、言语),0); 如果(activities.size()!=0){ speakButton.setOnClickListener(此); }否则{ speakButton.setEnabled(假); speakButton.setText(“识别器不存在”); } 要测试麦克风是否存在,只需按照[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(); 记录器=新的MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); setOutputFormat(MediaRecorder.OutputFormat.ThreeGPP); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); recorder.setOutputFile(路径); 记录器。准备(); [1]
[2]

[3]

在阅读了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;
    }
}
包;
导入java.io.File;
导入java.io.IOException;
导入java.util.List;
导入android.content.Context;
导入android.content.Intent;
导入android.content.pm.PackageManager;
导入android.content.pm.ResolveInfo;
导入android.media.MediaRecorder;
导入android.speech.RecognizerIntent;
公共类MediaUtil{
//返回麦克风是否存在
公共布尔getMicrophoneExists(上下文){
PackageManager PackageManager=context.getPackageManager();
返回packageManager.hasSystemFeature(packageManager.FEATURE_麦克风);
}
//返回麦克风是否可用
公共静态布尔getMicrophoneAvailable(上下文){
MediaRecorder=新的MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
recorder.setOutputFile(新文件(context.getCacheDir(),“MediaUtil#micAvailTestFile”).getAbsolutePath());
布尔值可用=真;
试试{
记录器。准备();
}
捕获(IOException异常){
可用=错误;
}
记录器。释放();
返回可用;
}
//返回文本到语音是否可用
公共静态布尔gettsavailable(上下文){
PackageManager PackageManager=context.getPackageManager();
意向演讲意向=新意向(识别者意向、行动、识别、演讲);
List speechActivities=packageManager.QueryInputActivities(speechIntent,0);
if(speechActivities.size()!=0)返回true;
返回false;
}
}

我真的需要准备录音吗?我只想看看是否有麦克风,我根本不想在这个阶段录音。没有getMicrophoneDevices()方法或其他方法?并确认,如果音频识别可用,这相当于语音到文本的可用性?此外,IOException可能有几个原因,除了设备中没有实现麦克风之外。@Tom是的,我这里指的是语音到文本的音频识别;注意,您只是询问底层系统是否存在可以处理ACTION-Recognite\u语音意图的活动,因此这并不保证麦克风可用。@Tom edit:看这里,似乎只有麦克风可用时才会返回识别活动(它可能有麦克风,或者这是由录制\u音频权限暗示的)@Guido,那么为了检测是否有麦克风,我必须准备一段录音?对我来说,这似乎缺少API…这种方法的另一个问题是,我没有路径可设置,因为我不想保存任何录制。你确定这是唯一的办法吗?