我正在开发一个简单的应用程序,可以在按下按钮时打开活动等。但我的主要活动是基于PocketSphinx API,我的第二个活动使用onClickListener
和Google的Speech to text API。
当我将这些活动作为单独的应用程序运行时,它们可以完美地运行。当我将它们集成到一个应用程序并分配正确的按钮等时,应用程序运行良好,但是当在“语音到文本”活动中,按钮gVoice
打开语音提示但立即显示“无法打开麦克风”
我已附加在我的清单文件下方,主要活动文件的一部分以及完整的Gvoice活动。
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.cmu.pocketsphinx.demo"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="20" />
<application
android:allowBackup="false"
android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity
android:name="PocketSphinxActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
//every new activity needs to be listed here:
<activity android:name=".Gvoice"></activity>
</application>
MainActivity文件的顶部:
package edu.cmu.pocketsphinx.demo;
import static android.widget.Toast.makeText;
import static edu.cmu.pocketsphinx.SpeechRecognizerSetup.defaultSetup;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Intent;
import android.view.View.OnClickListener;
import edu.cmu.pocketsphinx.Assets;
import edu.cmu.pocketsphinx.Hypothesis;
import edu.cmu.pocketsphinx.RecognitionListener;
import edu.cmu.pocketsphinx.SpeechRecognizer;
public class PocketSphinxActivity extends Activity implements RecognitionListener
{
private static final String KWS_SEARCH = "wakeup";
/* Keyword we are looking for to activate menu */
private static final String KEYPHRASE = "open voice command"; //adjust this keyphrase!
private SpeechRecognizer recognizer;
private HashMap<String, Integer> captions;
ListView lv;
TextView tv;
EditText a;
Button b;
Button c;
Boolean isDone = false;
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
// Prepare the data for UI
captions = new HashMap<String, Integer>();
captions.put(KWS_SEARCH, R.string.kws_caption);
setContentView(R.layout.main);
((TextView) findViewById(R.id.caption_text))
.setText("Preparing the recognizer");
lv = (ListView) findViewById(R.id.lvVoiceReturn);
tv = (TextView) findViewById(R.id.result_text);
a = (EditText) findViewById(R.id.TFusername);
b = (Button) findViewById(R.id.bVoice);
c = (Button)findViewById(R.id.Blogin);
// Recognizer initialization is a time-consuming and it involves IO,
// so we execute it in async task
new AsyncTask<Void, Void, Exception>() {
@Override
//etc...
Full Gvoice java文件:
package edu.cmu.pocketsphinx.demo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
public class Gvoice extends Activity implements OnClickListener{
ListView lv1;
static final int check = 1111;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gvoice);
lv1 = (ListView)findViewById(R.id.LVGVoiceReturn);
Button b1 = (Button)findViewById(R.id.GVoice);
b1.setOnClickListener(this);
}
public void onClick(View v){
Intent i1 = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i1.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i1.putExtra(RecognizerIntent.EXTRA_PROMPT, "Please Repeat Again");
startActivityForResult(i1, 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);
lv1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));
}
super.onActivityResult(requestCode, resultCode, data);
}
}
只是想知道是否存在任何软件包名称等问题,我已经获得了为互联网和录制音频等设置的权限。希望有知识渊博的人帮助我尽快解决这个问题。