Skip to content

Commit d808fc2

Browse files
committed
refactor: improve song loading skip logic, state reset conditions, and player pause timing during playback transitions
1 parent c9ce569 commit d808fc2

1 file changed

Lines changed: 28 additions & 33 deletions

File tree

lib/services/audio_service.dart

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,39 +1009,34 @@ class MusifyAudioHandler extends BaseAudioHandler {
10091009
}
10101010

10111011
Future<void> _playFromQueue(int index) async {
1012-
try {
1013-
// logger.log(
1014-
// '[PLAY_FROM_QUEUE] Called with index=$index, _currentLoadingIndex=$_currentLoadingIndex',
1015-
// null,
1016-
// null,
1017-
// );
1018-
if (index < 0 || index >= _queueList.length) {
1019-
logger.log('Invalid queue index: $index');
1020-
return;
1021-
}
1012+
if (index < 0 || index >= _queueList.length) {
1013+
logger.log('Invalid queue index: $index');
1014+
return;
1015+
}
10221016

1023-
// If already loading any song, skip the request
1024-
// UNLESS we're in the middle of handling a completion event (allow one load attempt)
1025-
if (_currentLoadingIndex >= 0 && !_completionEventPending) {
1026-
return;
1027-
}
1017+
// If already loading any song, skip the request
1018+
// UNLESS we're in the middle of handling a completion event (allow one load attempt)
1019+
if (_currentLoadingIndex == index && !_completionEventPending) {
1020+
return;
1021+
}
10281022

1029-
if (_currentLoadingIndex >= 0 &&
1030-
_completionEventPending &&
1031-
!_completionHandlerLoadStarted) {
1032-
_completionHandlerLoadStarted = true;
1033-
} else if (_currentLoadingIndex >= 0 &&
1034-
_completionEventPending &&
1035-
_completionHandlerLoadStarted) {
1036-
return;
1037-
}
1023+
if (_currentLoadingIndex >= 0 &&
1024+
_completionEventPending &&
1025+
!_completionHandlerLoadStarted) {
1026+
_completionHandlerLoadStarted = true;
1027+
} else if (_currentLoadingIndex >= 0 &&
1028+
_completionEventPending &&
1029+
_completionHandlerLoadStarted) {
1030+
return;
1031+
}
10381032

1039-
// Start new transition
1040-
_songTransitionCounter++;
1041-
final currentTransitionId = _songTransitionCounter;
1042-
_currentLoadingIndex = index;
1043-
_currentLoadingTransitionId = currentTransitionId;
1033+
// Start new transition
1034+
_songTransitionCounter++;
1035+
final currentTransitionId = _songTransitionCounter;
1036+
_currentLoadingIndex = index;
1037+
_currentLoadingTransitionId = currentTransitionId;
10441038

1039+
try {
10451040
final previousQueueIndex = _currentQueueIndex;
10461041
_currentQueueIndex = index;
10471042

@@ -1074,8 +1069,8 @@ class MusifyAudioHandler extends BaseAudioHandler {
10741069
logger.log('Error playing from queue', error: e, stackTrace: stackTrace);
10751070
_handlePlaybackError();
10761071
} finally {
1077-
// Only reset if we haven't already cleared it in the success path
1078-
if (_currentLoadingIndex >= 0) {
1072+
// Only reset if this is still the transition that started it
1073+
if (currentTransitionId == _currentLoadingTransitionId) {
10791074
_currentLoadingIndex = -1;
10801075
_currentLoadingTransitionId = -1;
10811076
}
@@ -1297,14 +1292,14 @@ class MusifyAudioHandler extends BaseAudioHandler {
12971292
}
12981293

12991294
_lastError = null;
1295+
if (audioPlayer.playing) await audioPlayer.pause();
1296+
13001297
final playback = await _resolvePlaybackSource(songData);
13011298
if (playback == null) {
13021299
_lastError = 'Failed to get song URL';
13031300
return false;
13041301
}
13051302

1306-
if (audioPlayer.playing) await audioPlayer.pause();
1307-
13081303
_emitOptimisticLoadingState(
13091304
song: songData,
13101305
includeMediaItem: true,

0 commit comments

Comments
 (0)