Skip to content

Commit 39d39cf

Browse files
Merge pull request #1 from recastrodiaz/speed
Implemented playback speed on iOS
2 parents 51f04f3 + b2d6080 commit 39d39cf

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

packages/video_player/CHANGELOG.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11

2-
## 0.10.0+6
3-
4-
* Android: Fix missing call to `event.put("event", "completed");` which makes it possible to detect when the video is over.
5-
6-
## 0.10.0+5.1
2+
## 0.10.0+7
73

84
* Implemented playback speed feature.
95

10-
## 0.10.0+5.2
6+
## 0.10.0+6
117

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

1410
## 0.10.0+5
1511

16-
* Implemented playback speed feature.
12+
* Fixed iOS build warnings about implicit retains.
1713

1814
## 0.10.0+4
1915

packages/video_player/ios/Classes/VideoPlayerPlugin.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,19 @@ - (void)setVolume:(double)volume {
342342
_player.volume = (volume < 0.0) ? 0.0 : ((volume > 1.0) ? 1.0 : volume);
343343
}
344344

345+
- (void)setPlayBackSpeed:(double)speed {
346+
if (speed == 1.0 || speed == 0.0) {
347+
_player.rate = speed;
348+
} else if (speed < 0 || speed > 2.0) {
349+
NSLog(@"Speed outside supported range %f", speed);
350+
} else if ((speed > 1.0 && _player.currentItem.canPlayFastForward) ||
351+
(speed < 1.0 && _player.currentItem.canPlaySlowForward)) {
352+
_player.rate = speed;
353+
} else {
354+
NSLog(@"Unsupported speed. Cannot play fast/slow forward: %f", speed);
355+
}
356+
}
357+
345358
- (CVPixelBufferRef)copyPixelBuffer {
346359
CMTime outputItemTime = [_videoOutput itemTimeForHostTime:CACurrentMediaTime()];
347360
if ([_videoOutput hasNewPixelBufferForItemTime:outputItemTime]) {
@@ -494,6 +507,9 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
494507
} else if ([@"pause" isEqualToString:call.method]) {
495508
[player pause];
496509
result(nil);
510+
} else if ([@"setPlayBackSpeed" isEqualToString:call.method]) {
511+
[player setPlayBackSpeed:[[argsMap objectForKey:@"speed"] doubleValue]];
512+
result(nil);
497513
} else {
498514
result(FlutterMethodNotImplemented);
499515
}

packages/video_player/lib/video_player.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ class VideoPlayerValue {
9393
final double speed;
9494

9595
bool get initialized => duration != null;
96+
9697
bool get hasError => errorDescription != null;
98+
9799
double get aspectRatio => size != null ? size.width / size.height : 1.0;
98100

99101
VideoPlayerValue copyWith({
@@ -359,6 +361,9 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
359361
value = value.copyWith(position: newPosition);
360362
},
361363
);
364+
365+
// Ensure the video is played at the correct speed
366+
await _applyPlayBackSpeed();
362367
} else {
363368
_timer?.cancel();
364369
// TODO(amirh): remove this on when the invokeMethod update makes it to stable Flutter.
@@ -433,6 +438,12 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
433438
return;
434439
}
435440

441+
// On iOS setting the speed will start playing the video automatically
442+
// Do not change the video speed until after the video is played
443+
if (!value.isPlaying) {
444+
return;
445+
}
446+
436447
// ignore: strong_mode_implicit_dynamic_method
437448
await _channel.invokeMethod(
438449
'setPlayBackSpeed',

0 commit comments

Comments
 (0)