Skip to content

Commit

Permalink
Fix bugs in web implementation when removing playlist items.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanheise committed Jan 20, 2021
1 parent 14c3684 commit b58b475
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
16 changes: 8 additions & 8 deletions just_audio/lib/just_audio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1685,7 +1685,7 @@ class ConcatenatingAudioSource extends AudioSource {
id: _id,
index: index,
children: [audioSource._toMessage()],
shuffleOrder: _shuffleOrder.indices));
shuffleOrder: List.of(_shuffleOrder.indices)));
}
}

Expand All @@ -1701,7 +1701,7 @@ class ConcatenatingAudioSource extends AudioSource {
id: _id,
index: index,
children: [audioSource._toMessage()],
shuffleOrder: _shuffleOrder.indices));
shuffleOrder: List.of(_shuffleOrder.indices)));
}
}

Expand All @@ -1720,7 +1720,7 @@ class ConcatenatingAudioSource extends AudioSource {
id: _id,
index: index,
children: children.map((child) => child._toMessage()).toList(),
shuffleOrder: _shuffleOrder.indices));
shuffleOrder: List.of(_shuffleOrder.indices)));
}
}

Expand All @@ -1738,7 +1738,7 @@ class ConcatenatingAudioSource extends AudioSource {
id: _id,
index: index,
children: children.map((child) => child._toMessage()).toList(),
shuffleOrder: _shuffleOrder.indices));
shuffleOrder: List.of(_shuffleOrder.indices)));
}
}

Expand All @@ -1754,7 +1754,7 @@ class ConcatenatingAudioSource extends AudioSource {
id: _id,
startIndex: index,
endIndex: index + 1,
shuffleOrder: _shuffleOrder.indices));
shuffleOrder: List.of(_shuffleOrder.indices)));
}
}

Expand All @@ -1770,7 +1770,7 @@ class ConcatenatingAudioSource extends AudioSource {
id: _id,
startIndex: start,
endIndex: end,
shuffleOrder: _shuffleOrder.indices));
shuffleOrder: List.of(_shuffleOrder.indices)));
}
}

Expand All @@ -1786,7 +1786,7 @@ class ConcatenatingAudioSource extends AudioSource {
id: _id,
currentIndex: currentIndex,
newIndex: newIndex,
shuffleOrder: _shuffleOrder.indices));
shuffleOrder: List.of(_shuffleOrder.indices)));
}
}

Expand All @@ -1801,7 +1801,7 @@ class ConcatenatingAudioSource extends AudioSource {
id: _id,
startIndex: 0,
endIndex: children.length,
shuffleOrder: _shuffleOrder.indices));
shuffleOrder: List.of(_shuffleOrder.indices)));
}
}

Expand Down
20 changes: 14 additions & 6 deletions just_audio_web/lib/just_audio_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ class Html5AudioPlayer extends JustAudioPlayer {
_durationCompleter?.completeError(_audioElement.error);
});
_audioElement.addEventListener('ended', (event) async {
_currentAudioSourcePlayer.complete();
_currentAudioSourcePlayer?.complete();
});
_audioElement.addEventListener('timeupdate', (event) {
_currentAudioSourcePlayer.timeUpdated(_audioElement.currentTime);
_currentAudioSourcePlayer?.timeUpdated(_audioElement.currentTime);
});
_audioElement.addEventListener('loadstart', (event) {
transition(ProcessingStateMessage.buffering);
Expand Down Expand Up @@ -166,7 +166,9 @@ class Html5AudioPlayer extends JustAudioPlayer {

// TODO: Improve efficiency.
IndexedAudioSourcePlayer get _currentAudioSourcePlayer =>
_audioSourcePlayer != null && _index < _audioSourcePlayer.sequence.length
_audioSourcePlayer != null &&
_audioSourcePlayer.sequence.isNotEmpty &&
_index < _audioSourcePlayer.sequence.length
? _audioSourcePlayer.sequence[_index]
: null;

Expand Down Expand Up @@ -313,6 +315,7 @@ class Html5AudioPlayer extends JustAudioPlayer {
if (request.index <= _index) {
_index += request.children.length;
}
broadcastPlaybackEvent();
return ConcatenatingInsertAllResponse();
}

Expand All @@ -323,13 +326,14 @@ class Html5AudioPlayer extends JustAudioPlayer {
// Pause if removing current item
_currentAudioSourcePlayer.pause();
}
_concatenating(request.id).setShuffleOrder(request.shuffleOrder);
_concatenating(request.id)
.removeRange(request.startIndex, request.endIndex);
_concatenating(request.id).setShuffleOrder(request.shuffleOrder);
if (_index >= request.startIndex && _index < request.endIndex) {
// Skip backward if there's nothing after this
if (request.startIndex >= _audioSourcePlayer.sequence.length) {
_index = request.startIndex - 1;
if (_index < 0) _index = 0;
} else {
_index = request.startIndex;
}
Expand All @@ -342,6 +346,7 @@ class Html5AudioPlayer extends JustAudioPlayer {
// Reflect that the current item has shifted its position
_index -= (request.endIndex - request.startIndex);
}
broadcastPlaybackEvent();
return ConcatenatingRemoveRangeResponse();
}

Expand All @@ -357,6 +362,7 @@ class Html5AudioPlayer extends JustAudioPlayer {
} else if (request.currentIndex > _index && request.newIndex <= _index) {
_index++;
}
broadcastPlaybackEvent();
return ConcatenatingMoveResponse();
}

Expand All @@ -374,10 +380,12 @@ class Html5AudioPlayer extends JustAudioPlayer {
}

@override
Duration getCurrentPosition() => _currentAudioSourcePlayer?.position;
Duration getCurrentPosition() =>
_currentAudioSourcePlayer?.position ?? Duration.zero;

@override
Duration getBufferedPosition() => _currentAudioSourcePlayer?.bufferedPosition;
Duration getBufferedPosition() =>
_currentAudioSourcePlayer?.bufferedPosition ?? Duration.zero;

@override
Duration getDuration() => _currentAudioSourcePlayer?.duration;
Expand Down

0 comments on commit b58b475

Please sign in to comment.