I have the lines of code below. I am trying to play a stream and I am using AudioTrack.
CoroutineScope(Dispatchers.IO).launch {
withContext(Dispatchers.IO) {
try {
decodedFrames.forEach {frame ->
audioTrack?.write(frame,0,frame.size)
}
} catch (e : Exception) {
Log.e("ERROR",e.message!!)
}
}
}
When I use write method without a coroutine context, the sound is good but the app becomes laggy. If I use the write method, with WRITE_NON_BLOCKING, then the sound is being interrupted, but the application is not laggy anymore. When I use the write method in a coroutine context, like the one above, then the sound is good, the app is not laggy anymore but after a few seconds or minutes, the app crashes with the error below. The error below is a very long error indicating that the problematic line is the audiotrack?.write one
releaseBuffer: mUnreleased out of range, !(stepCount:952 <= mUnreleased:0 <= mFrameCount:3848), BufferSizeInFrames:3848 2023-11-09 07:52:54.652 18535-18615 A runtime.cc:691] Runtime aborting... runtime.cc:691] Dumping all threads without mutator lock held runtime.cc:691] All threads: runtime.cc:691] DALVIK THREADS (26): runtime.cc:691] "main" prio=10 tid=1 Native runtime.cc:691] | group="" sCount=1 ucsCount=0 flags=1 obj=0x73500d40 self=0xb40000781c02c7b0 runtime.cc:691] | sysTid=18535 nice=-10 cgrp=top-app sched=0/0 handle=0x79ce39d500 runtime.cc:691] | state=S schedstat=( 10320252622 172287193 3307 ) utm=972 stm=59 core=5 HZ=100 runtime.cc:691] | stack=0x7fe496a000-0x7fe496c000 stackSize=8188KB runtime.cc:691] | held mutexes= runtime.cc:691] native: #00 pc 0004e1dc /apex/com.android.runtime/lib64/bionic/libc.so (syscall+28) (BuildId: 7464f932fe1115e080f728808bf0ade7) runtime.cc:691] native: #01 pc 0023746c /apex/com.android.art/lib64/libart.so (art::ConditionVariable::WaitHoldingLocks+140) (BuildId: 4cfdaa9e5146c43e20ae36ee1caf9b7f) runtime.cc:691] native: #02 pc 004676ac /apex/com.android.art/lib64/libart.so (artQuickGenericJniEndTrampoline+1008) (BuildId: 4cfdaa9e5146c43e20ae36ee1caf9b7f) runtime.cc:691] native: #03 pc 0034aa40 /apex/com.android.art/lib64/libart.so (art_quick_generic_jni_trampoline+160) (BuildId: 4cfdaa9e5146c43e20ae36ee1caf9b7f) runtime.cc:691] native: #04 pc 00333fa4 /apex/com.android.art/lib64/libart.so (art_quick_invoke_stub+612) (BuildId: 4cfdaa9e5146c43e20ae36ee1caf9b7f) runtime.cc:691] native: #05 pc 00511078 /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall<false>+1976) (BuildId: 4cfdaa9e5146c43e20ae36ee1caf9b7f) runtime.cc:691] native: #06 pc 0049e538 /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp<false>+2040) (BuildId: 4cfdaa9e5146c43e20ae36ee1caf9b7f) runtime.cc:691] native: #07 pc 0034d1d8 /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+8) (BuildId: 4cfdaa9e5146c43e20ae36ee1caf9b7f) runtime.cc:691] native: #08 pc 001fe800 /system/framework/framework.jar (android.os.MessageQueue.next) runtime.cc:691] native: #09 pc 00378bb0 /apex/com.android.art/lib64/libart.so (art::interpreter::Execute +232) (BuildId: 4cfdaa9e5146c43e20ae36ee1caf9b7f) runtime.cc:691] native: #10 pc 00511d44 /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall<false>+5252) (BuildId: 4cfdaa9e5146c43e20ae36ee1caf9b7f) runtime.cc:691] native: #11 pc 0049e100 /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp<false>+960) (BuildId: 4cfdaa9e5146c43e20ae36ee1caf9b7f) runtime.cc:691] native: #12 pc 0034d1d8 /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+8) (BuildId: 4cfdaa9e5146c43e20ae36ee1caf9b7f) runtime.cc:691] native: #13 pc 001fd700 /system/framework/framework.jar (android.os.Looper.loopOnce) runtime.cc:691] native: #14 pc 00378bb0 /apex/com.android.art/lib64/libart.so (art::interpreter::Execute +232) (BuildId: 4cfdaa9e5146c43e20ae36ee1caf9b7f) runtime.cc:691] native: #15 pc 00511d44 /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall<false>+5252) (BuildId: 4cfdaa9e5146c43e20ae36ee1caf9b7f) runtime.cc:691] native: #16 pc 0049e470 /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp<false>+1840) (BuildId: 4cfdaa9e5146c43e20ae36ee1caf9b7f) runtime.cc:691] native: #17 pc 0034d1d8 /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+8) (BuildId: 4cfdaa9e5146c43e20ae36ee1caf9b7f) runtime.cc:691] native: #18 pc 001fdf24 /system/framework/framework.jar (android.os.Looper.loop) runtime.cc:691] native: #19 pc 00378bb0 /apex/com.android.art/lib64/libart.so (art::interpreter::Execute +232) (BuildId: 4cfdaa9e5146c43e20ae36ee1caf9b7f) runtime.cc:691] native: #20 pc 00511d44 /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall<false>+5252) (BuildId: 4cfdaa9e5146c43e20ae36ee1caf9b7f)
I tried all the solutions above but nothing works for me.