diff --git a/just_audio/example/lib/main.dart b/just_audio/example/lib/main.dart index 85f7f6f..595da2f 100644 --- a/just_audio/example/lib/main.dart +++ b/just_audio/example/lib/main.dart @@ -284,7 +284,8 @@ class ControlButtons extends StatelessWidget { return IconButton( icon: Icon(Icons.replay), iconSize: 64.0, - onPressed: () => player.seek(Duration.zero, index: 0), + onPressed: () => player.seek(Duration.zero, + index: player.effectiveIndices.first), ); } }, diff --git a/just_audio/example/pubspec.lock b/just_audio/example/pubspec.lock index e250a2a..4555a6b 100644 --- a/just_audio/example/pubspec.lock +++ b/just_audio/example/pubspec.lock @@ -7,7 +7,7 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.5.0-nullsafety.1" + version: "2.5.0-nullsafety.3" audio_session: dependency: "direct main" description: @@ -21,35 +21,35 @@ packages: name: boolean_selector url: "https://pub.dartlang.org" source: hosted - version: "2.1.0-nullsafety.1" + version: "2.1.0-nullsafety.3" characters: dependency: transitive description: name: characters url: "https://pub.dartlang.org" source: hosted - version: "1.1.0-nullsafety.3" + version: "1.1.0-nullsafety.5" charcode: dependency: transitive description: name: charcode url: "https://pub.dartlang.org" source: hosted - version: "1.2.0-nullsafety.1" + version: "1.2.0-nullsafety.3" clock: dependency: transitive description: name: clock url: "https://pub.dartlang.org" source: hosted - version: "1.1.0-nullsafety.1" + version: "1.1.0-nullsafety.3" collection: dependency: transitive description: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.15.0-nullsafety.3" + version: "1.15.0-nullsafety.5" convert: dependency: transitive description: @@ -77,7 +77,7 @@ packages: name: fake_async url: "https://pub.dartlang.org" source: hosted - version: "1.2.0-nullsafety.1" + version: "1.2.0-nullsafety.3" ffi: dependency: transitive description: @@ -114,6 +114,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.16.1" + js: + dependency: transitive + description: + name: js + url: "https://pub.dartlang.org" + source: hosted + version: "0.6.3-nullsafety.3" just_audio: dependency: "direct main" description: @@ -141,21 +148,21 @@ packages: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.10-nullsafety.1" + version: "0.12.10-nullsafety.3" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.3.0-nullsafety.3" + version: "1.3.0-nullsafety.6" path: dependency: transitive description: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.8.0-nullsafety.1" + version: "1.8.0-nullsafety.3" path_provider: dependency: transitive description: @@ -230,49 +237,49 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.8.0-nullsafety.2" + version: "1.8.0-nullsafety.4" stack_trace: dependency: transitive description: name: stack_trace url: "https://pub.dartlang.org" source: hosted - version: "1.10.0-nullsafety.1" + version: "1.10.0-nullsafety.6" stream_channel: dependency: transitive description: name: stream_channel url: "https://pub.dartlang.org" source: hosted - version: "2.1.0-nullsafety.1" + version: "2.1.0-nullsafety.3" string_scanner: dependency: transitive description: name: string_scanner url: "https://pub.dartlang.org" source: hosted - version: "1.1.0-nullsafety.1" + version: "1.1.0-nullsafety.3" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.dartlang.org" source: hosted - version: "1.2.0-nullsafety.1" + version: "1.2.0-nullsafety.3" test_api: dependency: transitive description: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.19-nullsafety.2" + version: "0.2.19-nullsafety.6" typed_data: dependency: transitive description: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.3.0-nullsafety.3" + version: "1.3.0-nullsafety.5" uuid: dependency: transitive description: @@ -286,7 +293,7 @@ packages: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.1.0-nullsafety.3" + version: "2.1.0-nullsafety.5" win32: dependency: transitive description: @@ -302,5 +309,5 @@ packages: source: hosted version: "0.1.2" sdks: - dart: ">=2.10.0-110 <2.11.0" + dart: ">=2.12.0-0.0 <3.0.0" flutter: ">=1.12.13+hotfix.5 <2.0.0" diff --git a/just_audio/lib/just_audio.dart b/just_audio/lib/just_audio.dart index 639a866..262967d 100644 --- a/just_audio/lib/just_audio.dart +++ b/just_audio/lib/just_audio.dart @@ -105,7 +105,6 @@ class AudioPlayer { .map((event) => event.currentIndex) .distinct() .handleError((err, stack) {/* noop */})); - currentIndexStream.listen((index) => print('### index: $index')); _androidAudioSessionIdSubject.addStream(playbackEventStream .map((event) => event.androidAudioSessionId) .distinct() @@ -154,8 +153,6 @@ class AudioPlayer { currentIndex: message.currentIndex, androidAudioSessionId: message.androidAudioSessionId, ); - print( - "### received event with currentIndex: ${playbackEvent.currentIndex}"); _durationFuture = Future.value(playbackEvent.duration); if (playbackEvent.duration != _playbackEvent.duration) { _durationSubject.add(playbackEvent.duration); @@ -308,69 +305,48 @@ class AudioPlayer { Stream get sequenceStateStream => _sequenceStateSubject.stream; /// Whether there is another item after the current index. - bool get hasNext => _nextIndex != null; + bool get hasNext => nextIndex != null; /// Whether there is another item before the current index. - bool get hasPrevious => _previousIndex != null; + bool get hasPrevious => previousIndex != null; - int get _nextIndex => _getRelativeIndex(1); - int get _previousIndex => _getRelativeIndex(-1); + /// Returns [shuffleIndices] if [shuffleModeEnabled] is `true`, otherwise + /// returns the unshuffled indices. + List get effectiveIndices { + if (shuffleIndices == null || sequence == null) return null; + return shuffleModeEnabled + ? shuffleIndices + : List.generate(sequence.length, (i) => i); + } + + List get _effectiveIndicesInv { + if (shuffleIndices == null || sequence == null) return null; + return shuffleModeEnabled + ? _shuffleIndicesInv + : List.generate(sequence.length, (i) => i); + } + + int get nextIndex => _getRelativeIndex(1); + int get previousIndex => _getRelativeIndex(-1); int _getRelativeIndex(int offset) { - print('### _getRelativeIndex'); - print('audioSource = $_audioSource'); - print('currentIndex = $currentIndex'); - print('shuffleModeEnabled = $shuffleModeEnabled'); if (_audioSource == null || currentIndex == null || shuffleModeEnabled == null) return null; if (loopMode == LoopMode.one) return currentIndex; - int result; - print('shuffleModeEnabled: $shuffleModeEnabled'); - if (shuffleModeEnabled) { - print('shuffleIndices: $shuffleIndices'); - print('shuffleIndicesInv: $_shuffleIndicesInv'); - if (shuffleIndices == null) return null; - final shufflePos = _shuffleIndicesInv[currentIndex]; - var newShufflePos = shufflePos + offset; - print( - 'newshufflePos($newShufflePos) >= shuffleIndices.length(${shuffleIndices.length})'); - if (newShufflePos >= shuffleIndices.length) { - if (loopMode == LoopMode.all) { - newShufflePos = 0; - } else { - return null; - } - } - if (newShufflePos < 0) { - if (loopMode == LoopMode.all) { - newShufflePos = shuffleIndices.length - 1; - } else { - return null; - } - } - result = shuffleIndices[newShufflePos]; - } else { - print("sequence: $sequence"); - if (sequence == null) return null; - result = currentIndex + offset; - print("result($result >= sequence.length(${sequence.length}))"); - if (result >= sequence.length) { - if (loopMode == LoopMode.all) { - result = 0; - } else { - return null; - } - } - if (result < 0) { - if (loopMode == LoopMode.all) { - result = sequence.length - 1; - } else { - return null; - } + final effectiveIndices = this.effectiveIndices; + if (effectiveIndices == null) return null; + final effectiveIndicesInv = _effectiveIndicesInv; + final invPos = effectiveIndicesInv[currentIndex]; + var newInvPos = invPos + offset; + if (newInvPos >= effectiveIndices.length || newInvPos < 0) { + if (loopMode == LoopMode.all) { + newInvPos %= effectiveIndices.length; + } else { + return null; } } - print("returning $result"); + final result = effectiveIndices[newInvPos]; return result; } @@ -535,7 +511,6 @@ class AudioPlayer { if (_disposed) return null; try { _audioSource = source; - print("### set _audioSource = $_audioSource"); _broadcastSequence(); final duration = await _load(source, initialPosition: initialPosition, initialIndex: initialIndex); @@ -550,18 +525,12 @@ class AudioPlayer { } void _broadcastSequence() { - print("### _broadcastSequence"); _sequenceSubject.add(_audioSource?.sequence); - - print('sequence: ${_audioSource?.sequence?.length}'); - _updateShuffleIndices(); } _updateShuffleIndices() { _shuffleIndicesSubject.add(_audioSource?.shuffleIndices); - print('shuffle indices: ${_audioSource?.shuffleIndices?.length}'); - print('shuffleIndices: ${shuffleIndices?.length}'); final shuffleIndicesLength = shuffleIndices?.length ?? 0; if (_shuffleIndicesInv.length > shuffleIndicesLength) { _shuffleIndicesInv.removeRange( @@ -573,7 +542,6 @@ class AudioPlayer { for (var i = 0; i < shuffleIndicesLength; i++) { _shuffleIndicesInv[shuffleIndices[i]] = i; } - print('shuffleIndicesInv: ${_shuffleIndicesInv?.length}'); } _registerAudioSource(AudioSource source) { @@ -582,7 +550,6 @@ class AudioPlayer { Future _load(AudioSource source, {Duration initialPosition, int initialIndex}) async { - print("### _load with initialIndex=$initialIndex"); try { if (!kIsWeb && source._requiresHeaders) { if (_proxy == null) { @@ -732,13 +699,10 @@ class AudioPlayer { /// Recursively shuffles the children of the currently loaded [AudioSource]. Future shuffle() async { - print( - "shuffle. _disposed: $_disposed, currentIndex: $currentIndex, _audioSource: $_audioSource"); if (_disposed) return; if (_audioSource == null) return; _audioSource._shuffle(initialIndex: currentIndex); _updateShuffleIndices(); - print("Shuffle: $shuffleIndices"); await (await _platform).setShuffleOrder( SetShuffleOrderRequest(audioSourceMessage: _audioSource._toMessage())); } @@ -776,19 +740,17 @@ class AudioPlayer { } } - /// Seek to the next item. + /// Seek to the next item, or does nothing if there is no next item. Future seekToNext() async { - if (_disposed) return; if (hasNext) { - await seek(Duration.zero, index: _nextIndex); + await seek(Duration.zero, index: nextIndex); } } - /// Seek to the previous item. + /// Seek to the previous item, or does nothing if there is no previous item. Future seekToPrevious() async { - if (_disposed) return; if (hasPrevious) { - await seek(Duration.zero, index: _previousIndex); + await seek(Duration.zero, index: previousIndex); } } @@ -817,7 +779,6 @@ class AudioPlayer { await (await _platform).dispose(DisposeRequest()); } _audioSource = null; - print("### set _audioSource = $_audioSource (dispose)"); _audioSources.values.forEach((s) => s._dispose()); _audioSources.clear(); _proxy?.stop(); @@ -1445,8 +1406,6 @@ class ConcatenatingAudioSource extends AudioSource { child._shuffle(initialIndex: childInitialIndex); si += childLength; } - print( - "cat _shuffle (initialIndex=$initialIndex, localInitialIndex=$localInitialIndex)"); _shuffleOrder.shuffle(initialIndex: localInitialIndex); } @@ -1603,11 +1562,8 @@ class ConcatenatingAudioSource extends AudioSource { } final indices = []; for (var index in _shuffleOrder.indices) { - final childIndices = childIndicesList[index]; - //print("child indices: $childIndices"); - indices.addAll(childIndices); + indices.addAll(childIndicesList[index]); } - print("### returning: $indices"); return indices; } @@ -1728,11 +1684,8 @@ class DefaultShuffleOrder extends ShuffleOrder { @override void shuffle({int initialIndex}) { assert(initialIndex == null || indices.contains(initialIndex)); - print( - "### order shuffle, initialIndex: $initialIndex (existing: $indices)"); if (indices.length <= 1) return; indices.shuffle(_random); - print("now $indices"); if (initialIndex == null) return; final initialPos = 0; @@ -1741,7 +1694,6 @@ class DefaultShuffleOrder extends ShuffleOrder { final swapIndex = indices[initialPos]; indices[initialPos] = initialIndex; indices[swapPos] = swapIndex; - print("final $indices"); } @override diff --git a/just_audio/pubspec.lock b/just_audio/pubspec.lock index ac41e92..df82919 100644 --- a/just_audio/pubspec.lock +++ b/just_audio/pubspec.lock @@ -28,7 +28,7 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.5.0-nullsafety.1" + version: "2.5.0-nullsafety.3" audio_session: dependency: "direct main" description: @@ -42,7 +42,7 @@ packages: name: boolean_selector url: "https://pub.dartlang.org" source: hosted - version: "2.1.0-nullsafety.1" + version: "2.1.0-nullsafety.3" build: dependency: transitive description: @@ -70,14 +70,14 @@ packages: name: characters url: "https://pub.dartlang.org" source: hosted - version: "1.1.0-nullsafety.3" + version: "1.1.0-nullsafety.5" charcode: dependency: transitive description: name: charcode url: "https://pub.dartlang.org" source: hosted - version: "1.2.0-nullsafety.1" + version: "1.2.0-nullsafety.3" cli_util: dependency: transitive description: @@ -91,7 +91,7 @@ packages: name: clock url: "https://pub.dartlang.org" source: hosted - version: "1.1.0-nullsafety.1" + version: "1.1.0-nullsafety.3" code_builder: dependency: transitive description: @@ -105,7 +105,7 @@ packages: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.15.0-nullsafety.3" + version: "1.15.0-nullsafety.5" convert: dependency: transitive description: @@ -133,7 +133,7 @@ packages: name: fake_async url: "https://pub.dartlang.org" source: hosted - version: "1.2.0-nullsafety.1" + version: "1.2.0-nullsafety.3" ffi: dependency: transitive description: @@ -190,7 +190,7 @@ packages: name: js url: "https://pub.dartlang.org" source: hosted - version: "0.6.2" + version: "0.6.3-nullsafety.3" just_audio_platform_interface: dependency: "direct main" description: @@ -218,14 +218,14 @@ packages: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.10-nullsafety.1" + version: "0.12.10-nullsafety.3" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.3.0-nullsafety.3" + version: "1.3.0-nullsafety.6" mockito: dependency: "direct dev" description: @@ -260,7 +260,7 @@ packages: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.8.0-nullsafety.1" + version: "1.8.0-nullsafety.3" path_provider: dependency: "direct main" description: @@ -363,49 +363,49 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.8.0-nullsafety.2" + version: "1.8.0-nullsafety.4" stack_trace: dependency: transitive description: name: stack_trace url: "https://pub.dartlang.org" source: hosted - version: "1.10.0-nullsafety.1" + version: "1.10.0-nullsafety.6" stream_channel: dependency: transitive description: name: stream_channel url: "https://pub.dartlang.org" source: hosted - version: "2.1.0-nullsafety.1" + version: "2.1.0-nullsafety.3" string_scanner: dependency: transitive description: name: string_scanner url: "https://pub.dartlang.org" source: hosted - version: "1.1.0-nullsafety.1" + version: "1.1.0-nullsafety.3" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.dartlang.org" source: hosted - version: "1.2.0-nullsafety.1" + version: "1.2.0-nullsafety.3" test_api: dependency: transitive description: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.19-nullsafety.2" + version: "0.2.19-nullsafety.6" typed_data: dependency: transitive description: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.3.0-nullsafety.3" + version: "1.3.0-nullsafety.5" uuid: dependency: "direct main" description: @@ -419,7 +419,7 @@ packages: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.1.0-nullsafety.3" + version: "2.1.0-nullsafety.5" watcher: dependency: transitive description: @@ -449,5 +449,5 @@ packages: source: hosted version: "2.2.1" sdks: - dart: ">=2.10.0 <2.11.0" + dart: ">=2.12.0-0.0 <3.0.0" flutter: ">=1.12.13+hotfix.5 <2.0.0"