From c4196e80e55ad24df94508ab7159f6cd1891e68d Mon Sep 17 00:00:00 2001 From: Antoine Gersant Date: Tue, 27 Apr 2021 21:09:28 -0700 Subject: [PATCH 1/5] Repro --- just_audio/example/lib/main.dart | 48 ++++---------------------------- 1 file changed, 6 insertions(+), 42 deletions(-) diff --git a/just_audio/example/lib/main.dart b/just_audio/example/lib/main.dart index 811fb3e60..7569019f6 100644 --- a/just_audio/example/lib/main.dart +++ b/just_audio/example/lib/main.dart @@ -16,48 +16,7 @@ class MyApp extends StatefulWidget { class _MyAppState extends State { late AudioPlayer _player; - final _playlist = ConcatenatingAudioSource(children: [ - ClippingAudioSource( - start: Duration(seconds: 60), - end: Duration(seconds: 90), - child: AudioSource.uri(Uri.parse( - "https://s3.amazonaws.com/scifri-episodes/scifri20181123-episode.mp3")), - tag: AudioMetadata( - album: "Science Friday", - title: "A Salute To Head-Scratching Science (30 seconds)", - artwork: - "https://media.wnyc.org/i/1400/1400/l/80/1/ScienceFriday_WNYCStudios_1400.jpg", - ), - ), - AudioSource.uri( - Uri.parse( - "https://s3.amazonaws.com/scifri-episodes/scifri20181123-episode.mp3"), - tag: AudioMetadata( - album: "Science Friday", - title: "A Salute To Head-Scratching Science", - artwork: - "https://media.wnyc.org/i/1400/1400/l/80/1/ScienceFriday_WNYCStudios_1400.jpg", - ), - ), - AudioSource.uri( - Uri.parse("https://s3.amazonaws.com/scifri-segments/scifri201711241.mp3"), - tag: AudioMetadata( - album: "Science Friday", - title: "From Cat Rheology To Operatic Incompetence", - artwork: - "https://media.wnyc.org/i/1400/1400/l/80/1/ScienceFriday_WNYCStudios_1400.jpg", - ), - ), - AudioSource.uri( - Uri.parse("asset:///audio/nature.mp3"), - tag: AudioMetadata( - album: "Public Domain", - title: "Nature Sounds", - artwork: - "https://media.wnyc.org/i/1400/1400/l/80/1/ScienceFriday_WNYCStudios_1400.jpg", - ), - ), - ]); + final _playlist = ConcatenatingAudioSource(children: []); int _addedCount = 0; @override @@ -78,6 +37,11 @@ class _MyAppState extends State { onError: (Object e, StackTrace stackTrace) { print('A stream error occurred: $e'); }); + + _player.currentIndexStream.listen((index) { + print('currentIndexStream: $index'); + }); + try { await _player.setAudioSource(_playlist); } catch (e) { From 519d55572d2147807ce72ead1faedba860286999 Mon Sep 17 00:00:00 2001 From: Antoine Gersant Date: Fri, 11 Nov 2022 00:15:16 -0800 Subject: [PATCH 2/5] Improve performance when shuffle is enabled --- just_audio_background/CHANGELOG.md | 4 ++++ just_audio_background/lib/just_audio_background.dart | 1 + just_audio_background/pubspec.yaml | 2 +- 3 files changed, 6 insertions(+), 1 deletion(-) 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..e4fa8d60d 100644 --- a/just_audio_background/lib/just_audio_background.dart +++ b/just_audio_background/lib/just_audio_background.dart @@ -502,6 +502,7 @@ class _PlayerAudioHandler extends BaseAudioHandler ? shuffleIndices : List.generate(sequence.length, (i) => i); List get shuffleIndicesInv { + final effectiveIndices = this.effectiveIndices; final inv = List.filled(effectiveIndices.length, 0); for (var i = 0; i < effectiveIndices.length; i++) { inv[effectiveIndices[i]] = i; 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 From 206beb1c27827cf145369027f37bfa9c2a5e560a Mon Sep 17 00:00:00 2001 From: Antoine Gersant Date: Fri, 11 Nov 2022 21:32:24 -0800 Subject: [PATCH 3/5] Cache various lists of indices affected by shuffle --- .../lib/just_audio_background.dart | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/just_audio_background/lib/just_audio_background.dart b/just_audio_background/lib/just_audio_background.dart index e4fa8d60d..b7f22fddc 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; @@ -449,6 +453,7 @@ class _PlayerAudioHandler extends BaseAudioHandler Future customSetShuffleOrder( SetShuffleOrderRequest request) async { _source = request.audioSourceMessage; + _updateShuffleIndices(); _broadcastStateIfActive(); return await (await _player).setShuffleOrder(SetShuffleOrderRequest( audioSourceMessage: _source!, @@ -494,26 +499,28 @@ class _PlayerAudioHandler extends BaseAudioHandler Future _updateQueue() async { queue.add(sequence.map((source) => source.tag as MediaItem).toList()); + _updateShuffleIndices(); } - 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 effectiveIndices = this.effectiveIndices; - 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; @@ -521,9 +528,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; From ff21b555d27569b3d62ccee78b544e95149480d3 Mon Sep 17 00:00:00 2001 From: Antoine Gersant Date: Fri, 11 Nov 2022 23:30:23 -0800 Subject: [PATCH 4/5] Add missing calls to _broadcastStateIfActive Ensure _updateShuffleIndices is called after every operation that affects said indices --- just_audio_background/lib/just_audio_background.dart | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/just_audio_background/lib/just_audio_background.dart b/just_audio_background/lib/just_audio_background.dart index b7f22fddc..b32f7f845 100644 --- a/just_audio_background/lib/just_audio_background.dart +++ b/just_audio_background/lib/just_audio_background.dart @@ -464,6 +464,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); } @@ -472,6 +474,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); } @@ -481,6 +485,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); } @@ -499,7 +505,6 @@ class _PlayerAudioHandler extends BaseAudioHandler Future _updateQueue() async { queue.add(sequence.map((source) => source.tag as MediaItem).toList()); - _updateShuffleIndices(); } void _updateShuffleIndices() { @@ -620,6 +625,7 @@ class _PlayerAudioHandler extends BaseAudioHandler @override Future setShuffleMode(AudioServiceShuffleMode shuffleMode) async { _shuffleMode = shuffleMode; + _updateShuffleIndices(); _broadcastStateIfActive(); (await _player).setShuffleMode(SetShuffleModeRequest( shuffleMode: ShuffleModeMessage.values[ From bbd53d9a70d4a22acd65f9b4fc64e075ff1500c9 Mon Sep 17 00:00:00 2001 From: Ryan Heise Date: Thu, 17 Nov 2022 17:34:42 +1100 Subject: [PATCH 5/5] Add missing load case for updating shuffle indices. --- just_audio_background/lib/just_audio_background.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/just_audio_background/lib/just_audio_background.dart b/just_audio_background/lib/just_audio_background.dart index b32f7f845..fd9073e09 100644 --- a/just_audio_background/lib/just_audio_background.dart +++ b/just_audio_background/lib/just_audio_background.dart @@ -433,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!,