Skip to content

Commit

Permalink
Rotate video according to rotation correction in Android example app
Browse files Browse the repository at this point in the history
  • Loading branch information
FirentisTFW committed Dec 17, 2024
1 parent f3590a8 commit 53b35dc
Showing 1 changed file with 38 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import 'dart:async';
import 'dart:io';
import 'dart:math';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -39,6 +40,7 @@ class VideoPlayerValue {
this.isBuffering = false,
this.playbackSpeed = 1.0,
this.errorDescription,
this.rotationCorrection = 0,
});

/// Returns an instance for a video that hasn't been loaded.
Expand Down Expand Up @@ -83,6 +85,9 @@ class VideoPlayerValue {
/// Indicates whether or not the video has been loaded and is ready to play.
final bool isInitialized;

/// Degrees to rotate the video (clockwise) so it is displayed correctly.
final int rotationCorrection;

/// Indicates whether or not the video is in an error state. If this is true
/// [errorDescription] should have information about the problem.
bool get hasError => errorDescription != null;
Expand Down Expand Up @@ -116,6 +121,7 @@ class VideoPlayerValue {
bool? isBuffering,
double? playbackSpeed,
String? errorDescription,
int? rotationCorrection,
}) {
return VideoPlayerValue(
duration: duration ?? this.duration,
Expand All @@ -127,6 +133,7 @@ class VideoPlayerValue {
isBuffering: isBuffering ?? this.isBuffering,
playbackSpeed: playbackSpeed ?? this.playbackSpeed,
errorDescription: errorDescription ?? this.errorDescription,
rotationCorrection: rotationCorrection ?? this.rotationCorrection,
);
}

Expand All @@ -143,7 +150,8 @@ class VideoPlayerValue {
playbackSpeed == other.playbackSpeed &&
errorDescription == other.errorDescription &&
size == other.size &&
isInitialized == other.isInitialized;
isInitialized == other.isInitialized &&
rotationCorrection == other.rotationCorrection;

@override
int get hashCode => Object.hash(
Expand All @@ -156,6 +164,7 @@ class VideoPlayerValue {
errorDescription,
size,
isInitialized,
rotationCorrection,
);
}

Expand Down Expand Up @@ -261,10 +270,9 @@ class MiniController extends ValueNotifier<VideoPlayerValue> {
void eventListener(VideoEvent event) {
switch (event.eventType) {
case VideoEventType.initialized:
print('LALA initialized');
value = value.copyWith(
duration: event.duration,
// FIXME This is important!
rotationCorrection: event.rotationCorrection,
size: event.size,
isInitialized: event.duration != null,
);
Expand Down Expand Up @@ -438,15 +446,38 @@ class _VideoPlayerState extends State<VideoPlayer> {
Widget build(BuildContext context) {
return _textureId == MiniController.kUninitializedTextureId
? Container()
: _platform.buildViewWithOptions(
VideoViewOptions(
playerId: _textureId,
viewType: widget.controller.viewType,
: _VideoPlayerWithRotation(
rotation: widget.controller.value.rotationCorrection,
child: _platform.buildViewWithOptions(
VideoViewOptions(
playerId: _textureId,
viewType: widget.controller.viewType,
),
),
);
}
}

class _VideoPlayerWithRotation extends StatelessWidget {
const _VideoPlayerWithRotation({
required this.rotation,
required this.child,
});

final int rotation;
final Widget child;

@override
Widget build(BuildContext context) {
return rotation == 0
? child
: Transform.rotate(
angle: rotation * pi / 180,
child: child,
);
}
}

class _VideoScrubber extends StatefulWidget {
const _VideoScrubber({
required this.child,
Expand Down

0 comments on commit 53b35dc

Please sign in to comment.