Skip to content

Commit

Permalink
fix(Android): Sync recording thread start.
Browse files Browse the repository at this point in the history
fixes #435
  • Loading branch information
llfbandit committed Dec 23, 2024
1 parent 6571dcd commit 4c4ba96
Showing 1 changed file with 8 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.llfbandit.record.record.format.Format
import com.llfbandit.record.record.format.OpusFormat
import com.llfbandit.record.record.format.PcmFormat
import com.llfbandit.record.record.format.WaveFormat
import java.util.concurrent.CountDownLatch
import java.util.concurrent.Executors
import java.util.concurrent.Semaphore
import java.util.concurrent.atomic.AtomicBoolean
Expand Down Expand Up @@ -84,6 +85,8 @@ class RecordThread(
fun getAmplitude(): Double = mPcmReader?.getAmplitude() ?: -160.0

fun startRecording() {
val startLatch = CountDownLatch(1)

mExecutorService.execute {
try {
val format = selectFormat()
Expand All @@ -97,6 +100,8 @@ class RecordThread(

recordState()

startLatch.countDown()

while (isRecording()) {
if (isPaused()) {
mIsPausedSem.acquire()
Expand All @@ -112,9 +117,12 @@ class RecordThread(
} catch (ex: Exception) {
recorderListener.onFailure(ex)
} finally {
startLatch.countDown()
stopAndRelease()
}
}

startLatch.await()
}

private fun stopAndRelease() {
Expand Down

0 comments on commit 4c4ba96

Please sign in to comment.