feat: video player orientation

This commit is contained in:
Adrian Castro
2024-02-05 09:32:53 +01:00
parent 55be0860b9
commit 8e03075ebc
4 changed files with 52 additions and 14 deletions

View File

@@ -5,7 +5,6 @@ const defineConfig = (): ExpoConfig => ({
slug: "mw-mobile", slug: "mw-mobile",
scheme: "dev.movieweb.app", scheme: "dev.movieweb.app",
version: "0.1.0", version: "0.1.0",
orientation: "portrait",
icon: "./assets/images/icon.png", icon: "./assets/images/icon.png",
userInterfaceStyle: "automatic", userInterfaceStyle: "automatic",
splash: { splash: {
@@ -20,6 +19,7 @@ const defineConfig = (): ExpoConfig => ({
ios: { ios: {
bundleIdentifier: "dev.movieweb.app", bundleIdentifier: "dev.movieweb.app",
supportsTablet: true, supportsTablet: true,
requireFullScreen: true,
}, },
android: { android: {
package: "dev.movieweb.app", package: "dev.movieweb.app",
@@ -41,7 +41,13 @@ const defineConfig = (): ExpoConfig => ({
tsconfigPaths: true, tsconfigPaths: true,
typedRoutes: true, typedRoutes: true,
}, },
plugins: ["expo-router"], plugins: ["expo-router", [
"expo-screen-orientation",
{
initialOrientation: "DEFAULT"
}
]
],
}); });
export default defineConfig; export default defineConfig;

View File

@@ -26,6 +26,7 @@
"expo-constants": "~15.4.5", "expo-constants": "~15.4.5",
"expo-linking": "~6.2.2", "expo-linking": "~6.2.2",
"expo-router": "~3.4.6", "expo-router": "~3.4.6",
"expo-screen-orientation": "~6.4.1",
"expo-splash-screen": "~0.26.4", "expo-splash-screen": "~0.26.4",
"expo-status-bar": "~1.11.1", "expo-status-bar": "~1.11.1",
"expo-web-browser": "^12.8.2", "expo-web-browser": "^12.8.2",

View File

@@ -1,7 +1,9 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import { StyleSheet, View, ActivityIndicator } from 'react-native'; import { StyleSheet, ActivityIndicator } from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import type { VideoRef } from 'react-native-video'; import type { VideoRef } from 'react-native-video';
import Video from 'react-native-video'; import Video from 'react-native-video';
import * as ScreenOrientation from 'expo-screen-orientation';
interface VideoPlayerState { interface VideoPlayerState {
videoUrl: string; videoUrl: string;
@@ -16,7 +18,7 @@ class VideoPlayer extends Component<object, VideoPlayerState> {
constructor(props: object) { constructor(props: object) {
super(props); super(props);
this.state = { this.state = {
videoUrl: 'your_video_url', videoUrl: '',
fullscreen: true, fullscreen: true,
isLoading: true, isLoading: true,
paused: false paused: false
@@ -25,9 +27,25 @@ class VideoPlayer extends Component<object, VideoPlayerState> {
} }
componentDidMount() { componentDidMount() {
if (this.videoPlayer.current) { const lockOrientation = async () => {
this.videoPlayer.current.presentFullscreenPlayer(); await ScreenOrientation.lockAsync(ScreenOrientation.OrientationLock.LANDSCAPE);
} };
if (this.videoPlayer.current) {
this.videoPlayer.current.presentFullscreenPlayer();
void lockOrientation();
}
}
componentWillUnmount() {
const unlockOrientation = async () => {
await ScreenOrientation.lockAsync(ScreenOrientation.OrientationLock.PORTRAIT_UP);
}
if (this.videoPlayer.current) {
this.videoPlayer.current.dismissFullscreenPlayer();
}
void unlockOrientation();
} }
onVideoLoadStart = () => { onVideoLoadStart = () => {
@@ -38,27 +56,28 @@ class VideoPlayer extends Component<object, VideoPlayerState> {
this.setState({ isLoading: false }); this.setState({ isLoading: false });
}; };
onVideoError = () => { // onVideoError = () => { // probably useful later
console.log("Video playback error"); // console.log("Video playback error");
}; // };
render() { render() {
return ( return (
<View style={styles.container}> <SafeAreaView style={styles.container}>
<Video <Video
ref={this.videoPlayer} ref={this.videoPlayer}
source={{ uri: this.state.videoUrl }} source={{ uri: this.state.videoUrl }}
style={styles.fullScreen} style={styles.fullScreen}
fullscreen={this.state.fullscreen} fullscreen={this.state.fullscreen}
paused={this.state.paused} paused={this.state.paused}
controls={true}
onLoadStart={this.onVideoLoadStart} onLoadStart={this.onVideoLoadStart}
onReadyForDisplay={this.onReadyForDisplay} onReadyForDisplay={this.onReadyForDisplay}
onError={this.onVideoError} // onError={this.onVideoError}
/> />
{this.state.isLoading && ( {this.state.isLoading && (
<ActivityIndicator size="large" color="#0000ff" /> <ActivityIndicator size="large" color="#0000ff" />
)} )}
</View> </SafeAreaView>
); );
} }
} }
@@ -76,7 +95,8 @@ const styles = StyleSheet.create({
left: 0, left: 0,
bottom: 0, bottom: 0,
right: 0, right: 0,
} },
}); });
export default VideoPlayer; export default VideoPlayer;

11
pnpm-lock.yaml generated
View File

@@ -55,6 +55,9 @@ importers:
expo-router: expo-router:
specifier: ~3.4.6 specifier: ~3.4.6
version: 3.4.6(expo-constants@15.4.5)(expo-linking@6.2.2)(expo-modules-autolinking@1.10.2)(expo-status-bar@1.11.1)(expo@50.0.5)(react-dom@18.2.0)(react-native-reanimated@3.6.2)(react-native-safe-area-context@4.8.2)(react-native-screens@3.29.0)(react-native@0.73.2)(react@18.2.0) version: 3.4.6(expo-constants@15.4.5)(expo-linking@6.2.2)(expo-modules-autolinking@1.10.2)(expo-status-bar@1.11.1)(expo@50.0.5)(react-dom@18.2.0)(react-native-reanimated@3.6.2)(react-native-safe-area-context@4.8.2)(react-native-screens@3.29.0)(react-native@0.73.2)(react@18.2.0)
expo-screen-orientation:
specifier: ~6.4.1
version: 6.4.1(expo@50.0.5)
expo-splash-screen: expo-splash-screen:
specifier: ~0.26.4 specifier: ~0.26.4
version: 0.26.4(expo-modules-autolinking@1.10.2)(expo@50.0.5) version: 0.26.4(expo-modules-autolinking@1.10.2)(expo@50.0.5)
@@ -5378,6 +5381,14 @@ packages:
- supports-color - supports-color
dev: false dev: false
/expo-screen-orientation@6.4.1(expo@50.0.5):
resolution: {integrity: sha512-VM0C9ORNL1aT6Dr2OUeryzV519n0FjtXI2m+HlijOMi1QT2bPg4tBkCd7HLgywU4dZ1Esa46ewUudmk+fOqmMQ==}
peerDependencies:
expo: '*'
dependencies:
expo: 50.0.5(@babel/core@7.23.9)(@react-native/babel-preset@0.73.20)
dev: false
/expo-splash-screen@0.26.4(expo-modules-autolinking@1.10.2)(expo@50.0.5): /expo-splash-screen@0.26.4(expo-modules-autolinking@1.10.2)(expo@50.0.5):
resolution: {integrity: sha512-2DwofTQ0FFQCsvDysm/msENsbyNsJiAJwK3qK/oXeizECAPqD7bK19J4z9kuEbr7ORPX9MLnTQYKl6kmX3keUg==} resolution: {integrity: sha512-2DwofTQ0FFQCsvDysm/msENsbyNsJiAJwK3qK/oXeizECAPqD7bK19J4z9kuEbr7ORPX9MLnTQYKl6kmX3keUg==}
peerDependencies: peerDependencies: