網站首頁 編程語言 正文
前言
最近工作業務用到了react-native-video,還需要能夠全屏,全屏需要用到鎖定應用方向(橫屏),需要用到組件react-native-orientation-locker,本文記錄使用方法以及提供一種解決思路。
react-native-orientation-locker
橫豎屏方法
我就只介紹這常用的三個,其他的可以翻看官方文檔
import Orientation from 'react-native-orientation-locker';
Orientation.lockToLandscapeLeft() // 向左方向鎖定橫屏
Orientation.lockToLandscapeRight() // 向右方向鎖定橫屏
Orientation.unlockAllOrientations(); // 解除鎖定
Orientation.lockToPortrait(); // 鎖定豎屏
react-native-video
導入
import Video from 'react-native-video';
函數部分
? // 設置總時長
? setDuration({ duration }) {
? ? this.setState({ duration });
? }
? // 播放結束可將暫停變量設置為true
? onEnd() {
? ? this.setState({
? ? ? paused: true,
? ? });
? }
? // 緩沖,loading變量可控制緩沖圈顯示
? onBuffer({ isBuffering }) {
? ? this.setState({ loading: isBuffering });
? }
? // 設置進度條和播放時間的變化,sliderValue用來同步步進器
? setTime({ currentTime }) {
? ? this.setState({
? ? ? sliderValue: currentTime,
? ? });
? }
??
? // 播放暫停
? changePlayed() {
? ? this.setState({ paused: !this.state.paused });
? }
視頻組件
<View style={styles.Video}> { loading ? <View style={styles.loading}> <ActivityIndicator size='large' color='lightgray' /> // 緩沖圈 </View> : null } <Video ref={(ref: Video) => { this.video = ref; // 視頻Video的ref }} source={{ uri: 'http://xxx/xxx.mp4', //播放路徑 }} style={{ width: '100%', height: '100%' }} rete={1} volume={1} paused={paused} // 暫停變量 onEnd={() => { this.onEnd(); // 播放結束時執行 }} onBuffer={data => this.onBuffer(data)} // 緩沖時執行,用于顯示緩沖圈 onProgress={data => this.setTime(data)} // 播放時執行函數,用于同步步進器進度 onLoad={data => this.setDuration(data)} // 播放前得到總時長,用于步進器設置總長 muted={muted} // 靜音 /> </View>
控制臺
<View style={styles.videoControl}> {/* 暫停按鈕 */} <TouchableOpacity onPress={() => { this.changePlayed(); }} > <Image style={styles.paused} source={paused ? pausedImg : played} /> </TouchableOpacity> <Slider value={sliderValue} maximumValue={duration} // onValueChange 和 onSlidingComplete 是修改步進器進度時觸發的函數 // 可以在此時同步視頻播放,同步視頻播放的函數是,video的Ref.seek() // 中間需要設置視頻暫停和播放,否則邊拖動邊播放會很奇怪 onValueChange={(value) => { this.video.seek(value); this.setState({ paused: true, }); }} onSlidingComplete={(value) => { this.video.seek(value); this.setState({ paused: false, }); }} style={styles.slider} /> {/* 靜音按鈕 */} <TouchableOpacity onPress={() => { this.setState({ muted: !muted }); }} > <Image style={{ marginLeft: 10, height: 24, width: 24 }} source={muted ? mute : sound} /> </TouchableOpacity> {/* 全屏按鈕 */} <TouchableOpacity onPress={() => { // 這里涉及到react-native-orientation-locker // 可以鎖定應用為橫屏,這里的狀態設置是我的全屏解決方案 this.setState({ fullVideoShow: true, sliderValue2: sliderValue }, () => { this.setState({ paused: true });// 需要將原視頻暫停 }); Orientation.lockToLandscapeLeft(); }} > <Image style={{ marginLeft: 10, height: 20, width: 20 }} source={fullScreen} /> </TouchableOpacity> </View>
全屏實現方案(可參考)
我采用的是彈出層方案,使用Orientation橫屏時,新建一個model層覆蓋全屏,然后新建一個相同的播放組件,記得將原視頻組件暫停。
可以參考的點,以下表示model層上的視頻組件
? // 放大時,總長已經不需要再次獲取,我們可以在onLoad2時將sliderValue賦值給video2
? // 達到放大時同步進度的效果
? onLoad2(data) {
? ? this.video2.seek(this.state.sliderValue);
? }
??
? // 設置vedio2的同步步進器2進度時,需要注意,currentTime>0再賦值
? // 否則在視頻加載過程中會出現步進器2跳一下0再恢復的情況
? setTime2({ currentTime }) {
? ? if (currentTime > 0) {
? ? ? this.setState({
? ? ? ? sliderValue2: currentTime,
? ? ? });
? ? }
? }
??
? // 退出全屏
? outFullScreen() {
? ? const { sliderValue2, paused2 } = this.state;
? ? this.setState({ fullVideoShow: false, sliderValue: sliderValue2);
? ? Orientation.lockToPortrait();
? ? // 退出時將原視頻進度同步
? ? this.video.seek(sliderValue2);
? }
??
? // 播放暫停
? changePlayed2() {
? ? this.setState({ paused2: !this.state.paused2 });
? }
??
? // 另外全屏時,要將原視頻paused暫停,可以在全屏按鈕事件那里我有提到。
放大視頻
<Modal visible={fullVideoShow} transparent animationType='slide' > <View style={styles.videoModelBack}> <View style={styles.videoModel}> { loading ? <View style={styles.loading}> <ActivityIndicator size='large' color='lightgray' /> //緩沖圈可復用狀態 </View> : null } <View style={{ flex: 1 }}> <Video ref={(ref: Video) => { this.video2 = ref; }} source={{ uri: 'http://xxx/xxx.mp4', }} style={{ flex: 1 }} rete={1} volume={1} paused={paused2} onEnd={() => { this.onEnd(0); }} onBuffer={data => this.onBuffer(data)} onProgress={data => this.setTime2(data)} onLoad={data => this.onLoad2(data)} muted={muted} /> </View> </View> <View style={styles.videoBack}> <TouchableOpacity onPress={() => { this.changePlayed2(); }} > <Image style={[styles.paused]} source={paused2 ? pausedImg : played} /> </TouchableOpacity> <Slider value={sliderValue2} maximumValue={duration} onValueChange={(value) => { this.video2.seek(value); this.setState({ paused2: true, }); }} onSlidingComplete={(value) => { this.video2.seek(value); this.setState({ paused2: false, }); }} style={styles.slider} /> <TouchableOpacity onPress={() => { this.setState({ muted: !muted }); }} > <Image style={styles.img} source={muted ? mute : sound} /> </TouchableOpacity> <TouchableOpacity onPress={() => { this.outFullScreen(); }} > <Image source={outFullScreen} /> // 退出全屏按鈕 </TouchableOpacity> </View> </View> </Modal>
尾言
樣式我沒有寫出來,因為內容可能比較多,布局情況也不大相同,想完全復用不太現實,不過如果你耐心點理解重要的部分,相信你會有所收獲。
原文鏈接:https://blog.csdn.net/weixin_43877799/article/details/125273128
相關推薦
- 2021-12-11 Android?NDK開發(C語言字符串)_Android
- 2024-03-23 asp.net web api 用戶身份驗證
- 2022-03-06 正則表達式用法詳解_正則表達式
- 2022-07-21 react 高價組件HOC實現組件復用
- 2022-06-02 C++超詳細講解單鏈表的實現_C 語言
- 2022-08-10 Go語言反射獲取類型屬性和方法示例_Golang
- 2022-11-23 Android集成GreenDao數據庫的操作步驟_Android
- 2022-11-20 TS?中的類型推斷與放寬實例詳解_其它
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支