Skip to content

Commit

Permalink
Replace async-for with stream subscription.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanheise committed Nov 17, 2020
1 parent 1ca402a commit aad7674
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions just_audio/test/just_audio_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -211,33 +211,42 @@ void runTests() {
final period = Duration(seconds: 3);
final position1 = period;
final position2 = position1 + period;
double speed1 = 0.75;
final speed1 = 0.75;
final speed2 = 1.5;
final stepDuration = period ~/ 5;
var target = stepDuration;
player.setSpeed(speed1);
player.play();
final stopwatch = Stopwatch();
stopwatch.start();
await for (var position in player.positionStream) {

var completer = Completer();
StreamSubscription subscription;
subscription = player.positionStream.listen((position) {
if (position >= position1) {
break;
subscription.cancel();
completer.complete();
} else if (position >= target) {
expectDuration(position, stopwatch.elapsed * speed1);
target += stepDuration;
}
}
});
await completer.future;
player.setSpeed(speed2);
stopwatch.reset();

target = position1 + target;
await for (var position in player.positionStream) {
completer = Completer();
subscription = player.positionStream.listen((position) {
if (position >= position2) {
break;
subscription.cancel();
completer.complete();
} else if (position >= target) {
expectDuration(position, position1 + stopwatch.elapsed * speed2);
target += stepDuration;
}
}
});
await completer.future;
player.dispose();
});

Expand Down

0 comments on commit aad7674

Please sign in to comment.