Remove debug messages. Update example to use shuffle order.

This commit is contained in:
Ryan Heise 2020-12-11 21:19:49 +11:00
parent d0908a5f5e
commit 7a125149d9
4 changed files with 84 additions and 124 deletions

View File

@ -284,7 +284,8 @@ class ControlButtons extends StatelessWidget {
return IconButton( return IconButton(
icon: Icon(Icons.replay), icon: Icon(Icons.replay),
iconSize: 64.0, iconSize: 64.0,
onPressed: () => player.seek(Duration.zero, index: 0), onPressed: () => player.seek(Duration.zero,
index: player.effectiveIndices.first),
); );
} }
}, },

View File

@ -7,7 +7,7 @@ packages:
name: async name: async
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.5.0-nullsafety.1" version: "2.5.0-nullsafety.3"
audio_session: audio_session:
dependency: "direct main" dependency: "direct main"
description: description:
@ -21,35 +21,35 @@ packages:
name: boolean_selector name: boolean_selector
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.1.0-nullsafety.1" version: "2.1.0-nullsafety.3"
characters: characters:
dependency: transitive dependency: transitive
description: description:
name: characters name: characters
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.1.0-nullsafety.3" version: "1.1.0-nullsafety.5"
charcode: charcode:
dependency: transitive dependency: transitive
description: description:
name: charcode name: charcode
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.2.0-nullsafety.1" version: "1.2.0-nullsafety.3"
clock: clock:
dependency: transitive dependency: transitive
description: description:
name: clock name: clock
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.1.0-nullsafety.1" version: "1.1.0-nullsafety.3"
collection: collection:
dependency: transitive dependency: transitive
description: description:
name: collection name: collection
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.15.0-nullsafety.3" version: "1.15.0-nullsafety.5"
convert: convert:
dependency: transitive dependency: transitive
description: description:
@ -77,7 +77,7 @@ packages:
name: fake_async name: fake_async
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.2.0-nullsafety.1" version: "1.2.0-nullsafety.3"
ffi: ffi:
dependency: transitive dependency: transitive
description: description:
@ -114,6 +114,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.16.1" 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: just_audio:
dependency: "direct main" dependency: "direct main"
description: description:
@ -141,21 +148,21 @@ packages:
name: matcher name: matcher
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.12.10-nullsafety.1" version: "0.12.10-nullsafety.3"
meta: meta:
dependency: transitive dependency: transitive
description: description:
name: meta name: meta
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.3.0-nullsafety.3" version: "1.3.0-nullsafety.6"
path: path:
dependency: transitive dependency: transitive
description: description:
name: path name: path
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.8.0-nullsafety.1" version: "1.8.0-nullsafety.3"
path_provider: path_provider:
dependency: transitive dependency: transitive
description: description:
@ -230,49 +237,49 @@ packages:
name: source_span name: source_span
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.8.0-nullsafety.2" version: "1.8.0-nullsafety.4"
stack_trace: stack_trace:
dependency: transitive dependency: transitive
description: description:
name: stack_trace name: stack_trace
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.10.0-nullsafety.1" version: "1.10.0-nullsafety.6"
stream_channel: stream_channel:
dependency: transitive dependency: transitive
description: description:
name: stream_channel name: stream_channel
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.1.0-nullsafety.1" version: "2.1.0-nullsafety.3"
string_scanner: string_scanner:
dependency: transitive dependency: transitive
description: description:
name: string_scanner name: string_scanner
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.1.0-nullsafety.1" version: "1.1.0-nullsafety.3"
term_glyph: term_glyph:
dependency: transitive dependency: transitive
description: description:
name: term_glyph name: term_glyph
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.2.0-nullsafety.1" version: "1.2.0-nullsafety.3"
test_api: test_api:
dependency: transitive dependency: transitive
description: description:
name: test_api name: test_api
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.2.19-nullsafety.2" version: "0.2.19-nullsafety.6"
typed_data: typed_data:
dependency: transitive dependency: transitive
description: description:
name: typed_data name: typed_data
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.3.0-nullsafety.3" version: "1.3.0-nullsafety.5"
uuid: uuid:
dependency: transitive dependency: transitive
description: description:
@ -286,7 +293,7 @@ packages:
name: vector_math name: vector_math
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.1.0-nullsafety.3" version: "2.1.0-nullsafety.5"
win32: win32:
dependency: transitive dependency: transitive
description: description:
@ -302,5 +309,5 @@ packages:
source: hosted source: hosted
version: "0.1.2" version: "0.1.2"
sdks: 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" flutter: ">=1.12.13+hotfix.5 <2.0.0"

View File

