Skip to content

Commit

Permalink
Merge pull request #1 from recastrodiaz/speed
Browse files Browse the repository at this point in the history
Implemented playback speed on iOS
  • Loading branch information
JONA-Cureambit authored Apr 25, 2019
2 parents 51f04f3 + b2d6080 commit 39d39cf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
12 changes: 4 additions & 8 deletions packages/video_player/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@

## 0.10.0+6

* Android: Fix missing call to `event.put("event", "completed");` which makes it possible to detect when the video is over.

## 0.10.0+5.1
## 0.10.0+7

* Implemented playback speed feature.

## 0.10.0+5.2
## 0.10.0+6

* Fixed iOS build warnings about implicit retains.
* Android: Fix missing call to `event.put("event", "completed");` which makes it possible to detect when the video is over.

## 0.10.0+5

* Implemented playback speed feature.
* Fixed iOS build warnings about implicit retains.

## 0.10.0+4

Expand Down
16 changes: 16 additions & 0 deletions packages/video_player/ios/Classes/VideoPlayerPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,19 @@ - (void)setVolume:(double)volume {
_player.volume = (volume < 0.0) ? 0.0 : ((volume > 1.0) ? 1.0 : volume);
}

- (void)setPlayBackSpeed:(double)speed {
if (speed == 1.0 || speed == 0.0) {
_player.rate = speed;
} else if (speed < 0 || speed > 2.0) {
NSLog(@"Speed outside supported range %f", speed);
} else if ((speed > 1.0 && _player.currentItem.canPlayFastForward) ||
(speed < 1.0 && _player.currentItem.canPlaySlowForward)) {
_player.rate = speed;
} else {
NSLog(@"Unsupported speed. Cannot play fast/slow forward: %f", speed);
}
}

- (CVPixelBufferRef)copyPixelBuffer {
CMTime outputItemTime = [_videoOutput itemTimeForHostTime:CACurrentMediaTime()];
if ([_videoOutput hasNewPixelBufferForItemTime:outputItemTime]) {
Expand Down Expand Up @@ -494,6 +507,9 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
} else if ([@"pause" isEqualToString:call.method]) {
[player pause];
result(nil);
} else if ([@"setPlayBackSpeed" isEqualToString:call.method]) {
[player setPlayBackSpeed:[[argsMap objectForKey:@"speed"] doubleValue]];
result(nil);
} else {
result(FlutterMethodNotImplemented);
}
Expand Down
11 changes: 11 additions & 0 deletions packages/video_player/lib/video_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ class VideoPlayerValue {
final double speed;

bool get initialized => duration != null;

bool get hasError => errorDescription != null;

double get aspectRatio => size != null ? size.width / size.height : 1.0;

VideoPlayerValue copyWith({
Expand Down Expand Up @@ -359,6 +361,9 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
value = value.copyWith(position: newPosition);
},
);

// Ensure the video is played at the correct speed
await _applyPlayBackSpeed();
} else {
_timer?.cancel();
// TODO(amirh): remove this on when the invokeMethod update makes it to stable Flutter.
Expand Down Expand Up @@ -433,6 +438,12 @@ 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
if (!value.isPlaying) {
return;
}

// ignore: strong_mode_implicit_dynamic_method
await _channel.invokeMethod(
'setPlayBackSpeed',
Expand Down

0 comments on commit 39d39cf

Please sign in to comment.