欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

react-native 封裝視頻播放器react-native-video的使用

 更新時(shí)間:2023年01月31日 15:07:03   作者:在下月亮有何貴干  
本文主要介紹了react-native 封裝視頻播放器react-native-video的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

前言

最近工作業(yè)務(wù)用到了react-native-video,還需要能夠全屏,全屏需要用到鎖定應(yīng)用方向(橫屏),需要用到組件react-native-orientation-locker,本文記錄使用方法以及提供一種解決思路。

react-native-orientation-locker

橫豎屏方法

我就只介紹這常用的三個(gè),其他的可以翻看官方文檔

import Orientation from 'react-native-orientation-locker';
Orientation.lockToLandscapeLeft() // 向左方向鎖定橫屏
Orientation.lockToLandscapeRight() // 向右方向鎖定橫屏
Orientation.unlockAllOrientations(); // 解除鎖定
Orientation.lockToPortrait(); // 鎖定豎屏

react-native-video

導(dǎo)入

import Video from 'react-native-video';

函數(shù)部分

? // 設(shè)置總時(shí)長(zhǎng)
? setDuration({ duration }) {
? ? this.setState({ duration });
? }

? // 播放結(jié)束可將暫停變量設(shè)置為true
? onEnd() {
? ? this.setState({
? ? ? paused: true,
? ? });
? }

? // 緩沖,loading變量可控制緩沖圈顯示
? onBuffer({ isBuffering }) {
? ? this.setState({ loading: isBuffering });
? }

? // 設(shè)置進(jìn)度條和播放時(shí)間的變化,sliderValue用來(lái)同步步進(jìn)器
? 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(); // 播放結(jié)束時(shí)執(zhí)行
            }}
            onBuffer={data => this.onBuffer(data)} // 緩沖時(shí)執(zhí)行,用于顯示緩沖圈
            onProgress={data => this.setTime(data)} // 播放時(shí)執(zhí)行函數(shù),用于同步步進(jìn)器進(jìn)度
            onLoad={data => this.setDuration(data)} // 播放前得到總時(shí)長(zhǎng),用于步進(jìn)器設(shè)置總長(zhǎng)
            muted={muted} // 靜音
        />
    </View>

控制臺(tái)

    <View style={styles.videoControl}>
        {/* 暫停按鈕 */}
        <TouchableOpacity
            onPress={() => {
                this.changePlayed();
            }}
        >
            <Image style={styles.paused} source={paused ? pausedImg : played} />
        </TouchableOpacity>
        <Slider
            value={sliderValue}
            maximumValue={duration}
            // onValueChange 和 onSlidingComplete 是修改步進(jìn)器進(jìn)度時(shí)觸發(fā)的函數(shù)
            // 可以在此時(shí)同步視頻播放,同步視頻播放的函數(shù)是,video的Ref.seek()
            // 中間需要設(shè)置視頻暫停和播放,否則邊拖動(dòng)邊播放會(huì)很奇怪
            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
                // 可以鎖定應(yīng)用為橫屏,這里的狀態(tài)設(shè)置是我的全屏解決方案
                this.setState({ fullVideoShow: true, sliderValue2: sliderValue }, () => {
                  this.setState({ paused: true });// 需要將原視頻暫停
                });
                Orientation.lockToLandscapeLeft();
            }}
        >
            <Image style={{ marginLeft: 10, height: 20, width: 20 }} source={fullScreen} />
        </TouchableOpacity>
    </View>

全屏實(shí)現(xiàn)方案(可參考)

我采用的是彈出層方案,使用Orientation橫屏?xí)r,新建一個(gè)model層覆蓋全屏,然后新建一個(gè)相同的播放組件,記得將原視頻組件暫停。

可以參考的點(diǎn),以下表示model層上的視頻組件

