Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 2.1.0 (#unreleased)

- Feature [#468](https://github.com/SimformSolutionsPvtLtd/audio_waveforms/pull/468) - Add macOS support
- Fixed [#482](https://github.com/SimformSolutionsPvtLtd/audio_waveforms/issues/482) - Prevent Android crash on stop by checking muxer state before stopping

## 2.0.2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,22 @@ class AudioRecorder : PluginRegistry.RequestPermissionsResultListener {
)
wavEncoder?.start(result)
} else {
commonEncoder.initCodec(recorderSettings = recorderSettings!!, result = result) {
recordingThread?.join()
}
commonEncoder.initCodec(recorderSettings = recorderSettings!!, result = result)
}
val buffer = ByteArray(bufferSize!!)
recordingThread = Thread {
while (recorderState == RecorderState.Recording || recorderState == RecorderState.Paused) {
if (recorderState == RecorderState.Recording) {
val read = audioRecord?.read(buffer, 0, buffer.size) ?: 0

if (read < 0) {
// Negative return is an AudioRecord error (e.g. ERROR_INVALID_OPERATION,
// ERROR_DEAD_OBJECT). Reading again would keep failing, so stop cleanly.
Log.e(LOG_TAG, "AudioRecord.read() error: $read")
recorderState = RecorderState.Stopped
break
}

if (read > 0) {
val audioData = buffer.copyOf(read)
if (encoder?.encodeForWav == true) {
Expand All @@ -169,12 +175,15 @@ class AudioRecorder : PluginRegistry.RequestPermissionsResultListener {

fun stop(result: Result) {
try {
// Stop the read loop first, then unblock and join the recording thread so it
// can never call audioRecord.read() after the AudioRecord is released below.
recorderState = RecorderState.Stopped
audioRecord?.stop()
recordingThread?.join()
recordingThread = null
totalSamples = 0L
recorderState = RecorderState.Stopped
if (encoder?.encodeForWav == true) {
wavEncoder?.stop(result)
recordingThread?.join()
sendRecordingResult(result)
} else {
commonEncoder.setOnEncodingCompleted {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,11 @@ class CommonEncoder {
try {
mediaCodec.stop()
mediaCodec.release()
mediaMuxer?.stop()
// Only stop the muxer if it was actually started; stopping an unstarted muxer
// throws and triggers the native MPEG4Writer "track not started" crash.
if (isMuxerStarted) {
mediaMuxer?.stop()
}
mediaMuxer?.release()
outputStream.close()
handlerThread.quitSafely()
Expand Down