diff --git a/just_audio_background/CHANGELOG.md b/just_audio_background/CHANGELOG.md index 69e287b63..f1b2ce372 100644 --- a/just_audio_background/CHANGELOG.md +++ b/just_audio_background/CHANGELOG.md @@ -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. diff --git a/just_audio_background/lib/just_audio_background.dart b/just_audio_background/lib/just_audio_background.dart index c3929098f..fd9073e09 100644 --- a/just_audio_background/lib/just_audio_background.dart +++ b/just_audio_background/lib/just_audio_background.dart @@ -341,6 +341,10 @@ class _PlayerAudioHandler extends BaseAudioHandler _Seeker? _seeker; AudioServiceRepeatMode _repeatMode = AudioServiceRepeatMode.none; AudioServiceShuffleMode _shuffleMode = AudioServiceShuffleMode.none; + List _shuffleIndices = []; + List _shuffleIndicesInv = []; + List _effectiveIndices = []; + List _effectiveIndicesInv = []; Future get _player => _playerCompleter.future; int? get index => _justAudioEvent.currentIndex; @@ -429,6 +433,7 @@ class _PlayerAudioHandler extends BaseAudioHandler Future customLoad(LoadRequest request) async { _source = request.audioSourceMessage; + _updateShuffleIndices(); _updateQueue(); final response = await (await _player).load(LoadRequest( audioSourceMessage: _source!, @@ -449,6 +454,7 @@ class _PlayerAudioHandler extends BaseAudioHandler Future customSetShuffleOrder( SetShuffleOrderRequest request) async { _source = request.audioSourceMessage; + _updateShuffleIndices(); _broadcastStateIfActive(); return await (await _player).setShuffleOrder(SetShuffleOrderRequest( audioSourceMessage: _source!, @@ -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); } @@ -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); } @@ -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); } @@ -496,23 +508,25 @@ class _PlayerAudioHandler extends BaseAudioHandler queue.add(sequence.map((source) => source.tag as MediaItem).toList()); } - List get sequence => _source?.sequence ?? []; - List get shuffleIndices => _source?.shuffleIndices ?? []; - List get effectiveIndices => _shuffleMode != AudioServiceShuffleMode.none - ? shuffleIndices - : List.generate(sequence.length, (i) => i); - List 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 get effectiveIndicesInv => - _shuffleMode != AudioServiceShuffleMode.none - ? shuffleIndicesInv - : List.generate(sequence.length, (i) => i); + List get sequence => _source?.sequence ?? []; + List get shuffleIndices => _shuffleIndices; + List get effectiveIndices => _effectiveIndices; + List get shuffleIndicesInv => _shuffleIndicesInv; + List get effectiveIndicesInv => _effectiveIndicesInv; int get nextIndex => getRelativeIndex(1); int get previousIndex => getRelativeIndex(-1); bool get hasNext => nextIndex != -1; @@ -520,9 +534,7 @@ class _PlayerAudioHandler extends BaseAudioHandler 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; @@ -614,6 +626,7 @@ class _PlayerAudioHandler extends BaseAudioHandler @override Future setShuffleMode(AudioServiceShuffleMode shuffleMode) async { _shuffleMode = shuffleMode; + _updateShuffleIndices(); _broadcastStateIfActive(); (await _player).setShuffleMode(SetShuffleModeRequest( shuffleMode: ShuffleModeMessage.values[ diff --git a/just_audio_background/pubspec.yaml b/just_audio_background/pubspec.yaml index 314b81f9e..6f481fc89 100644 --- a/just_audio_background/pubspec.yaml +++ b/just_audio_background/pubspec.yaml @@ -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