栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android使用百度语音识别的示例代码

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

Android使用百度语音识别的示例代码

本文使用百度语音识别,完成语音识别的功能,使用百度语音识别,先要申请APP ID,这个直接到百度网站上有说明文档,本文不再赘述。申请之后,下载SDK包,按照百度官网要求,合并libs和res两个目录到项目中,然后在build.gradle(module:app)中的Android{...}下添加

sourceSets{ 
  main{ 
    jniLibs.srcDirs=['libs'] 
  } 
} 

这样, 百度语音识别的so文件才能正常使用。

Manifest文件中添加权限

 
 
 
 
 
 
 

然后还要在Manifest中添加

 
 
 
 
 
 
 
 

其中的APP ID,API_KEY和SECRET_KEY替换为你申请的内容。

我们封装了一个工具类,用来使用语音识别

package com.yjp.speechrecognizer; 
 
import android.content.ComponentName; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.speech.RecognitionListener; 
import android.speech.SpeechRecognizer; 
import android.widget.Toast; 
 
import com.baidu.speech.VoiceRecognitionService; 
 
public class SpeechRecognizerTool implements RecognitionListener { 
 
  public interface ResultsCallback { 
    void onResults(String result); 
  } 
 
  private Context mContext; 
 
  private SpeechRecognizer mSpeechRecognizer; 
 
  private ResultsCallback mResultsCallback; 
 
  public SpeechRecognizerTool(Context context) { 
    mContext = context; 
  } 
 
  public synchronized void createTool() { 
    if (null == mSpeechRecognizer) { 
 
      // 创建识别器 
      mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(mContext, 
   new ComponentName(mContext, VoiceRecognitionService.class)); 
 
      // 注册监听器 
      mSpeechRecognizer.setRecognitionListener(this); 
    } 
  } 
 
  public synchronized void destroyTool() { 
    mSpeechRecognizer.stopListening(); 
    mSpeechRecognizer.destroy(); 
    mSpeechRecognizer = null; 
  } 
 
  // 开始识别 
  public void startASR(ResultsCallback callback) { 
    mResultsCallback = callback; 
 
    Intent intent = new Intent(); 
    bindParams(intent); 
    mSpeechRecognizer.startListening(intent); 
  } 
 
  //停止识别 
  public void stopASR() { 
    mSpeechRecognizer.stopListening(); 
  } 
 
  private void bindParams(Intent intent) { 
    // 设置识别参数 
  } 
 
  @Override 
  public void onReadyForSpeech(Bundle params) { 
    // 准备就绪 
    Toast.makeText(mContext, "请开始说话", Toast.LENGTH_SHORT).show(); 
  } 
 
  @Override 
  public void onBeginningOfSpeech() { 
    // 开始说话处理 
  } 
 
  @Override 
  public void onRmsChanged(float rmsdB) { 
    // 音量变化处理 
  } 
 
  @Override 
  public void onBufferReceived(byte[] buffer) { 
    // 录音数据传出处理 
  } 
 
  @Override 
  public void onEndOfSpeech() { 
    // 说话结束处理 
  } 
 
  @Override 
  public void onError(int error) { 
  } 
 
  @Override 
  public void onResults(Bundle results) { 
 
    // 最终结果处理 
    if (mResultsCallback != null) { 
      String text = results.get(SpeechRecognizer.RESULTS_RECOGNITION) 
   .toString().replace("]", "").replace("[", ""); 
      mResultsCallback.onResults(text); 
    } 
  } 
 
  @Override 
  public void onPartialResults(Bundle partialResults) { 
    // 临时结果处理 
  } 
 
  @Override 
  public void onEvent(int eventType, Bundle params) { 
  } 
} 

MainActivity的界面如下

 
 
 
  

MainActivity的类实现为:

package com.yjp.speechrecognizer; 
 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
 
public class MainActivity extends AppCompatActivity implements SpeechRecognizerTool.ResultsCallback { 
 
  private Button mStartSpeechButton; 
  private TextView mTextView; 
 
  private SpeechRecognizerTool mSpeechRecognizerTool = new SpeechRecognizerTool(this); 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
 
    mTextView = (TextView) findViewById(R.id.speechTextView); 
 
    mStartSpeechButton = (Button) findViewById(R.id.startSpeechButton); 
    mStartSpeechButton.setonTouchListener(new View.onTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
 int action = event.getAction(); 
 switch (action) { 
   case MotionEvent.ACTION_DOWN: 
     mSpeechRecognizerTool.startASR(MainActivity.this); 
     mStartSpeechButton.setBackgroundResource( 
  R.drawable.bdspeech_btn_orangelight_pressed); 
     break; 
   case MotionEvent.ACTION_UP: 
     mSpeechRecognizerTool.stopASR(); 
     mStartSpeechButton.setBackgroundResource( 
  R.drawable.bdspeech_btn_orangelight_normal); 
     break; 
   default: 
     return false; 
 } 
 
 return true; 
      } 
    }); 
  } 
 
  @Override 
  protected void onStart() { 
    super.onStart(); 
    mSpeechRecognizerTool.createTool(); 
  } 
 
  @Override 
  protected void onStop() { 
    super.onStop(); 
    mSpeechRecognizerTool.destroyTool(); 
  } 
 
  @Override 
  public void onResults(String result) { 
    final String finalResult = result; 
    MainActivity.this.runonUiThread(new Runnable() { 
      @Override 
      public void run() { 
 mTextView.setText(finalResult); 
      } 
    }); 
  } 
} 


可以运行看一下效果,感觉识别率还是不错的。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

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

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

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