Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 014e373

Browse files
recastrodiazdnfield
authored andcommitted
[video_player] Fix aspect ratio (#690)
* * Added access to the video rotation in degrees (rotationDegrees). Possible values are: 0, 90, 180 and 270. * Updated the aspectRatio getter to use the video rotation. * Fixed the aspect ratio of videos in the video_player plugin example. It now properly displays 'rotated' videos. * Ensure videos are not upside down nor streteched when using the video_player plugin on iOS devices. The fix sets the videoComposition of the respective AVPlayerItem to use the preferredTransform of the video (the rotation matrix for the video) * - Bump video_player version to 0.9.0 - Move play/pause logic within setState closure in video_player example
1 parent 7a6dd46 commit 014e373

File tree

8 files changed

+192
-58
lines changed

8 files changed

+192
-58
lines changed

packages/image_picker/example/lib/main.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,9 @@ class AspectRatioVideoState extends State<AspectRatioVideo> {
226226
@override
227227
Widget build(BuildContext context) {
228228
if (initialized) {
229-
final Size size = controller.value.size;
230229
return Center(
231230
child: AspectRatio(
232-
aspectRatio: size.width / size.height,
231+
aspectRatio: controller.value?.aspectRatio,
233232
child: VideoPlayer(controller),
234233
),
235234
);

packages/video_player/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.9.0
2+
3+
* Fixed the aspect ratio and orientation of videos. Videos are now properly displayed when recorded
4+
in portrait mode both in iOS and Android.
5+
16
## 0.8.0
27

38
* Android: Upgrade ExoPlayer to 2.9.1

packages/video_player/README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,12 @@ class VideoApp extends StatefulWidget {
5555
5656
class _VideoAppState extends State<VideoApp> {
5757
VideoPlayerController _controller;
58-
bool _isPlaying = false;
5958
6059
@override
6160
void initState() {
6261
super.initState();
6362
_controller = VideoPlayerController.network(
64-
'http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_20mb.mp4',
65-
)
66-
..addListener(() {
67-
final bool isPlaying = _controller.value.isPlaying;
68-
if (isPlaying != _isPlaying) {
69-
setState(() {
70-
_isPlaying = isPlaying;
71-
});
72-
}
73-
})
63+
'http://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_20mb.mp4')
7464
..initialize().then((_) {
7565
// Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
7666
setState(() {});
@@ -91,15 +81,25 @@ class _VideoAppState extends State<VideoApp> {
9181
: Container(),
9282
),
9383
floatingActionButton: FloatingActionButton(
94-
onPressed: _controller.value.isPlaying
95-
? _controller.pause
96-
: _controller.play,
84+
onPressed: () {
85+
setState(() {
86+
_controller.value.isPlaying
87+
? _controller.pause()
88+
: _controller.play();
89+
});
90+
},
9791
child: Icon(
9892
_controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
9993
),
10094
),
10195
),
10296
);
10397
}
98+
99+
@override
100+
void dispose() {
101+
super.dispose();
102+
_controller.dispose();
103+
}
104104
}
105105
```

packages/video_player/android/src/main/java/io/flutter/plugins/videoplayer/VideoPlayerPlugin.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.google.android.exoplayer2.C;
1616
import com.google.android.exoplayer2.ExoPlaybackException;
1717
import com.google.android.exoplayer2.ExoPlayerFactory;
18+
import com.google.android.exoplayer2.Format;
1819
import com.google.android.exoplayer2.Player;
1920
import com.google.android.exoplayer2.Player.DefaultEventListener;
2021
import com.google.android.exoplayer2.SimpleExoPlayer;
@@ -51,6 +52,8 @@
5152

5253
public class VideoPlayerPlugin implements MethodCallHandler {
5354

55+
private static final String TAG = "VideoPlayerPlugin";
56+
5457
private static class VideoPlayer {
5558

5659
private SimpleExoPlayer exoPlayer;
@@ -220,9 +223,19 @@ private void sendInitialized() {
220223
Map<String, Object> event = new HashMap<>();
221224
event.put("event", "initialized");
222225
event.put("duration", exoPlayer.getDuration());
226+
223227
if (exoPlayer.getVideoFormat() != null) {
224-
event.put("width", exoPlayer.getVideoFormat().width);
225-
event.put("height", exoPlayer.getVideoFormat().height);
228+
Format videoFormat = exoPlayer.getVideoFormat();
229+
int width = videoFormat.width;
230+
int height = videoFormat.height;
231+
int rotationDegrees = videoFormat.rotationDegrees;
232+
// Switch the width/height if video was taken in portrait mode
233+
if (rotationDegrees == 90 || rotationDegrees == 270) {
234+
width = exoPlayer.getVideoFormat().height;
235+
height = exoPlayer.getVideoFormat().width;
236+
}
237+
event.put("width", width);
238+
event.put("height", height);
226239
}
227240
eventSink.success(event);
228241
}

packages/video_player/example/lib/main.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ void main() {
380380
Column(
381381
children: <Widget>[
382382
NetworkPlayerLifeCycle(
383-
'http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_20mb.mp4',
383+
'http://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_20mb.mp4',
384384
(BuildContext context, VideoPlayerController controller) =>
385385
AspectRatioVideo(controller),
386386
),
@@ -395,7 +395,7 @@ void main() {
395395
],
396396
),
397397
NetworkPlayerLifeCycle(
398-
'http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_20mb.mp4',
398+
'http://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_20mb.mp4',
399399
(BuildContext context, VideoPlayerController controller) =>
400400
AspectRatioVideo(controller),
401401
),

0 commit comments

Comments
 (0)