1

我正在创建一个使用 google 的 api 将语音转换为文本的应用程序。当我将以下代码设置为主要活动时,它可以完美运行。但是,我现在正在创建一个新应用程序,并将 xml 布局和 java 文件复制粘贴为一个名为display_1. 我现在的问题是当我通过应用程序导航到此窗口时,我希望语音识别会在 500 毫秒后开始,实际上确实如此,但我收到一条消息说“无法打开麦克风”。

我已将正确的权限添加到 gradle 文件中并设置了正确的最低 API 级别。请不要我的应用程序的主要活动使用 PocketSphinx sdk,然后第二个活动称为display_1使用 google api 实现onClickListener

如果有人能告诉我是什么导致我收到“无法打开麦克风”警告,我们将不胜感激。请注意wifi等已连接。下面是java文件:

public class Display_1 extends Activity implements View.OnClickListener {

ListView lv;
static final int check = 1111;
Button b;
EditText a;
Button c;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.display_1);
    lv = (ListView) findViewById(R.id.lvVoiceReturn_1);
    a = (EditText) findViewById(R.id.TFusername_1);
    b = (Button) findViewById(R.id.bVoice_1);
    c = (Button)findViewById(R.id.Blogin_1);

    b.setOnClickListener(this);
    //This now handles an automatic press of the bVoice button 1 second after the activity is opened
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            b.callOnClick();
        }
    }, 1000);
}

public void onButtonClick(View v) {
    if (v.getId() == R.id.Blogin_1) {
        String str = a.getText().toString();


        //Go to the relevant page if any part of the phrase or word entered in the 'EditText' field contains 'next' which is not case sensitive
        if (str.toLowerCase().contains("next")) {
            Intent userintent = new Intent(Display_1.this, Display_2.class);
            startActivity(userintent);
        }else {
            Toast.makeText(getApplicationContext(), "Incorrect Information", Toast.LENGTH_SHORT).show();
        }
    }
}

@Override
public void onClick(View v) {
    Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Please Repeat Again");
    startActivityForResult(i, check);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == check && resultCode == RESULT_OK) {
        ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));

        a.setText((String) lv.getItemAtPosition(0));    //Get the first phrase in the first row of list view

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                c.performClick();
            }
        }, 500);    //Automatically click the 'Blogin' button after 500ms
    }

    super.onActivityResult(requestCode, resultCode, data);
}

}

以下是我在 android manifest 文件中设置的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
4

0 回答 0