Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
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 @@ -152,6 +152,8 @@ private void sendInitialized() {
Map<String, Object> event = new HashMap<>();
event.put("event", "initialized");
event.put("duration", mediaPlayer.getDuration());
event.put("width", mediaPlayer.getVideoWidth());
event.put("height", mediaPlayer.getVideoHeight());
eventSink.success(event);
}
}
Expand Down
55 changes: 40 additions & 15 deletions packages/video_player/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,7 @@ class VideoInListOfCards extends StatelessWidget {
alignment: FractionalOffset.bottomRight +
const FractionalOffset(-0.1, -0.1),
children: <Widget>[
new Center(
child: new AspectRatio(
aspectRatio: 3 / 2,
child: new VideoPlayPause(controller),
),
),
new AspectRatioVideo(controller),
new Image.asset('assets/flutter-mark-square-64.png'),
]),
],
Expand All @@ -276,19 +271,49 @@ class VideoInListOfCards extends StatelessWidget {
}
}

class FullScreenVideo extends StatelessWidget {
class AspectRatioVideo extends StatefulWidget {
final VideoPlayerController controller;

FullScreenVideo(this.controller);
AspectRatioVideo(this.controller);

@override
AspectRatioVideoState createState() => new AspectRatioVideoState();
}

class AspectRatioVideoState extends State<AspectRatioVideo> {
VideoPlayerController get controller => widget.controller;
bool initialized = false;

VoidCallback listener;

@override
void initState() {
super.initState();
listener = () {
if (!mounted) {
return;
}
if (initialized != controller.value.initialized) {
initialized = controller.value.initialized;
setState(() {});
}
};
controller.addListener(listener);
}

@override
Widget build(BuildContext context) {
return new Center(
child: new AspectRatio(
aspectRatio: 3 / 2,
child: new VideoPlayPause(controller),
),
);
if (initialized) {
final Size size = controller.value.size;
return new Center(
child: new AspectRatio(
aspectRatio: size.width / size.height,
child: new VideoPlayPause(controller),
),
);
} else {
return new Container();
}
}
}

Expand All @@ -313,7 +338,7 @@ void main() {
new PlayerLifeCycle(
'http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_20mb.mp4',
(BuildContext context, VideoPlayerController controller) =>
new FullScreenVideo(controller),
new AspectRatioVideo(controller),
),
new PlayerLifeCycle(
'http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_20mb.mp4',
Expand Down
8 changes: 7 additions & 1 deletion packages/video_player/ios/Classes/VideoPlayerPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,13 @@ - (void)updatePlayingState {

- (void)sendInitialized {
if (_eventSink && _isInitialized) {
_eventSink(@{ @"event" : @"initialized", @"duration" : @([self duration]) });
CGSize size = [self.player currentItem].presentationSize;
_eventSink(@{
@"event" : @"initialized",
@"duration" : @([self duration]),
@"width" : @(size.width),
@"height" : @(size.height),
});
}
}

Expand Down
14 changes: 14 additions & 0 deletions packages/video_player/lib/video_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ class VideoPlayerValue {
final bool isLooping;
final double volume;
final String errorDescription;
final Size size;

VideoPlayerValue({
@required this.duration,
this.size,
this.position: const Duration(),
this.buffered: const <DurationRange>[],
this.isPlaying: false,
Expand All @@ -57,9 +59,11 @@ class VideoPlayerValue {

bool get initialized => duration != null;
bool get isErroneous => errorDescription != null;
double get aspectRatio => size.width / size.height;

VideoPlayerValue copyWith({
Duration duration,
Size size,
Duration position,
List<DurationRange> buffered,
bool isPlaying,
Expand All @@ -69,6 +73,7 @@ class VideoPlayerValue {
}) {
return new VideoPlayerValue(
duration: duration ?? this.duration,
size: size ?? this.size,
position: position ?? this.position,
buffered: buffered ?? this.buffered,
isPlaying: isPlaying ?? this.isPlaying,
Expand All @@ -82,6 +87,7 @@ class VideoPlayerValue {
String toString() {
return '$runtimeType('
'duration: $duration, '
'size: $size, '
'position: $position, '
'buffered: [${buffered.join(', ')}], '
'isplaying: $isPlaying, '
Expand Down Expand Up @@ -134,6 +140,7 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
if (event["event"] == "initialized") {
value = value.copyWith(
duration: new Duration(milliseconds: event["duration"]),
size: new Size(event["width"].toDouble(), event["height"].toDouble()),
);
_applyLooping();
_applyVolume();
Expand Down Expand Up @@ -168,6 +175,7 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
if (_creatingCompleter != null) {
await _creatingCompleter.future;
if (!isDisposed) {
isDisposed = true;
timer?.cancel();
await _eventSubscription?.cancel();
await _channel.invokeMethod(
Expand Down Expand Up @@ -218,7 +226,13 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
timer = new Timer.periodic(
const Duration(milliseconds: 500),
(Timer timer) async {
if (isDisposed) {
return;
}
final Duration newPosition = await position;
if (isDisposed) {
return;
}
value = value.copyWith(position: newPosition);
},
);
Expand Down