Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,13 @@ class AudioFocusManager(
) {
private val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager

@Volatile
private var currentFocusState = FocusState.NO_FOCUS

@Volatile
private var wasPlayingBeforeTransientLoss = false

@Volatile
private var wasDucked = false
Comment thread
gagip marked this conversation as resolved.

private val audioFocusChangeListener =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,11 @@ class PlayQueueManager(
* 다음 곡 재생
*/
override suspend fun playNext() {
check(_playQueue.value != null) { "재생 큐가 설정되지 않은 상태에서 playNext() 호출" }
playlistMutex.withLock { playNextInternal() }
val action = playlistMutex.withLock {
check(_playQueue.value != null) { "재생 큐가 설정되지 않은 상태에서 playNext() 호출" }
computeNextAction()
}
action()
}

/**
Expand Down Expand Up @@ -132,30 +135,33 @@ class PlayQueueManager(
}
}

private suspend fun playNextInternal() {
val current = _playQueue.value ?: return
// 락 안에서 상태를 변경하고, 락 밖에서 실행할 side effect(emit/play)를 반환한다.
private fun computeNextAction(): suspend () -> Unit {
val current = _playQueue.value ?: return {}

when (_repeatMode.value) {
RepeatMode.ONE -> {
current.currentTrack?.let { audioController.prepareTrack(it) }
if (_repeatMode.value == RepeatMode.ONE) {
val track = current.currentTrack
return {
track?.let { audioController.prepareTrack(it) }
audioController.play()
return
}
// NONE/ALL은 공통 로직(다음 인덱스 계산)으로 fall-through
RepeatMode.NONE,
RepeatMode.ALL,
-> Unit
}

val nextIndex = getNextIndex(current)
if (nextIndex != null) {
return if (nextIndex != null) {
val updated = current.copy(currentIndex = nextIndex)
_playQueue.value = updated
updated.currentTrack?.let { audioController.prepareTrack(it) }
audioController.play()
val track = updated.currentTrack
{
track?.let { audioController.prepareTrack(it) }
audioController.play()
}
} else {
_events.emit(PlaylistEvent.PlaylistEnded)
current.currentTrack?.let { audioController.prepareTrack(it) }
val track = current.currentTrack
{
_events.emit(PlaylistEvent.PlaylistEnded)
track?.let { audioController.prepareTrack(it) }
}
}
}

Expand Down
Loading