Java Code Examples for android.media.AudioRecord#setPositionNotificationPeriod()

The following examples show how to use android.media.AudioRecord#setPositionNotificationPeriod() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Timber4.java    From Muzesto with GNU General Public License v3.0 6 votes vote down vote up
private void initRecorder() {
    final int bufferSize = 2 * AudioRecord.getMinBufferSize(RECORDER_SAMPLE_RATE,
            RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING);
    audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, RECORDER_SAMPLE_RATE,
            RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING, bufferSize);
    AudioUtil.initProcessor(RECORDER_SAMPLE_RATE, RECORDER_CHANNELS, RECORDER_ENCODING_BIT);

    recordingThread = new Thread("recorder") {
        @Override
        public void run() {
            super.run();
            buffer = new byte[bufferSize];
            Looper.prepare();
            audioRecord.setRecordPositionUpdateListener(recordPositionUpdateListener, new Handler(Looper.myLooper()));
            int bytePerSample = RECORDER_ENCODING_BIT / 8;
            float samplesToDraw = bufferSize / bytePerSample;
            audioRecord.setPositionNotificationPeriod((int) samplesToDraw);
            //We need to read first chunk to motivate recordPositionUpdateListener.
            //Mostly, for lower versions - https://code.google.com/p/android/issues/detail?id=53996
            audioRecord.read(buffer, 0, bufferSize);
            Looper.loop();
        }
    };
}
 
Example 2
Source File: Timber5.java    From Muzesto with GNU General Public License v3.0 6 votes vote down vote up
private void initRecorder() {
    final int bufferSize = 2 * AudioRecord.getMinBufferSize(RECORDER_SAMPLE_RATE,
            RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING);
    audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, RECORDER_SAMPLE_RATE,
            RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING, bufferSize);
    AudioUtil.initProcessor(RECORDER_SAMPLE_RATE, RECORDER_CHANNELS, RECORDER_ENCODING_BIT);

    recordingThread = new Thread("recorder") {
        @Override
        public void run() {
            super.run();
            buffer = new byte[bufferSize];
            Looper.prepare();
            audioRecord.setRecordPositionUpdateListener(recordPositionUpdateListener, new Handler(Looper.myLooper()));
            int bytePerSample = RECORDER_ENCODING_BIT / 8;
            float samplesToDraw = bufferSize / bytePerSample;
            audioRecord.setPositionNotificationPeriod((int) samplesToDraw);
            //We need to read first chunk to motivate recordPositionUpdateListener.
            //Mostly, for lower versions - https://code.google.com/p/android/issues/detail?id=53996
            audioRecord.read(buffer, 0, bufferSize);
            Looper.loop();
        }
    };
}
 
Example 3
Source File: MP3Recorder.java    From GSYRecordWave with Apache License 2.0 5 votes vote down vote up
/**
   * Initialize audio recorder
   */
  private void initAudioRecorder() throws IOException {
      mBufferSize = AudioRecord.getMinBufferSize(DEFAULT_SAMPLING_RATE,
              DEFAULT_CHANNEL_CONFIG, DEFAULT_AUDIO_FORMAT.getAudioFormat());

      int bytesPerFrame = DEFAULT_AUDIO_FORMAT.getBytesPerFrame();
      /* Get number of samples. Calculate the buffer size
       * (round up to the factor of given frame size)
 * 使能被整除,方便下面的周期性通知
 * */
      int frameSize = mBufferSize / bytesPerFrame;
      if (frameSize % FRAME_COUNT != 0) {
          frameSize += (FRAME_COUNT - frameSize % FRAME_COUNT);
          mBufferSize = frameSize * bytesPerFrame;
      }

/* Setup audio recorder */
      mAudioRecord = new AudioRecord(DEFAULT_AUDIO_SOURCE,
              DEFAULT_SAMPLING_RATE, DEFAULT_CHANNEL_CONFIG, DEFAULT_AUDIO_FORMAT.getAudioFormat(),
              mBufferSize);

      mPCMBuffer = new short[mBufferSize];
      /*
       * Initialize lame buffer
 * mp3 sampling rate is the same as the recorded pcm sampling rate 
 * The bit rate is 32kbps
 * 
 */
      LameUtil.init(DEFAULT_SAMPLING_RATE, DEFAULT_LAME_IN_CHANNEL, DEFAULT_SAMPLING_RATE, DEFAULT_LAME_MP3_BIT_RATE, DEFAULT_LAME_MP3_QUALITY);
      // Create and run thread used to encode data
      // The thread will
      mEncodeThread = new DataEncodeThread(mRecordFile, mBufferSize);
      mEncodeThread.start();
      mAudioRecord.setRecordPositionUpdateListener(mEncodeThread, mEncodeThread.getHandler());
      mAudioRecord.setPositionNotificationPeriod(FRAME_COUNT);
  }
 
Example 4
Source File: Mp3Recorder.java    From Android-Application-ZJB with Apache License 2.0 4 votes vote down vote up
/**
   * Initialize audio recorder
   */
  private void initAudioRecorder() throws IOException {
      int bytesPerFrame = audioFormat.getBytesPerFrame();
      /* Get number of samples. Calculate the buffer size (round up to the
         factor of given frame size) */
      int frameSize = AudioRecord.getMinBufferSize(samplingRate,
              channelConfig, audioFormat.getAudioFormat()) / bytesPerFrame;
      if (frameSize % FRAME_COUNT != 0) {
          frameSize = frameSize + (FRAME_COUNT - frameSize % FRAME_COUNT);
          Log.d(TAG, "Frame size: " + frameSize);
      }

      bufferSize = frameSize * bytesPerFrame;

/* Setup audio recorder */
      audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
              samplingRate, channelConfig, audioFormat.getAudioFormat(),
              bufferSize);

      // Setup RingBuffer. Currently is 10 times size of hardware buffer
      // Initialize buffer to hold data
      ringBuffer = new RingBuffer(10 * bufferSize);
      buffer = new byte[bufferSize];

      // Initialize lame buffer
      // mp3 sampling rate is the same as the recorded pcm sampling rate
      // The bit rate is 32kbps
      SimpleLame.init(samplingRate, 1, samplingRate, BIT_RATE);

      if (TextUtils.isEmpty(audioOutputPath)) {
          throw new NullPointerException("please set the audio output path!");
      }
      mp3File = new File(audioOutputPath);
      os = new FileOutputStream(mp3File);

      // Create and run thread used to encode data
      // The thread will
      encodeThread = new DataEncodeThread(ringBuffer, os, bufferSize);
      encodeThread.start();
      audioRecord.setRecordPositionUpdateListener(encodeThread, encodeThread.getHandler());
      audioRecord.setPositionNotificationPeriod(FRAME_COUNT);
  }