溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么Android項(xiàng)目中使用百度語音識(shí)別

發(fā)布時(shí)間:2020-11-24 15:05:09 來源:億速云 閱讀:209 作者:Leah 欄目:移動(dòng)開發(fā)

這篇文章將為大家詳細(xì)講解有關(guān)怎么Android項(xiàng)目中使用百度語音識(shí)別,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

使用百度語音識(shí)別,先要申請(qǐng)APP ID,這個(gè)直接到百度網(wǎng)站上有說明文檔,本文不再贅述。申請(qǐng)之后,下載SDK包,按照百度官網(wǎng)要求,合并libs和res兩個(gè)目錄到項(xiàng)目中,然后在build.gradle(module:app)中的Android{...}下添加

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

這樣, 百度語音識(shí)別的so文件才能正常使用。

Manifest文件中添加權(quán)限

<uses-permission android:name="android.permission.RECORD_AUDIO" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> 
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

然后還要在Manifest中添加

<!-- 請(qǐng)?zhí)顚憫?yīng)用實(shí)際的APP_ID --> 
<meta-data android:name="com.baidu.speech.APP_ID" android:value="APP ID"/> 
<!-- 請(qǐng)?zhí)顚憫?yīng)用實(shí)際的API_KEY --> 
<meta-data android:name="com.baidu.speech.API_KEY" android:value="API_KEY"/> 
<!-- 請(qǐng)?zhí)顚憫?yīng)用實(shí)際的SECRET_KEY --> 
<meta-data android:name="com.baidu.speech.SECRET_KEY" android:value="SECRET_KEY"/> 
 
<service android:name="com.baidu.speech.VoiceRecognitionService" android:exported="false" /> 

其中的APP ID,API_KEY和SECRET_KEY替換為你申請(qǐng)的內(nèi)容。

我們封裝了一個(gè)工具類,用來使用語音識(shí)別

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) { 
 
      // 創(chuàng)建識(shí)別器 
      mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(mContext, 
          new ComponentName(mContext, VoiceRecognitionService.class)); 
 
      // 注冊(cè)監(jiān)聽器 
      mSpeechRecognizer.setRecognitionListener(this); 
    } 
  } 
 
  public synchronized void destroyTool() { 
    mSpeechRecognizer.stopListening(); 
    mSpeechRecognizer.destroy(); 
    mSpeechRecognizer = null; 
  } 
 
  // 開始識(shí)別 
  public void startASR(ResultsCallback callback) { 
    mResultsCallback = callback; 
 
    Intent intent = new Intent(); 
    bindParams(intent); 
    mSpeechRecognizer.startListening(intent); 
  } 
 
  //停止識(shí)別 
  public void stopASR() { 
    mSpeechRecognizer.stopListening(); 
  } 
 
  private void bindParams(Intent intent) { 
    // 設(shè)置識(shí)別參數(shù) 
  } 
 
  @Override 
  public void onReadyForSpeech(Bundle params) { 
    // 準(zhǔn)備就緒 
    Toast.makeText(mContext, "請(qǐng)開始說話", Toast.LENGTH_SHORT).show(); 
  } 
 
  @Override 
  public void onBeginningOfSpeech() { 
    // 開始說話處理 
  } 
 
  @Override 
  public void onRmsChanged(float rmsdB) { 
    // 音量變化處理 
  } 
 
  @Override 
  public void onBufferReceived(byte[] buffer) { 
    // 錄音數(shù)據(jù)傳出處理 
  } 
 
  @Override 
  public void onEndOfSpeech() { 
    // 說話結(jié)束處理 
  } 
 
  @Override 
  public void onError(int error) { 
  } 
 
  @Override 
  public void onResults(Bundle results) { 
 
    // 最終結(jié)果處理 
    if (mResultsCallback != null) { 
      String text = results.get(SpeechRecognizer.RESULTS_RECOGNITION) 
          .toString().replace("]", "").replace("[", ""); 
      mResultsCallback.onResults(text); 
    } 
  } 
 
  @Override 
  public void onPartialResults(Bundle partialResults) { 
    // 臨時(shí)結(jié)果處理 
  } 
 
  @Override 
  public void onEvent(int eventType, Bundle params) { 
  } 
} 

MainActivity的界面如下

<&#63;xml version="1.0" encoding="utf-8"&#63;> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:id="@+id/activity_main" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:paddingBottom="@dimen/activity_vertical_margin" 
  android:paddingLeft="@dimen/activity_horizontal_margin" 
  android:paddingRight="@dimen/activity_horizontal_margin" 
  android:paddingTop="@dimen/activity_vertical_margin" 
  android:orientation="vertical" 
  android:gravity="center" 
  tools:context="com.yjp.speechrecognizer.MainActivity"> 
 
  <Button 
    android:id="@+id/startSpeechButton" 
    android:layout_width="60dp" 
    android:layout_height="40dp" 
    android:background="@drawable/bdspeech_btn_orangelight_normal" 
    android:text="按住說話"/> 
 
  <TextView 
    android:id="@+id/speechTextView" 
    android:layout_margin="10dp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
</LinearLayout> 

MainActivity的類實(shí)現(xiàn)為:

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); 
      } 
    }); 
  } 
} 


關(guān)于怎么Android項(xiàng)目中使用百度語音識(shí)別就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI