Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.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
Android 语音识别结果不更新编辑文本_Android_Speech Recognition_Speech To Text - Fatal编程技术网

Android 语音识别结果不更新编辑文本

Android 语音识别结果不更新编辑文本,android,speech-recognition,speech-to-text,Android,Speech Recognition,Speech To Text,我尝试在我的应用程序中使用语音到文本功能。 它会打开录制窗口/对话框或其他任何东西,但文本不会显示。 代码如下: public void onTalk(View v){ promptSpeechInput(); } public void promptSpeechInput() { Intent i=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); i.putExtra(Recog

我尝试在我的应用程序中使用语音到文本功能。 它会打开录制窗口/对话框或其他任何东西,但文本不会显示。 代码如下:

public void onTalk(View v){
    promptSpeechInput();
}

    public void promptSpeechInput()
    {
        Intent i=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
        i.putExtra(RecognizerIntent.EXTRA_PROMPT,"say something");

        try{
        startActivityForResult(i, 50);
    }
        catch(ActivityNotFoundException e){
            Toast.makeText(this,"sorry your device doesnt support speech language",Toast.LENGTH_LONG).show();
        }
    }
然后是结果部分:

public void onActivityResult(int request_code, int result_code, Intent i){
    super.onActivityResult(request_code,result_code,i);
    if (request_code==RESULT_OK)
        if(i!=null) {
            EditText locationTS = (EditText) findViewById (R.id.locationET);
            ArrayList<String> result = i.getStringArrayListExtra((RecognizerIntent.EXTRA_RESULTS));
            locationTS.setText(result.get(0));
        }
}

locationTS是一个编辑文本,在我讲话后它不会显示任何文本。

这在我的情况下起作用:

我有一个单独的助手类:

public class SpeechRecognitionHelper {

private static final int VOICE_RECOGNITION_REQUEST_CODE=101;

public static void run(Activity activity){
    if(isSpeechRecognitionActivityPresent(activity)){
        startRecognition(activity);
    }
    else {
        Toast.makeText(activity,"You must install Google Voice Search",Toast.LENGTH_LONG).show();
        installGoogleVoiceSearch(activity);
    }
}

private static boolean isSpeechRecognitionActivityPresent(Activity activity){
    try {
        PackageManager packageManager = activity.getPackageManager();
        List activities = packageManager.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),0);
        if (activities.size()!=0){
            return true;
        }
    }
    catch (Exception e){

    }
    return false;
}

public static void startRecognition(Activity activity){
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"Say Something");
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,1);

    activity.startActivityForResult(intent,VOICE_RECOGNITION_REQUEST_CODE);
}

private static void installGoogleVoiceSearch(final Activity activity){
    Dialog dialog = new AlertDialog.Builder(activity)
            .setMessage("For recognition Install Google Voice Search")
            .setTitle("Install voice search?")
            .setPositiveButton("Install", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.google.android.voicesearch"));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
                    activity.startActivity(intent);
                }
            })
            .setNegativeButton("Cancel", null)
            .create();
    dialog.show();
}
}
然后在主活动中:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private static final int VOICE_RECOGNITION_REQUEST_CODE=101;
private static final int CAMERA_REQUEST=201;
private Button buttonVoiceCommand;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    buttonVoiceCommand = (Button)findViewById(R.id.buttonVoiceCommand);
    buttonVoiceCommand.setOnClickListener(this);
}


@Override
public void onClick(View view) {
    int id = view.getId();
    if (id == R.id.buttonVoiceCommand){
        run(MainActivity.this);
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode,resultCode,data);
    if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
        ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        if(matches.size()>0){
            if(matches.get(0).indexOf("camera")>0){
                Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent,CAMERA_REQUEST);
            }
            else {
                Toast.makeText(this,matches.get(0),Toast.LENGTH_LONG).show();
            }
        }
    }
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
        //Perform whatever you want to do (-.-)
        Toast.makeText(this,"Camera Working",Toast.LENGTH_LONG).show();
    }
}
}

我为匹配的内容干杯。get0你只需在编辑文本中设置它

google提供语音到文本服务为什么要在日志中使用itPrint result.get0,检查logcat输出,它实际上包含文本吗?