Android MediaRecorder与AudioRecord音频录制

MediaRecorder
AudioRecord

  1. MediaRecorder
    需要的权限
  
  
  /**
   * 开始录音
   */
  class startRecordListener implements OnClickListener {

    @Override public void onClick(View v) {
      try {
        MediaRecorder mRecorder = new MediaRecorder();
        // 设置声音源为麦克风
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        // 设置输出格式为3gp
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        // 设置输出文件路径,mFileName为录音音频输出路径
        mRecorder.setOutputFile(mFileName);
        // 设置声音解码AMR_NB
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        // 媒体录制器准备
        mRecorder.prepare();
        // 开始录制
        mRecorder.start();
      } catch (IOException e) {
      }
    }
  }

  /**
   * 停止录音
   */
  class stopRecordListener implements OnClickListener {

    @Override public void onClick(View v) {
      mRecorder.stop();
      mRecorder.release();
      mRecorder = null;
    }
  }

2.AudioRecord

AudioRecord录音步骤:
1.    创建一个数据流。
2.    构造一个AudioRecord对象,其中需要的最小录音缓存buffer大小可以通过getMinBufferSize方法得到。如果buffer容量过小,将导致对象构造的失败。
3.    初始化一个buffer,该buffer大于等于AudioRecord对象用于写声音数据的buffer大小。
4.    开始录音。
5.    从AudioRecord中读取声音数据到初始化buffer,将buffer中数据导入数据流。
6.    停止录音。
7.    关闭数据流。

录音代码:

  /**
   * 录音,需要在新的线程中执行,防止主线程阻塞
   */
  private void record() {
    int frequency = 11025;
    int channelConfiguration = AudioFormat.CHANNEL_IN_STEREO;
    int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
    try {
      // 创建一个数据输入流往文件里写数据
      OutputStream os = new FileOutputStream(mFileName);
      BufferedOutputStream bos = new BufferedOutputStream(os);
      DataOutputStream dos = new DataOutputStream(bos);
      // 创建一个AudioRecord对象来录音
      int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
      AudioRecord mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency, channelConfiguration,
          audioEncoding, bufferSize);
      short[] buffer = new short[bufferSize];
      // 开始录音
      mAudioRecord.startRecording();

      isRecording = true;
      Log.e(TAG, "record: 录音开始");
      while (isRecording) {
        int bufferReadResult = mAudioRecord.read(buffer, 0, bufferSize);
        for (int i = 0; i < bufferReadResult; i++) {
          dos.writeShort(buffer[i]);
          Log.e(TAG, "record: 录音中...");
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * 停止录音
   */
  class stopRecordListener implements OnClickListener {

    @Override public void onClick(View v) {
      mAudioRecord.stop();
      Log.e(TAG, "record: 录音结束");
    }
  }

播放代码:

  /**
   * 播放录音
   */
  class startPlayListener implements OnClickListener {

    @Override public void onClick(View v) {
      try {
        // Get the file we want to playback.
        File file = new File(mFileName);
        // Get the length of the audio stored in the file (16 bit so 2 bytes per short)
        // and create a short array to store the recorded audio.
        int musicLength = (int) (file.length() / 2);
        short[] music = new short[musicLength];
        // Create a DataInputStream to read the audio data back from the saved file
        InputStream is = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(is);
        DataInputStream dis = new DataInputStream(bis);

        // Read the file into the music array.
        int i = 0;
        while (dis.available() > 0) {
          music[i++] = dis.readShort();
        }

        // close the input streams
        dis.close();

        // Create a new AudioTrack object using the same parameters as the AudioRecord
        // object used to create the file.
        AudioTrack audioTrack =
            new AudioTrack(AudioManager.STREAM_MUSIC, 11025, AudioFormat.CHANNEL_IN_STEREO,
                AudioFormat.ENCODING_PCM_16BIT, musicLength * 2, AudioTrack.MODE_STREAM);
        Log.e(TAG, "onClick: 开始播放");
        // Start playback
        audioTrack.play();

        // Write the music buffer to the AudioTrack object
        audioTrack.write(music, 0, musicLength);
        Log.e(TAG, "onClick: 播放结束");
        audioTrack.stop();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

你可能感兴趣的:(Android MediaRecorder与AudioRecord音频录制)