我似乎无法在手机上运行语音识别器。 app instals和按钮返回它需要返回的内容,但实际的语音识别器没有返回开始语音等等......这是我的代码,希望它是一个简单的修复。我正在运行Galaxy s3,它显示错误(跟踪:无法打开,没有这样的目录)和ActivityManager:警告:活动未启动,其当前任务已被带到前面。可能是权限问题。任何帮助都会很棒!
public class MainActivity extends Activity implements OnClickListener {
ListView lv;
private SpeechRecognizer mSpeechRecognizer;
private Intent mSpeechRecognizerIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView)findViewById(R.id.lvVoiceReturn);
Button b= (Button)findViewById(R.id.bVoice);
b.setOnClickListener(this);
boolean available = SpeechRecognizer.isRecognitionAvailable(this);
Log.d("Speech", "available = " + available);
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizer.setRecognitionListener(new SpeechListener());
mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
this.getPackageName());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("speech", "button active");
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
}
private class SpeechListener implements RecognitionListener {
@Override
public void onBeginningOfSpeech() {
Log.d("Speech", "onBeginningOfSpeech");
}
@Override
public void onBufferReceived(byte[] buffer) {
Log.d("Speech", "onBufferReceived");
}
@Override
public void onEndOfSpeech() {
Log.d("Speech", "onEndOfSpeech");
}
@Override
public void onError(int error) {
Log.d("Speech", "onError");
}
@Override
public void onEvent(int eventType, Bundle params) {
Log.d("Speech", "onEvent");
}
@Override
public void onPartialResults(Bundle partialResults) {
Log.d("Speech", "onPartialResults");
}
@Override
public void onReadyForSpeech(Bundle params) {
Log.d("Speech", "onReadyForSpeech");
}
@Override
public void onResults(Bundle results) {
Log.d("Speech", "results");
}
@Override
public void onRmsChanged(float rmsdB) {
//Log.d("Speech", "onRmsChanged");
}
}
}
这是我的清单
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.speech.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:0)
您需要以下权限
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
并修复以下错误而不是
protected class mSpeechRecognizerIntent implements RecognitionListener
应该是
private class SpeechListener implements RecognitionListener
并在行
之后的onCreate
中添加以下内容
boolean available = SpeechRecognizer.isRecognitionAvailable();
Log.d("Speech", "available = " + available);
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizer.setRecognitionListener(new SpeechListener());