working arbitrary track queueing

This commit is contained in:
minjaesong
2024-07-08 18:06:44 +09:00
parent 657c8051f8
commit 86c6f008f9
4 changed files with 37 additions and 11 deletions

View File

@@ -114,14 +114,14 @@ object MusicService : TransactionListener() {
override fun onFailure(e: Throwable, state: TransactionState) {
printdbg(this, "FIREPLAY resume OK but startMusic failed, entering intermission")
enterSTATE_INTERMISSION(getRandomMusicInterval()) // will try again after a random interval
enterIntermissionAndWaitForPlaylist() // will try again
}
})
},
/* onFailure: (Throwable) -> Unit */
{
printdbg(this, "FIREPLAY resume failed, entering intermission")
enterSTATE_INTERMISSION(getRandomMusicInterval()) // will try again after a random interval
enterIntermissionAndWaitForPlaylist() // will try again
},
// onFinally: () -> Unit
{
@@ -297,7 +297,6 @@ object MusicService : TransactionListener() {
}
override fun onSuccess(state: TransactionState) {
enterSTATE_INTERMISSION(0f)
enterSTATE_FIREPLAY()
onSuccess()
@@ -320,8 +319,8 @@ object MusicService : TransactionListener() {
App.audioMixer.requestFadeOut(App.audioMixer.musicTrack) {
try {
// callback: play prev song in the playlist
// TODO queue the nth song on the playlist, the actual playback will be done by the state machine update
// queue the nth song on the playlist, the actual playback will be done by the state machine update
(state["currentPlaylist"] as TerrarumMusicPlaylist).queueNthSong(index)
fadedOut = true
}
@@ -335,6 +334,8 @@ object MusicService : TransactionListener() {
}
override fun onSuccess(state: TransactionState) {
enterSTATE_INTERMISSION(0f)
enterSTATE_FIREPLAY()
onSuccess()
}
override fun onFailure(e: Throwable, state: TransactionState) {

View File

@@ -84,9 +84,21 @@ class TerrarumMusicPlaylist(
}
fun queueNthSong(n: Int): MusicContainer {
checkRefill()
internalIndices.add(currentIndexCursor, n)
currentIndexCursor -= 1
if (shuffled) {
internalIndices.clear()
refillInternalIndices()
internalIndices.add(n)
refillInternalIndices()
currentIndexCursor = musicList.size - 1
}
else {
internalIndices.clear()
refillInternalIndices()
refillInternalIndices()
currentIndexCursor = musicList.size - 1 + n
checkRefill()
}
return musicList[internalIndices[currentIndexCursor]]
}

View File

@@ -33,6 +33,17 @@ abstract class TransactionListener {
val transactionLockingClass: AtomicReference<Transaction?> = AtomicReference(null)
val transactionLocked: Boolean; get() = (transactionLockingClass.get() != null)
private fun acquireLock(locker: Transaction) {
synchronized(this) {
transactionLockingClass.set(locker)
}
}
private fun releaseLock() {
synchronized(this) {
transactionLockingClass.set(null)
}
}
/**
* Transaction modifies a given state to a new state, then applies the new state to the object.
@@ -47,7 +58,7 @@ abstract class TransactionListener {
val state = getCurrentStatusForTransaction()
val currentLock = transactionLockingClass.get()
if (currentLock == null) {
transactionLockingClass.set(transaction)
acquireLock(transaction)
try {
transaction.start(state)
// if successful:
@@ -62,7 +73,7 @@ abstract class TransactionListener {
transaction.onFailure(e, state)
}
finally {
transactionLockingClass.set(null)
releaseLock()
onFinally()
}
}