使用活动时无法打开麦克风错误

时间:2016-05-09 05:52:30

标签: android listview android-studio android-edittext android-speech-api

我正在创建一个使用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清单文件中设置的权限:

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

0 个答案:

没有答案