栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

没有弹出窗口的Android语音识别应用

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

没有弹出窗口的Android语音识别应用

AndroidManifest.xml

添加以下权限:

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

班级成员

private SpeechRecognizer mSpeechRecognizer;private Intent mSpeechRecognizerIntent; private boolean mIslistening;

在onCreate中

@Overrideprotected void onCreate(Bundle savedInstanceState){    super.onCreate(savedInstanceState);    .........    .........    mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);    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());    SpeechRecognitionListener listener = new SpeechRecognitionListener();    mSpeechRecognizer.setRecognitionListener(listener);}

在您的按钮侦听器中只需使用此代码

if (!mIsListening){    mSpeechRecognizer.startListening(mSpeechRecognizerIntent);}

在onDestroy

if (mSpeechRecognizer != null){        mSpeechRecognizer.destroy();}

在您的活动中创建内部类

protected class SpeechRecognitionListener implements RecognitionListener{    @Override    public void onBeginningOfSpeech()    { //Log.d(TAG, "onBeginingOfSpeech");     }    @Override    public void onBufferReceived(byte[] buffer)    {    }    @Override    public void onEndOfSpeech()    {        //Log.d(TAG, "onEndOfSpeech");     }    @Override    public void onError(int error)    {         mSpeechRecognizer.startListening(mSpeechRecognizerIntent);        //Log.d(TAG, "error = " + error);    }    @Override    public void onEvent(int eventType, Bundle params)    {    }    @Override    public void onPartialResults(Bundle partialResults)    {    }    @Override    public void onReadyForSpeech(Bundle params)    {        Log.d(TAG, "onReadyForSpeech"); //$NON-NLS-1$    }    @Override    public void onResults(Bundle results)    {        //Log.d(TAG, "onResults"); //$NON-NLS-1$        ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);        // matches are the return values of speech recognition engine        // Use these values for whatever you wish to do    }    @Override    public void onRmsChanged(float rmsdB)    {    }}

EDIT
2015-02-07:将ZakiMak)和Born ToWin对此问题的答案中的代码合并到此答案中的代码中,以使此答案更加完整。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/595256.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号