Skip to content

Commit 71af261

Browse files
authored
fix(android): interrupt handling and main looper usage (#2649)
1 parent 6aaec2e commit 71af261

3 files changed

Lines changed: 21 additions & 12 deletions

File tree

packages/kotlin/src/android/src/main/java/alphaTab/AlphaTabView.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ class AlphaTabView : RelativeLayout {
7878
}
7979

8080
override fun onDetachedFromWindow() {
81-
super.onDetachedFromWindow()
8281
_api.destroy()
82+
super.onDetachedFromWindow()
8383
}
8484

8585
private fun init(context: Context) {

packages/kotlin/src/android/src/main/java/alphaTab/platform/android/AndroidAudioWorker.kt

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,28 @@ internal class AndroidAudioWorker(
5656

5757
private fun writeSamples() {
5858
while (!_stopped) {
59-
if (_track.playState == AudioTrack.PLAYSTATE_PLAYING) {
60-
val samplesFromBuffer = _output.read(_buffer, 0, _buffer.size)
61-
if (_previousPosition == -1) {
62-
_previousPosition = _track.playbackHeadPosition
63-
_track.getTimestamp(_timestamp)
59+
try {
60+
if (_track.playState == AudioTrack.PLAYSTATE_PLAYING) {
61+
val samplesFromBuffer = _output.read(_buffer, 0, _buffer.size)
62+
if (_previousPosition == -1) {
63+
_previousPosition = _track.playbackHeadPosition
64+
_track.getTimestamp(_timestamp)
65+
}
66+
_track.write(_buffer, 0, samplesFromBuffer, AudioTrack.WRITE_BLOCKING)
67+
} else {
68+
_playingSemaphore.acquire() // wait for playing to start
69+
_playingSemaphore.release() // release semaphore for others
6470
}
65-
_track.write(_buffer, 0, samplesFromBuffer, AudioTrack.WRITE_BLOCKING)
66-
} else {
67-
_playingSemaphore.acquire() // wait for playing to start
68-
_playingSemaphore.release() // release semaphore for others
71+
} catch (_: InterruptedException) {
72+
Thread.currentThread().interrupt()
73+
break;
6974
}
7075
}
7176
}
7277

7378
fun close() {
74-
_playingSemaphore.release() // proceed thread
7579
_stopped = true
80+
_playingSemaphore.release() // proceed thread
7681
_track.stop()
7782
_writeThread!!.interrupt()
7883
_writeThread!!.join()

packages/kotlin/src/android/src/main/java/alphaTab/platform/android/AndroidUiFacade.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import alphaTab.synth.IAudioExporterWorker
2626
import android.annotation.SuppressLint
2727
import android.graphics.Bitmap
2828
import android.os.Handler
29+
import android.os.Looper
2930
import android.view.MotionEvent
3031
import android.view.View
3132
import android.view.ViewGroup
@@ -54,6 +55,8 @@ internal class AndroidUiFacade : IUiFacade<AlphaTabView> {
5455
private val _renderSurface: AlphaTabRenderSurface
5556
private val _renderWrapper: RelativeLayout
5657

58+
private val _uiLooper:Handler;
59+
5760
public constructor(
5861
outerScroll: SuspendableHorizontalScrollView,
5962
innerScroll: SuspendableScrollView,
@@ -64,6 +67,7 @@ internal class AndroidUiFacade : IUiFacade<AlphaTabView> {
6467
_innerScroll = innerScroll
6568
_renderSurface = renderSurface
6669
_renderWrapper = renderWrapper
70+
_uiLooper = Handler(Looper.getMainLooper())
6771

6872
rootContainer =
6973
AndroidRootViewContainer(outerScroll, innerScroll, renderSurface, this::beginInvoke)
@@ -163,7 +167,7 @@ internal class AndroidUiFacade : IUiFacade<AlphaTabView> {
163167
}
164168

165169
private fun postToUIThread(action: () -> Unit) {
166-
this._renderSurface.post(action)
170+
_uiLooper.post(action)
167171
}
168172

169173
private fun openDefaultSoundFont(): InputStream {

0 commit comments

Comments
 (0)