欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2018年01月29日 14:25:30   作者:沉水之木  
這篇文章主要介紹了Android編程錄音工具類RecorderUtil定義與用法,結(jié)合實(shí)例形式分析了Android錄音工具類實(shí)現(xiàn)開始錄音、停止錄音、取消錄音、獲取錄音信息等相關(guān)操作技巧,需要的朋友可以參考下

本文實(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í)長(zhǎng),單位秒
   */
  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é)

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

相關(guān)文章

最新評(píng)論