溫馨提示×

溫馨提示×

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

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

Android編程錄音工具類RecorderUtil定義與用法示例

發(fā)布時(shí)間:2020-09-03 13:39:02 來源:腳本之家 閱讀:90 作者:沉水之木 欄目:移動(dòng)開發(fā)

本文實(shí)例講述了Android編程錄音工具類RecorderUtil定義與用法。分享給大家供大家參考,具體如下:

以下工具類都是經(jīng)過實(shí)戰(zhàn)開發(fā)驗(yàn)證都是可以直接復(fù)制使用的。

錄音工具類介紹:

錄音工具類主要平時(shí)用來開發(fā)語音聊天的,在微信和QQ上該工具類都是常用的,因?yàn)檎Z音聊天。

使用硬件一般都要開權(quán)限,別忘了。這里還需要搭配 Android FileUtil 類使用,為了方便才這么封裝的

import android.media.MediaRecorder;
import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
 * 錄音工具
 */
public class RecorderUtil {
  private static final String TAG = "RecorderUtil";
  private String mFileName = null;
  private MediaRecorder mRecorder = null;
  private long startTime;
  private long timeInterval;
  private boolean isRecording;
  public RecorderUtil(){
    mFileName = FileUtil.getCacheFilePath("tempAudio");
  }
  /**
   * 開始錄音
   */
  public void startRecording() {
    if (mFileName == null) return;
    if (isRecording){
      mRecorder.release();
      mRecorder = null;
    }
    mRecorder = new MediaRecorder();
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mRecorder.setOutputFile(mFileName);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    startTime = System.currentTimeMillis();
    try {
      mRecorder.prepare();
      mRecorder.start();
      isRecording = true;
    } catch (Exception e){
      Log.e(TAG, "prepare() failed");
    }
  }
  /**
   * 停止錄音
   */
  public void stopRecording() {
    if (mFileName == null) return;
    timeInterval = System.currentTimeMillis() - startTime;
    try{
      if (timeInterval>1000){
        mRecorder.stop();
      }
      mRecorder.release();
      mRecorder = null;
      isRecording =false;
    }catch (Exception e){
      Log.e(TAG, "release() failed");
    }
  }
  /**
   * 取消語音
   */
  public synchronized void cancelRecording() {
    if (mRecorder != null) {
      try {
        mRecorder.release();
        mRecorder = null;
      } catch (Exception e) {
        e.printStackTrace();
      }
      File file = new File(mFileName);
      file.deleteOnExit();
    }
    isRecording =false;
  }
  /**
   * 獲取錄音文件
   */
  public byte[] getDate() {
    if (mFileName == null) return null;
    try{
      return readFile(new File(mFileName));
    }catch (IOException e){
      Log.e(TAG, "read file error" + e);
      return null;
    }
  }
  /**
   * 獲取錄音文件地址
   */
  public String getFilePath(){
    return mFileName;
  }
  /**
   * 獲取錄音時(shí)長,單位秒
   */
  public long getTimeInterval() {
    return timeInterval/1000;
  }
  /**
   * 將文件轉(zhuǎn)化為byte[]
   *
   * @param file 輸入文件
   */
  private static byte[] readFile(File file) throws IOException {
    // Open file
    RandomAccessFile f = new RandomAccessFile(file, "r");
    try {
      // Get and check length
      long longlength = f.length();
      int length = (int) longlength;
      if (length != longlength)
        throw new IOException("File size >= 2 GB");
      // Read file and return data
      byte[] data = new byte[length];
      f.readFully(data);
      return data;
    } finally {
      f.close();
    }
  }
}

使用步驟:

1. 首先private RecorderUtil recorder = new RecorderUtil(); 實(shí)例化一下
2. 開始錄音recorder.startRecording();
3. 錄音完成后停止錄音recorder.stopRecording();
4. 當(dāng)然如果錄音開始之后想取消語音發(fā)送,類似于微信上滑取消語音發(fā)送,解決方案滑動(dòng)監(jiān)聽判斷確定取消發(fā)送,就不要將消息發(fā)出去并且還要調(diào)用recorder.cancelRecording(); //取消語音釋放資源 即可

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》

希望本文所述對大家Android程序設(shè)計(jì)有所幫助。

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

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

AI