Skip to content

Commit

Permalink
Merge branch 'agersant-shuffle-fix' into minor
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanheise committed Nov 17, 2022
2 parents 532e6f9 + bbd53d9 commit 9526090
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
4 changes: 4 additions & 0 deletions just_audio_background/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.0.1-beta.8

* Fix performance issues when shuffleMode is enabled (@agersant)

## 0.0.1-beta.7

* Fix build when targeting Android 13.
Expand Down
45 changes: 29 additions & 16 deletions just_audio_background/lib/just_audio_background.dart
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@ class _PlayerAudioHandler extends BaseAudioHandler
_Seeker? _seeker;
AudioServiceRepeatMode _repeatMode = AudioServiceRepeatMode.none;
AudioServiceShuffleMode _shuffleMode = AudioServiceShuffleMode.none;
List<int> _shuffleIndices = [];
List<int> _shuffleIndicesInv = [];
List<int> _effectiveIndices = [];
List<int> _effectiveIndicesInv = [];

Future<AudioPlayerPlatform> get _player => _playerCompleter.future;
int? get index => _justAudioEvent.currentIndex;
Expand Down Expand Up @@ -429,6 +433,7 @@ class _PlayerAudioHandler extends BaseAudioHandler

Future<LoadResponse> customLoad(LoadRequest request) async {
_source = request.audioSourceMessage;
_updateShuffleIndices();
_updateQueue();
final response = await (await _player).load(LoadRequest(
audioSourceMessage: _source!,
Expand All @@ -449,6 +454,7 @@ class _PlayerAudioHandler extends BaseAudioHandler
Future<SetShuffleOrderResponse> customSetShuffleOrder(
SetShuffleOrderRequest request) async {
_source = request.audioSourceMessage;
_updateShuffleIndices();
_broadcastStateIfActive();
return await (await _player).setShuffleOrder(SetShuffleOrderRequest(
audioSourceMessage: _source!,
Expand All @@ -459,6 +465,8 @@ class _PlayerAudioHandler extends BaseAudioHandler
ConcatenatingInsertAllRequest request) async {
final cat = _source!.findCat(request.id)!;
cat.children.insertAll(request.index, request.children);
_updateShuffleIndices();
_broadcastStateIfActive();
_updateQueue();
return await (await _player).concatenatingInsertAll(request);
}
Expand All @@ -467,6 +475,8 @@ class _PlayerAudioHandler extends BaseAudioHandler
ConcatenatingRemoveRangeRequest request) async {
final cat = _source!.findCat(request.id)!;
cat.children.removeRange(request.startIndex, request.endIndex);
_updateShuffleIndices();
_broadcastStateIfActive();
_updateQueue();
return await (await _player).concatenatingRemoveRange(request);
}
Expand All @@ -476,6 +486,8 @@ class _PlayerAudioHandler extends BaseAudioHandler
final cat = _source!.findCat(request.id)!;
cat.children
.insert(request.newIndex, cat.children.removeAt(request.currentIndex));
_updateShuffleIndices();
_broadcastStateIfActive();
_updateQueue();
return await (await _player).concatenatingMove(request);
}
Expand All @@ -496,33 +508,33 @@ class _PlayerAudioHandler extends BaseAudioHandler
queue.add(sequence.map((source) => source.tag as MediaItem).toList());
}

List<IndexedAudioSourceMessage> get sequence => _source?.sequence ?? [];
List<int> get shuffleIndices => _source?.shuffleIndices ?? [];
List<int> get effectiveIndices => _shuffleMode != AudioServiceShuffleMode.none
? shuffleIndices
: List.generate(sequence.length, (i) => i);
List<int> get shuffleIndicesInv {
final inv = List.filled(effectiveIndices.length, 0);
for (var i = 0; i < effectiveIndices.length; i++) {
inv[effectiveIndices[i]] = i;
void _updateShuffleIndices() {
_shuffleIndices = _source?.shuffleIndices ?? [];
_effectiveIndices = _shuffleMode != AudioServiceShuffleMode.none
? _shuffleIndices
: List.generate(sequence.length, (i) => i);
_shuffleIndicesInv = List.filled(_effectiveIndices.length, 0);
for (var i = 0; i < _effectiveIndices.length; i++) {
_shuffleIndicesInv[_effectiveIndices[i]] = i;
}
return inv;
_effectiveIndicesInv = _shuffleMode != AudioServiceShuffleMode.none
? _shuffleIndicesInv
: List.generate(sequence.length, (i) => i);
}

List<int> get effectiveIndicesInv =>
_shuffleMode != AudioServiceShuffleMode.none
? shuffleIndicesInv
: List.generate(sequence.length, (i) => i);
List<IndexedAudioSourceMessage> get sequence => _source?.sequence ?? [];
List<int> get shuffleIndices => _shuffleIndices;
List<int> get effectiveIndices => _effectiveIndices;
List<int> get shuffleIndicesInv => _shuffleIndicesInv;
List<int> get effectiveIndicesInv => _effectiveIndicesInv;
int get nextIndex => getRelativeIndex(1);
int get previousIndex => getRelativeIndex(-1);
bool get hasNext => nextIndex != -1;
bool get hasPrevious => previousIndex != -1;

int getRelativeIndex(int offset) {
if (_repeatMode == AudioServiceRepeatMode.one) return index!;
final effectiveIndices = this.effectiveIndices;
if (effectiveIndices.isEmpty) return -1;
final effectiveIndicesInv = this.effectiveIndicesInv;
if (index! >= effectiveIndicesInv.length) return -1;
final invPos = effectiveIndicesInv[index!];
var newInvPos = invPos + offset;
Expand Down Expand Up @@ -614,6 +626,7 @@ class _PlayerAudioHandler extends BaseAudioHandler
@override
Future<void> setShuffleMode(AudioServiceShuffleMode shuffleMode) async {
_shuffleMode = shuffleMode;
_updateShuffleIndices();
_broadcastStateIfActive();
(await _player).setShuffleMode(SetShuffleModeRequest(
shuffleMode: ShuffleModeMessage.values[
Expand Down
2 changes: 1 addition & 1 deletion just_audio_background/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: just_audio_background
description: An add-on for just_audio that supports background playback and media notifications.
homepage: https://github.com/ryanheise/just_audio/tree/master/just_audio_background
version: 0.0.1-beta.7
version: 0.0.1-beta.8

dependencies:
just_audio_platform_interface: ^4.2.0
Expand Down

0 comments on commit 9526090

Please sign in to comment.