Small changes to audio recording. Still bug

pull/360/head
Mattia Iavarone 6 years ago
parent a87b73a35d
commit 20691effb3
  1. 12
      cameraview/src/main/gles/com/otaliastudios/cameraview/AudioMediaEncoder.java
  2. 2
      cameraview/src/main/gles/com/otaliastudios/cameraview/MediaEncoder.java
  3. 3
      cameraview/src/main/gles/com/otaliastudios/cameraview/MediaEncoderEngine.java

@ -67,18 +67,17 @@ class AudioMediaEncoder extends MediaEncoder {
@Override @Override
void stop() { void stop() {
mRequestStop = true; mRequestStop = true;
try {
synchronized (mLock) { synchronized (mLock) {
try {
mLock.wait(); mLock.wait();
} } catch (InterruptedException e) {}
} catch (InterruptedException e) {
// Ignore
} }
} }
@Override @Override
void release() { void release() {
super.release(); super.release();
mRequestStop = false;
} }
class AudioThread extends Thread { class AudioThread extends Thread {
@ -93,8 +92,7 @@ class AudioMediaEncoder extends MediaEncoder {
if (bufferSize < minBufferSize) { if (bufferSize < minBufferSize) {
bufferSize = ((minBufferSize / SAMPLES_PER_FRAME) + 1) * SAMPLES_PER_FRAME * 2; bufferSize = ((minBufferSize / SAMPLES_PER_FRAME) + 1) * SAMPLES_PER_FRAME * 2;
} }
mAudioRecord = new AudioRecord( mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.CAMCORDER, SAMPLE_RATE,
MediaRecorder.AudioSource.CAMCORDER, SAMPLE_RATE,
AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize); AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize);
} }
@ -120,6 +118,8 @@ class AudioMediaEncoder extends MediaEncoder {
encode(null, 0, getPresentationTime()); encode(null, 0, getPresentationTime());
drain(false); drain(false);
mAudioRecord.stop(); mAudioRecord.stop();
mAudioRecord.release();
mAudioRecord = null;
synchronized (mLock) { synchronized (mLock) {
mLock.notify(); mLock.notify();
} }

@ -77,7 +77,7 @@ abstract class MediaEncoder {
*/ */
protected void encode(final ByteBuffer buffer, final int length, final long presentationTimeUs) { protected void encode(final ByteBuffer buffer, final int length, final long presentationTimeUs) {
final ByteBuffer[] inputBuffers = mMediaCodec.getInputBuffers(); final ByteBuffer[] inputBuffers = mMediaCodec.getInputBuffers();
while (true) { // TODO: stop if stop() is called! while (true) {
final int inputBufferIndex = mMediaCodec.dequeueInputBuffer(TIMEOUT_USEC); final int inputBufferIndex = mMediaCodec.dequeueInputBuffer(TIMEOUT_USEC);
if (inputBufferIndex >= 0) { if (inputBufferIndex >= 0) {
final ByteBuffer inputBuffer = inputBuffers[inputBufferIndex]; final ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];

@ -74,6 +74,7 @@ class MediaEncoderEngine {
if (!mMediaMuxerStarted) { if (!mMediaMuxerStarted) {
throw new IllegalStateException("Trying to write before muxer started"); throw new IllegalStateException("Trying to write before muxer started");
} }
Log.e("MediaEncoderEngine", "Writing data." + track);
mMediaMuxer.writeSampleData(track, encodedData, info); mMediaMuxer.writeSampleData(track, encodedData, info);
} }
} }
@ -107,7 +108,6 @@ class MediaEncoderEngine {
for (MediaEncoder encoder : mEncoders) { for (MediaEncoder encoder : mEncoders) {
encoder.stop(); encoder.stop();
} }
onStop.run();
for (MediaEncoder encoder : mEncoders) { for (MediaEncoder encoder : mEncoders) {
encoder.release(); encoder.release();
} }
@ -118,6 +118,7 @@ class MediaEncoderEngine {
mMediaMuxer.release(); mMediaMuxer.release();
mMediaMuxer = null; mMediaMuxer = null;
} }
onStop.run();
mMediaMuxerStartCount = 0; mMediaMuxerStartCount = 0;
mMediaMuxerStarted = false; mMediaMuxerStarted = false;
} }

Loading…
Cancel
Save