From f71288170af024390db0e321b45a9b87772b3701 Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Tue, 18 Sep 2018 00:09:18 -0300 Subject: [PATCH] Small changes to audio recording. Still bug --- .../cameraview/AudioMediaEncoder.java | 14 +++++++------- .../com/otaliastudios/cameraview/MediaEncoder.java | 2 +- .../cameraview/MediaEncoderEngine.java | 3 ++- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/cameraview/src/main/gles/com/otaliastudios/cameraview/AudioMediaEncoder.java b/cameraview/src/main/gles/com/otaliastudios/cameraview/AudioMediaEncoder.java index 4df2746c..1a39c8e7 100644 --- a/cameraview/src/main/gles/com/otaliastudios/cameraview/AudioMediaEncoder.java +++ b/cameraview/src/main/gles/com/otaliastudios/cameraview/AudioMediaEncoder.java @@ -67,18 +67,17 @@ class AudioMediaEncoder extends MediaEncoder { @Override void stop() { mRequestStop = true; - try { - synchronized (mLock) { + synchronized (mLock) { + try { mLock.wait(); - } - } catch (InterruptedException e) { - // Ignore + } catch (InterruptedException e) {} } } @Override void release() { super.release(); + mRequestStop = false; } class AudioThread extends Thread { @@ -93,8 +92,7 @@ class AudioMediaEncoder extends MediaEncoder { if (bufferSize < minBufferSize) { bufferSize = ((minBufferSize / SAMPLES_PER_FRAME) + 1) * SAMPLES_PER_FRAME * 2; } - mAudioRecord = new AudioRecord( - MediaRecorder.AudioSource.CAMCORDER, SAMPLE_RATE, + mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.CAMCORDER, SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize); } @@ -120,6 +118,8 @@ class AudioMediaEncoder extends MediaEncoder { encode(null, 0, getPresentationTime()); drain(false); mAudioRecord.stop(); + mAudioRecord.release(); + mAudioRecord = null; synchronized (mLock) { mLock.notify(); } diff --git a/cameraview/src/main/gles/com/otaliastudios/cameraview/MediaEncoder.java b/cameraview/src/main/gles/com/otaliastudios/cameraview/MediaEncoder.java index 4d5b49f3..977e777b 100644 --- a/cameraview/src/main/gles/com/otaliastudios/cameraview/MediaEncoder.java +++ b/cameraview/src/main/gles/com/otaliastudios/cameraview/MediaEncoder.java @@ -77,7 +77,7 @@ abstract class MediaEncoder { */ protected void encode(final ByteBuffer buffer, final int length, final long presentationTimeUs) { final ByteBuffer[] inputBuffers = mMediaCodec.getInputBuffers(); - while (true) { // TODO: stop if stop() is called! + while (true) { final int inputBufferIndex = mMediaCodec.dequeueInputBuffer(TIMEOUT_USEC); if (inputBufferIndex >= 0) { final ByteBuffer inputBuffer = inputBuffers[inputBufferIndex]; diff --git a/cameraview/src/main/gles/com/otaliastudios/cameraview/MediaEncoderEngine.java b/cameraview/src/main/gles/com/otaliastudios/cameraview/MediaEncoderEngine.java index 5f75a0e3..ff963d6d 100644 --- a/cameraview/src/main/gles/com/otaliastudios/cameraview/MediaEncoderEngine.java +++ b/cameraview/src/main/gles/com/otaliastudios/cameraview/MediaEncoderEngine.java @@ -74,6 +74,7 @@ class MediaEncoderEngine { if (!mMediaMuxerStarted) { throw new IllegalStateException("Trying to write before muxer started"); } + Log.e("MediaEncoderEngine", "Writing data." + track); mMediaMuxer.writeSampleData(track, encodedData, info); } } @@ -107,7 +108,6 @@ class MediaEncoderEngine { for (MediaEncoder encoder : mEncoders) { encoder.stop(); } - onStop.run(); for (MediaEncoder encoder : mEncoders) { encoder.release(); } @@ -118,6 +118,7 @@ class MediaEncoderEngine { mMediaMuxer.release(); mMediaMuxer = null; } + onStop.run(); mMediaMuxerStartCount = 0; mMediaMuxerStarted = false; }