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

微信小程序?qū)崿F(xiàn)自動播放視頻模仿gif動圖效果實(shí)例

 更新時(shí)間:2021年07月09日 13:09:07   作者:ananas_ananas  
這篇文章主要給大家介紹了關(guān)于微信小程序?qū)崿F(xiàn)自動播放視頻模仿gif動圖效果的相關(guān)資料,通過本文介紹的方法可以實(shí)現(xiàn)自動播放視頻,視頻無控制條無聲音且自動循環(huán)播放,需要的朋友可以參考下

需求背景:

在小程序頁面插入gif動態(tài)圖,但gif圖一般體積比較大,轉(zhuǎn)而用自動播放視頻的模式來模擬gif圖的效果,豐富頁面展示。自動播放的視頻,無控制條,無聲音,自動循環(huán)播放。

技術(shù)難點(diǎn):

因?yàn)槲⑿判〕绦蛟谕粋€(gè)頁面,存在多個(gè)視頻時(shí)(建議不超過3個(gè)視頻),會出現(xiàn)卡頓甚至閃退的情況。
developers.weixin.qq.com/community/d

方案:

參考小程序社區(qū)討論方案,當(dāng)視頻未出現(xiàn)在屏幕可視區(qū)域時(shí),用圖片占位,出現(xiàn)在屏幕中,把圖片替換成視頻,并且自動播放。

代碼注意點(diǎn):

video標(biāo)簽用wx:if來控制,image標(biāo)簽用visibility樣式來占位。

<view class="container" style="width: {{videoWidth}}rpx;height: {{videoHeight}}rpx">
  <image class="image" style="visibility: {{play ? 'hidden' : 'visible'}};" id="image_{{videoId}}" src="{{poster}}" />
  <video class="video" wx:if="{{play}}" id="video_{{videoId}}" controls="{{controls}}" object-fit='contain' show-center-play-btn="{{showCenterPlayBtn}}" enable-progress-gesture="{{enableProgressGesture}}" direction="{{direction}}" enable-play-gesture="{{enablePlayGesture}}" muted="{{muted}}" loop="{{loop}}" src="{{videoUrl}}" />
</view>
.container {
    position: relative;
    width: 100%;
    height: 100%;
}
.image {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 10;
    width: 100%;
    height: 100%;
}
.video {
    width: 100%;
    height: 100%;
}
Component({
    properties: {
        // 視頻寬度
        videoWidth: {
            type: Number,
            value: 0,
        },
        // 視頻高度
        videoHeight: {
            type: Number,
            value: 0,
        },
        // 視頻海報(bào)/封面圖
        poster: {
            type: String,
            value: '',
        },
        // 視頻鏈接
        videoUrl: {
            type: String,
            value: '',
        },
        // 是否顯示播放進(jìn)度條
        controls: {
            type: Boolean,
            value: false,
        },
        // 是否顯示中間播放按鈕
        showCenterPlayBtn: {
            type: Boolean,
            value: false,
        },
        // 是否靜音
        muted: {
            type: Boolean,
            value: true,
        },
        // 是否顯示靜音按鈕
        showMuteBtn: {
            type: Boolean,
            value: true,
        },
        // 是否啟用手勢控制進(jìn)度
        enableProgressGesture: {
            type: Boolean,
            value: false,
        },
        // 是否啟用手勢控制播放
        enablePlayGesture: {
            type: Boolean,
            value: false,
        },
        // 方向
        direction: {
            type: Number,
            value: 0,
        },
        // 指定開始播放時(shí)間,單位:秒
        showPlayTime: {
            type: Number,
            value: 0,
        },
        // 視頻id(唯一標(biāo)識)
        videoId: {
            type: String,
            value: '',
        },
        // 是否播放
        play: {
            type: Boolean,
            value: false,
        },
        // 是否循環(huán)播放
        loop: {
            type: Boolean,
            value: true,
        },
    },
    data: {
        paly: false, // 是否播放
        context: null, // 創(chuàng)建的視頻實(shí)例對象
    },
    lifetimes: {
        attached() {
            this.videObserve();
        },
    },
    methods: {
        // 監(jiān)聽視頻組件是否進(jìn)入可視區(qū)域
        videObserve() {
            this._observer = this.createIntersectionObserver({
                observeAll: true,
            });

            this._observer.relativeToViewport().observe(`#image_${this.data.videoId}`, (res) => {
                // res.intersectionRatio === 0表示不相交
                if (res.intersectionRatio === 0) {
                    this.setData({
                        play: false,
                    });
                } else {
                    const ctx = this.data.context || wx.createVideoContext(`video_${this.data.videoId}`, this);
                    if (ctx) {
                        this.setData(
                            {
                                context: ctx,
                                play: true,
                            },
                            () => {
                                // 加延時(shí)是為了等wxml節(jié)點(diǎn)創(chuàng)建完,拿到節(jié)點(diǎn)再播放,否則可能會出現(xiàn)播放失敗
                                setTimeout(() => {
                                    ctx.play();
                                }, 400);
                            }
                        );
                    }
                }
            });
        },
    },
});

效果圖

視頻進(jìn)入可視區(qū)域,才加載視頻開始播放。視頻離開可視區(qū)域,play=false,清除video標(biāo)簽,即清除視頻。

未來優(yōu)化點(diǎn)

目前視頻剛開始渲染時(shí),會出現(xiàn)閃黑屏的效果。后續(xù)看看能否改成白色(白色比黑色好接受一些),也要看小程序視頻支持情況。

目前未限制一屏不能超過3個(gè)視頻同時(shí)播放。如果視頻寬高比較小,可能會出現(xiàn)一屏有很多視頻,從而卡頓或閃退。

總結(jié)

到此這篇關(guān)于微信小程序?qū)崿F(xiàn)自動播放視頻模仿gif動圖效果的文章就介紹到這了,更多相關(guān)小程序自動播放視頻模仿gif動圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論