Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.PlaybackParameters;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.Player.DefaultEventListener;
import com.google.android.exoplayer2.SimpleExoPlayer;
Expand Down Expand Up @@ -207,6 +208,14 @@ void setVolume(double value) {
exoPlayer.setVolume(bracketedValue);
}

void setSpeed(double value) {
if (value <= 0.0) return;
PlaybackParameters parameters = exoPlayer.getPlaybackParameters();
PlaybackParameters newParameters =
new PlaybackParameters((float) value, parameters.pitch, parameters.skipSilence);
exoPlayer.setPlaybackParameters(newParameters);
}

void seekTo(int location) {
exoPlayer.seekTo(location);
}
Expand Down Expand Up @@ -356,6 +365,10 @@ private void onMethodCall(MethodCall call, Result result, long textureId, VideoP
player.setVolume((Double) call.argument("volume"));
result.success(null);
break;
case "setSpeed":
player.setSpeed((Double) call.argument("speed"));
result.success(null);
break;
case "play":
player.play();
result.success(null);
Expand Down
3 changes: 3 additions & 0 deletions packages/video_player/ios/Classes/VideoPlayerPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,9 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
} else if ([@"setVolume" isEqualToString:call.method]) {
[player setVolume:[[argsMap objectForKey:@"volume"] doubleValue]];
result(nil);
} else if ([@"setSpeed" isEqualToString:call.method]) {
// TODO: Implement setSpeed
result(nil);
} else if ([@"play" isEqualToString:call.method]) {
[player play];
result(nil);
Expand Down
29 changes: 28 additions & 1 deletion packages/video_player/lib/video_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class VideoPlayerValue {
this.isLooping = false,
this.isBuffering = false,
this.volume = 1.0,
this.speed = 1.0,
this.errorDescription,
});

Expand Down Expand Up @@ -75,6 +76,9 @@ class VideoPlayerValue {
/// The current volume of the playback.
final double volume;

/// The current speed of the playback.
final double speed;

/// A description of the error if present.
///
/// If [hasError] is false this is [null].
Expand All @@ -98,6 +102,7 @@ class VideoPlayerValue {
bool isLooping,
bool isBuffering,
double volume,
double speed,
String errorDescription,
}) {
return VideoPlayerValue(
Expand All @@ -109,6 +114,7 @@ class VideoPlayerValue {
isLooping: isLooping ?? this.isLooping,
isBuffering: isBuffering ?? this.isBuffering,
volume: volume ?? this.volume,
speed: speed ?? this.speed,
errorDescription: errorDescription ?? this.errorDescription,
);
}
Expand All @@ -122,8 +128,9 @@ class VideoPlayerValue {
'buffered: [${buffered.join(', ')}], '
'isPlaying: $isPlaying, '
'isLooping: $isLooping, '
'isBuffering: $isBuffering'
'isBuffering: $isBuffering, '
'volume: $volume, '
'speed: $speed, '
'errorDescription: $errorDescription)';
}
}
Expand Down Expand Up @@ -233,6 +240,7 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
initializingCompleter.complete(null);
_applyLooping();
_applyVolume();
_applySpeed();
_applyPlayPause();
break;
case 'completed':
Expand Down Expand Up @@ -355,6 +363,16 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
);
}

Future<void> _applySpeed() async {
if (!value.initialized || _isDisposed) {
return;
}
await _channel.invokeMethod(
'setSpeed',
<String, dynamic>{'textureId': _textureId, 'speed': value.speed},
);
}

/// The position in the current video.
Future<Duration> get position async {
if (_isDisposed) {
Expand Down Expand Up @@ -392,6 +410,15 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
value = value.copyWith(volume: volume.clamp(0.0, 1.0));
await _applyVolume();
}

/// Sets the playback speed of [this].
///
/// [speed] must be greater than zero.
Future<void> setSpeed(double speed) async {
if (speed <= 0.0) return;
value = value.copyWith(speed: speed);
await _applySpeed();
}
}

class _VideoAppLifeCycleObserver extends Object with WidgetsBindingObserver {
Expand Down
2 changes: 2 additions & 0 deletions packages/video_player/test/video_player_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class FakeController extends ValueNotifier<VideoPlayerValue>
@override
Future<void> setVolume(double volume) async {}
@override
Future<void> setSpeed(double speed) async {}
@override
Future<void> initialize() async {}
@override
Future<void> pause() async {}
Expand Down