? // 放大時(shí),總長(zhǎng)已經(jīng)不需要再次獲取,我們可以在onLoad2時(shí)將sliderValue賦值給video2
? // 達(dá)到放大時(shí)同步進(jìn)度的效果
? onLoad2(data) {
? ? this.video2.seek(this.state.sliderValue);
? }
??
? // 設(shè)置vedio2的同步步進(jìn)器2進(jìn)度時(shí),需要注意,currentTime>0再賦值
? // 否則在視頻加載過(guò)程中會(huì)出現(xiàn)步進(jìn)器2跳一下0再恢復(fù)的情況
? setTime2({ currentTime }) {
? ? if (currentTime > 0) {
? ? ? this.setState({
? ? ? ? sliderValue2: currentTime,
? ? ? });
? ? }
? }
??
? // 退出全屏
? outFullScreen() {
? ? const { sliderValue2, paused2 } = this.state;
? ? this.setState({ fullVideoShow: false, sliderValue: sliderValue2);
? ? Orientation.lockToPortrait();
? ? // 退出時(shí)將原視頻進(jìn)度同步
? ? this.video.seek(sliderValue2);
? }
??
? // 播放暫停
? changePlayed2() {
? ? this.setState({ paused2: !this.state.paused2 });
? }
??
? // 另外全屏?xí)r,要將原視頻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' /> //緩沖圈可復(fù)用狀態(tài)
                    </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>

尾言

樣式我沒(méi)有寫(xiě)出來(lái),因?yàn)閮?nèi)容可能比較多,布局情況也不大相同,想完全復(fù)用不太現(xiàn)實(shí),不過(guò)如果你耐心點(diǎn)理解重要的部分,相信你會(huì)有所收獲。

到此這篇關(guān)于react-native 封裝視頻播放器react-native-video的使用的文章就介紹到這了,更多相關(guān)react-native 視頻播放器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 解決React報(bào)錯(cuò)Property does not exist on type 'JSX.IntrinsicElements'

    解決React報(bào)錯(cuò)Property does not exist on 

    這篇文章主要為大家介紹了React報(bào)錯(cuò)Property does not exist on type 'JSX.IntrinsicElements'解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • React+TypeScript項(xiàng)目中使用CodeMirror的步驟

    React+TypeScript項(xiàng)目中使用CodeMirror的步驟

    CodeMirror被廣泛應(yīng)用于許多Web應(yīng)用程序和開(kāi)發(fā)工具,之前做需求用到過(guò)codeMirror這個(gè)工具,覺(jué)得還不錯(cuò),功能很強(qiáng)大,所以記錄一下改工具的基礎(chǔ)用法,對(duì)React+TypeScript項(xiàng)目中使用CodeMirror的步驟感興趣的朋友跟隨小編一起看看吧
    2023-07-07
  • React Native自定義路由管理的深入理解

    React Native自定義路由管理的深入理解

    路由管理的功能主要指的頁(yè)面跳轉(zhuǎn)、goBack、帶參數(shù)跳轉(zhuǎn)等功能,這篇文章主要給大家介紹了關(guān)于React Native自定義路由管理的相關(guān)資料,需要的朋友可以參考下
    2021-08-08
  • react自定義實(shí)現(xiàn)狀態(tài)管理詳解

    react自定義實(shí)現(xiàn)狀態(tài)管理詳解

    這篇文章主要為大家詳細(xì)介紹了react如何自定義實(shí)現(xiàn)狀態(tài)管理,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-01-01
  • React Native中NavigatorIOS組件的簡(jiǎn)單使用詳解

    React Native中NavigatorIOS組件的簡(jiǎn)單使用詳解

    這篇文章主要介紹了React Native中NavigatorIOS組件的簡(jiǎn)單使用詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • 深入理解React調(diào)度(Scheduler)原理

    深入理解React調(diào)度(Scheduler)原理

    本文主要介紹了深入理解React調(diào)度(Scheduler)原理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • React?Context?變遷及背后實(shí)現(xiàn)原理詳解

    React?Context?變遷及背后實(shí)現(xiàn)原理詳解

    這篇文章主要為大家介紹了React?Context?變遷及背后實(shí)現(xiàn)原理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • ReactDOM.render在react源碼中執(zhí)行原理

    ReactDOM.render在react源碼中執(zhí)行原理

    這篇文章主要為大家介紹了ReactDOM.render在react源碼中執(zhí)行原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • React實(shí)現(xiàn)基于Antd密碼強(qiáng)度校驗(yàn)組件示例詳解

    React實(shí)現(xiàn)基于Antd密碼強(qiáng)度校驗(yàn)組件示例詳解

    這篇文章主要為大家介紹了React實(shí)現(xiàn)基于Antd密碼強(qiáng)度校驗(yàn)組件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • react中useRef的應(yīng)用使用詳解

    react中useRef的應(yīng)用使用詳解

    這篇文章主要介紹了react中useRef的應(yīng)用使用詳解的相關(guān)資料,需要的朋友可以參考下
    2023-05-05

最新評(píng)論