Android Programming Recording Tool Class RecorderUtil Definition and Usage Example

  • 2021-08-12 03:35:33
  • OfStack

In this paper, the definition and usage of Android programming recording tool class RecorderUtil are described with examples. Share it for your reference, as follows:

The following tool classes are verified by actual development and can be directly copied and used.

Introduction to recording tools:

Recording tools are mainly used to develop voice chat, which is commonly used on WeChat and QQ because of voice chat.

When using Hardware 1, you should open permissions, don't forget. It also needs to be used with Android FileUtil class, which is encapsulated for convenience


import android.media.MediaRecorder;
import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
 *  Recording tool 
 */
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");
  }
  /**
   *  Start recording 
   */
  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");
    }
  }
  /**
   *  Stop recording 
   */
  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");
    }
  }
  /**
   *  Cancel voice 
   */
  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;
  }
  /**
   *  Get the recording file 
   */
  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;
    }
  }
  /**
   *  Get the address of the recording file 
   */
  public String getFilePath(){
    return mFileName;
  }
  /**
   *  Get recording duration , Unit second 
   */
  public long getTimeInterval() {
    return timeInterval/1000;
  }
  /**
   *  Convert a file to byte[]
   *
   * @param file  Input 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();
    }
  }
}

Use steps:

1. First private RecorderUtil recorder = new RecorderUtil(); Instantiation 1
Step 2 Start recording recorder.startRecording();
3. Stop recording after recording recorder.stopRecording();
4. Of course, if you want to cancel the voice transmission after the recording starts, similar to canceling the voice transmission on WeChat, the solution sliding monitoring judgment determines to cancel the transmission, so don't send the message and call it recorder.cancelRecording(); //取消语音释放资源 You can

More readers interested in Android can check the topics of this site: "Introduction and Advanced Tutorial of Android Development", "Summary of Android Multimedia Operation Skills (Audio, Video, Recording, etc.)", "Summary of View Skills in Android View", "Summary of activity Operation Skills in Android Programming", "Summary of Android Operation Skills in json Format Data", "Summary of Android Resource Operation Skills" and "Summary of Android Control Usage"

I hope this article is helpful to everyone's Android programming.


Related articles: