Skip to content

Commit

Permalink
Merge pull request flutter#4 from recastrodiaz/speed
Browse files Browse the repository at this point in the history
Throw an error if speed is unsupported on iOS + better documentation
  • Loading branch information
JONA-Cureambit authored Aug 12, 2019
2 parents 9696c9d + f48cccc commit 6882ce4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
22 changes: 17 additions & 5 deletions packages/video_player/ios/Classes/VideoPlayerPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -354,16 +354,28 @@ - (void)setVolume:(double)volume {
_player.volume = (float)((volume < 0.0) ? 0.0 : ((volume > 1.0) ? 1.0 : volume));
}

- (void)setSpeed:(double)speed {
- (void)setSpeed:(double)speed result:(FlutterResult)result {
if (speed == 1.0 || speed == 0.0) {
_player.rate = speed;
result(nil);
} else if (speed < 0 || speed > 2.0) {
NSLog(@"Speed outside supported range %f", speed);
result([FlutterError errorWithCode:@"unsupported_speed"
message:@"Speed must be >= 0.0 and <= 2.0"
details:nil]);
} else if ((speed > 1.0 && _player.currentItem.canPlayFastForward) ||
(speed < 1.0 && _player.currentItem.canPlaySlowForward)) {
_player.rate = speed;
result(nil);
} else {
NSLog(@"Unsupported speed. Cannot play fast/slow forward: %f", speed);
if (speed > 1.0) {
result([FlutterError errorWithCode:@"unsupported_fast_forward"
message:@"This video cannot be played fast forward"
details:nil]);
} else {
result([FlutterError errorWithCode:@"unsupported_slow_forward"
message:@"This video cannot be played slow forward"
details:nil]);
}
}
}

Expand Down Expand Up @@ -520,8 +532,8 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
[player pause];
result(nil);
} else if ([@"setSpeed" isEqualToString:call.method]) {
[player setSpeed:[[argsMap objectForKey:@"speed"] doubleValue]];
result(nil);
[player setSpeed:[[argsMap objectForKey:@"speed"] doubleValue] result:result];
return;
} else {
result(FlutterMethodNotImplemented);
}
Expand Down
9 changes: 7 additions & 2 deletions packages/video_player/lib/video_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,10 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
return;
}

// On iOS setting the speed will start playing the video automatically
// Do not change the video speed until after the video is played
// On iOS setting the speed on an AVPlayer starts playing
// the video straightaway. We avoid this surprising behaviour
// by not changing the speed of the player until after the video
// starts playing
if (!value.isPlaying) {
return;
}
Expand All @@ -432,6 +434,9 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
///
/// [speed] can be 0.5x, 1x, 2x
/// by default speed value is 1.0
///
/// Negative speeds are not supported
/// speeds above 2x are not supported on iOS
Future<void> setSpeed(double speed) async {
value = value.copyWith(speed: speed);
await _applySpeed();
Expand Down

0 comments on commit 6882ce4

Please sign in to comment.