0

我似乎无法在手机上运行语音识别器。应用程序安装并且按钮正在返回它需要返回的内容,但实际的语音识别器没有返回 onBeginning of Speech 等......这是我的代码,希望它是一个简单的修复。我在 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>
4

1 回答 1

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());
于 2013-05-03T03:20:13.580 回答