@ -105,7 +105,6 @@ class AudioPlayer {
.map((event) => event.currentIndex) .map((event) => event.currentIndex)
.distinct() .distinct()
.handleError((err, stack) {/* noop */})); .handleError((err, stack) {/* noop */}));
currentIndexStream.listen((index) => print('### index: $index'));
_androidAudioSessionIdSubject.addStream(playbackEventStream _androidAudioSessionIdSubject.addStream(playbackEventStream
.map((event) => event.androidAudioSessionId) .map((event) => event.androidAudioSessionId)
.distinct() .distinct()
@ -154,8 +153,6 @@ class AudioPlayer {
currentIndex: message.currentIndex, currentIndex: message.currentIndex,
androidAudioSessionId: message.androidAudioSessionId, androidAudioSessionId: message.androidAudioSessionId,
); );
print(
"### received event with currentIndex: ${playbackEvent.currentIndex}");
_durationFuture = Future.value(playbackEvent.duration); _durationFuture = Future.value(playbackEvent.duration);
if (playbackEvent.duration != _playbackEvent.duration) { if (playbackEvent.duration != _playbackEvent.duration) {
_durationSubject.add(playbackEvent.duration); _durationSubject.add(playbackEvent.duration);
@ -308,69 +305,48 @@ class AudioPlayer {
Stream<SequenceState> get sequenceStateStream => _sequenceStateSubject.stream; Stream<SequenceState> get sequenceStateStream => _sequenceStateSubject.stream;
/// Whether there is another item after the current index. /// 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. /// Whether there is another item before the current index.
bool get hasPrevious => _previousIndex != null; bool get hasPrevious => previousIndex != null;
int get _nextIndex => _getRelativeIndex(1); /// Returns [shuffleIndices] if [shuffleModeEnabled] is `true`, otherwise
int get _previousIndex => _getRelativeIndex(-1); /// returns the unshuffled indices.
List<int> get effectiveIndices {
if (shuffleIndices == null || sequence == null) return null;
return shuffleModeEnabled
? shuffleIndices
: List.generate(sequence.length, (i) => i);
}
List<int> 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) { int _getRelativeIndex(int offset) {
print('### _getRelativeIndex');
print('audioSource = $_audioSource');
print('currentIndex = $currentIndex');
print('shuffleModeEnabled = $shuffleModeEnabled');
if (_audioSource == null || if (_audioSource == null ||
currentIndex == null || currentIndex == null ||
shuffleModeEnabled == null) return null; shuffleModeEnabled == null) return null;
if (loopMode == LoopMode.one) return currentIndex; if (loopMode == LoopMode.one) return currentIndex;
int result; final effectiveIndices = this.effectiveIndices;
print('shuffleModeEnabled: $shuffleModeEnabled'); if (effectiveIndices == null) return null;
if (shuffleModeEnabled) { final effectiveIndicesInv = _effectiveIndicesInv;
print('shuffleIndices: $shuffleIndices'); final invPos = effectiveIndicesInv[currentIndex];
print('shuffleIndicesInv: $_shuffleIndicesInv'); var newInvPos = invPos + offset;
if (shuffleIndices == null) return null; if (newInvPos >= effectiveIndices.length || newInvPos < 0) {
final shufflePos = _shuffleIndicesInv[currentIndex]; if (loopMode == LoopMode.all) {
var newShufflePos = shufflePos + offset; newInvPos %= effectiveIndices.length;
print( } else {
'newshufflePos($newShufflePos) >= shuffleIndices.length(${shuffleIndices.length})'); return null;
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;
}
} }
} }
print("returning $result"); final result = effectiveIndices[newInvPos];
return result; return result;
} }
@ -535,7 +511,6 @@ class AudioPlayer {
if (_disposed) return null; if (_disposed) return null;
try { try {
_audioSource = source; _audioSource = source;
print("### set _audioSource = $_audioSource");
_broadcastSequence(); _broadcastSequence();
final duration = await _load(source, final duration = await _load(source,
initialPosition: initialPosition, initialIndex: initialIndex); initialPosition: initialPosition, initialIndex: initialIndex);
@ -550,18 +525,12 @@ class AudioPlayer {
} }
void _broadcastSequence() { void _broadcastSequence() {
print("### _broadcastSequence");
_sequenceSubject.add(_audioSource?.sequence); _sequenceSubject.add(_audioSource?.sequence);
print('sequence: ${_audioSource?.sequence?.length}');
_updateShuffleIndices(); _updateShuffleIndices();
} }
_updateShuffleIndices() { _updateShuffleIndices() {
_shuffleIndicesSubject.add(_audioSource?.shuffleIndices); _shuffleIndicesSubject.add(_audioSource?.shuffleIndices);
print('shuffle indices: ${_audioSource?.shuffleIndices?.length}');
print('shuffleIndices: ${shuffleIndices?.length}');
final shuffleIndicesLength = shuffleIndices?.length ?? 0; final shuffleIndicesLength = shuffleIndices?.length ?? 0;
if (_shuffleIndicesInv.length > shuffleIndicesLength) { if (_shuffleIndicesInv.length > shuffleIndicesLength) {
_shuffleIndicesInv.removeRange( _shuffleIndicesInv.removeRange(
@ -573,7 +542,6 @@ class AudioPlayer {
for (var i = 0; i < shuffleIndicesLength; i++) { for (var i = 0; i < shuffleIndicesLength; i++) {
_shuffleIndicesInv[shuffleIndices[i]] = i; _shuffleIndicesInv[shuffleIndices[i]] = i;
} }
print('shuffleIndicesInv: ${_shuffleIndicesInv?.length}');
} }
_registerAudioSource(AudioSource source) { _registerAudioSource(AudioSource source) {
@ -582,7 +550,6 @@ class AudioPlayer {
Future<Duration> _load(AudioSource source, Future<Duration> _load(AudioSource source,
{Duration initialPosition, int initialIndex}) async { {Duration initialPosition, int initialIndex}) async {
print("### _load with initialIndex=$initialIndex");
try { try {
if (!kIsWeb && source._requiresHeaders) { if (!kIsWeb && source._requiresHeaders) {
if (_proxy == null) { if (_proxy == null) {
@ -732,13 +699,10 @@ class AudioPlayer {
/// Recursively shuffles the children of the currently loaded [AudioSource]. /// Recursively shuffles the children of the currently loaded [AudioSource].
Future<void> shuffle() async { Future<void> shuffle() async {
print(
"shuffle. _disposed: $_disposed, currentIndex: $currentIndex, _audioSource: $_audioSource");
if (_disposed) return; if (_disposed) return;
if (_audioSource == null) return; if (_audioSource == null) return;
_audioSource._shuffle(initialIndex: currentIndex); _audioSource._shuffle(initialIndex: currentIndex);
_updateShuffleIndices(); _updateShuffleIndices();
print("Shuffle: $shuffleIndices");
await (await _platform).setShuffleOrder( await (await _platform).setShuffleOrder(
SetShuffleOrderRequest(audioSourceMessage: _audioSource._toMessage())); 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<void> seekToNext() async { Future<void> seekToNext() async {
if (_disposed) return;
if (hasNext) { 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<void> seekToPrevious() async { Future<void> seekToPrevious() async {
if (_disposed) return;
if (hasPrevious) { 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()); await (await _platform).dispose(DisposeRequest());
} }
_audioSource = null; _audioSource = null;
print("### set _audioSource = $_audioSource (dispose)");
_audioSources.values.forEach((s) => s._dispose()); _audioSources.values.forEach((s) => s._dispose());
_audioSources.clear(); _audioSources.clear();
_proxy?.stop(); _proxy?.stop();
@ -1445,8 +1406,6 @@ class ConcatenatingAudioSource extends AudioSource {
child._shuffle(initialIndex: childInitialIndex); child._shuffle(initialIndex: childInitialIndex);
si += childLength; si += childLength;
} }
print(
"cat _shuffle (initialIndex=$initialIndex, localInitialIndex=$localInitialIndex)");
_shuffleOrder.shuffle(initialIndex: localInitialIndex); _shuffleOrder.shuffle(initialIndex: localInitialIndex);
} }
@ -1603,11 +1562,8 @@ class ConcatenatingAudioSource extends AudioSource {
} }
final indices = <int>[]; final indices = <int>[];
for (var index in _shuffleOrder.indices) { for (var index in _shuffleOrder.indices) {
final childIndices = childIndicesList[index]; indices.addAll(childIndicesList[index]);
//print("child indices: $childIndices");
indices.addAll(childIndices);
} }
print("### returning: $indices");
return indices; return indices;
} }
@ -1728,11 +1684,8 @@ class DefaultShuffleOrder extends ShuffleOrder {
@override @override
void shuffle({int initialIndex}) { void shuffle({int initialIndex}) {
assert(initialIndex == null || indices.contains(initialIndex)); assert(initialIndex == null || indices.contains(initialIndex));
print(
"### order shuffle, initialIndex: $initialIndex (existing: $indices)");
if (indices.length <= 1) return; if (indices.length <= 1) return;
indices.shuffle(_random); indices.shuffle(_random);
print("now $indices");
if (initialIndex == null) return; if (initialIndex == null) return;
final initialPos = 0; final initialPos = 0;
@ -1741,7 +1694,6 @@ class DefaultShuffleOrder extends ShuffleOrder {
final swapIndex = indices[initialPos]; final swapIndex = indices[initialPos];
indices[initialPos] = initialIndex; indices[initialPos] = initialIndex;
indices[swapPos] = swapIndex; indices[swapPos] = swapIndex;
print("final $indices");
} }
@override @override

View File

@ -28,7 +28,7 @@ packages:
name: async name: async
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.5.0-nullsafety.1" version: "2.5.0-nullsafety.3"
audio_session: audio_session:
dependency: "direct main" dependency: "direct main"
description: description:
@ -42,7 +42,7 @@ packages:
name: boolean_selector name: boolean_selector
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.1.0-nullsafety.1" version: "2.1.0-nullsafety.3"
build: build:
dependency: transitive dependency: transitive
description: description:
@ -70,14 +70,14 @@ packages:
name: characters name: characters
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.1.0-nullsafety.3" version: "1.1.0-nullsafety.5"
charcode: charcode:
dependency: transitive dependency: transitive
description: description:
name: charcode name: charcode
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.2.0-nullsafety.1" version: "1.2.0-nullsafety.3"
cli_util: cli_util:
dependency: transitive dependency: transitive
description: description:
@ -91,7 +91,7 @@ packages:
name: clock name: clock
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.1.0-nullsafety.1" version: "1.1.0-nullsafety.3"
code_builder: code_builder:
dependency: transitive dependency: transitive
description: description:
@ -105,7 +105,7 @@ packages:
name: collection name: collection
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.15.0-nullsafety.3" version: "1.15.0-nullsafety.5"
convert: convert:
dependency: transitive dependency: transitive
description: description:
@ -133,7 +133,7 @@ packages:
name: fake_async name: fake_async
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.2.0-nullsafety.1" version: "1.2.0-nullsafety.3"
ffi: ffi:
dependency: transitive dependency: transitive
description: description:
@ -190,7 +190,7 @@ packages:
name: js name: js
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.6.2" version: "0.6.3-nullsafety.3"
just_audio_platform_interface: just_audio_platform_interface:
dependency: "direct main" dependency: "direct main"
description: description:
@ -218,14 +218,14 @@ packages:
name: matcher name: matcher
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.12.10-nullsafety.1" version: "0.12.10-nullsafety.3"
meta: meta:
dependency: transitive dependency: transitive
description: description:
name: meta name: meta
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.3.0-nullsafety.3" version: "1.3.0-nullsafety.6"
mockito: mockito:
dependency: "direct dev" dependency: "direct dev"
description: description:
@ -260,7 +260,7 @@ packages:
name: path name: path
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.8.0-nullsafety.1" version: "1.8.0-nullsafety.3"
path_provider: path_provider:
dependency: "direct main" dependency: "direct main"
description: description:
@ -363,49 +363,49 @@ packages:
name: source_span name: source_span
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.8.0-nullsafety.2" version: "1.8.0-nullsafety.4"
stack_trace: stack_trace:
dependency: transitive dependency: transitive
description: description:
name: stack_trace name: stack_trace
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.10.0-nullsafety.1" version: "1.10.0-nullsafety.6"
stream_channel: stream_channel:
dependency: transitive dependency: transitive
description: description:
name: stream_channel name: stream_channel
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.1.0-nullsafety.1" version: "2.1.0-nullsafety.3"
string_scanner: string_scanner:
dependency: transitive dependency: transitive
description: description:
name: string_scanner name: string_scanner
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.1.0-nullsafety.1" version: "1.1.0-nullsafety.3"
term_glyph: term_glyph:
dependency: transitive dependency: transitive
description: description:
name: term_glyph name: term_glyph
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.2.0-nullsafety.1" version: "1.2.0-nullsafety.3"
test_api: test_api:
dependency: transitive dependency: transitive
description: description:
name: test_api name: test_api
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.2.19-nullsafety.2" version: "0.2.19-nullsafety.6"
typed_data: typed_data:
dependency: transitive dependency: transitive
description: description:
name: typed_data name: typed_data
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.3.0-nullsafety.3" version: "1.3.0-nullsafety.5"
uuid: uuid:
dependency: "direct main" dependency: "direct main"
description: description:
@ -419,7 +419,7 @@ packages:
name: vector_math name: vector_math
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.1.0-nullsafety.3" version: "2.1.0-nullsafety.5"
watcher: watcher:
dependency: transitive dependency: transitive
description: description:
@ -449,5 +449,5 @@ packages:
source: hosted source: hosted
version: "2.2.1" version: "2.2.1"
sdks: 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" flutter: ">=1.12.13+hotfix.5 <2.0.0"