使用 React Native...
我制作了这个很好用的 useEffect。唯一的问题是我使用的是 React Native Youtube Iframe,它还没有在 iOS 上监听全屏请求,所以我不得不添加自己的全屏按钮。如果有人对如何帮助图书馆所有者有任何想法,将受到欢迎。 ;-)
https://github.com/LonelyCpp/react-native-youtube-iframe/issues/45
这个 useEffect 通过监听 playerFullScreen 变量并更改保存播放器样式的帧以及屏幕方向来工作。
这样播放器实际上不会全屏显示。占据屏幕所有位置的是持有它的视图。
// rely on useDimension and screen dimensions since useWindowsDimensions seems to have some issues with iFrames on React 0.63
// https://github.com/facebook/react-native/issues/29451
import { useDimensions } from '@react-native-community/hooks'
import Orientation from 'react-native-orientation-locker'
const windowWidth = useDimensions().screen.width
const windowHeight = useDimensions().screen.height
/** Change the layout when the player goes fullscreen */
useEffect(() => {
// constants
const boxedViewStyle: ViewStyle = {
position: 'absolute',
right: 0,
left: 0,
zIndex: 1,
}
const fullscreenViewStyle: ViewStyle = {
position: 'absolute',
top: 0,
right: 0,
left: 0,
bottom: 0,
zIndex: 2,
}
// Do the magic trick to change everything
if (mounted) {
if (playerFullScreen) {
Orientation.lockToLandscape() // View horizontal
setPlayerViewStyle(fullscreenViewStyle)
setPlayerWidth(windowWidth)
setPlayerHeight(windowHeight)
} else {
Orientation.lockToPortrait() // View
setPlayerViewStyle(boxedViewStyle)
setPlayerWidth(windowWidth)
setPlayerHeight(PixelRatio.roundToNearestPixel(windowWidth / (16 / 9)))
}
}
return (): void => {
Orientation.lockToPortrait()
}
}, [playerFullScreen, windowHeight, windowWidth])
这是 JSX 的样子:
return (
<View style={[styles.playerView, playerViewStyle]}>
{playerFullScreen && <StatusBar hidden={true} />}
<YoutubePlayer
videoId={videoId}
play={mediaPlayerIsUp}
onChangeState={onStateChange}
height={playerHeight}
initialPlayerParams={{
preventFullScreen: true,
}}
onError={(error): void => {
console.log('ERROR > MediaPlayerBox > YoutubePlayer: ', error)
}}
forceAndroidAutoplay={Platform.OS === 'android'}
/>
<View style={styles.fullScreen}>
<TouchableOpacity activeOpacity={0.8} onPress={toggleFullScreen}>
<Icon path={paths.mdiFullscreen} color={colors.darkGrey} size={40} />
</TouchableOpacity>
</View>
<View style={styles.close}>
<TouchableOpacity activeOpacity={0.8} onPress={onClose}>
<Icon path={paths.mdiClose} color={colors.darkGrey} size={40} />
</TouchableOpacity>
</View>
</View>
)
注意 preventFullScreen 隐藏了播放器的全屏按钮以使用它所替换的按钮。一旦解决了 React Native Youtube Iframe 上的问题,实际上最好在 YouTube 的全屏按钮上使用列表器。
还要注意隐藏状态栏的技巧,让它看起来像全屏。