WeChat applet to achieve automatic video playback imitating GIF animation effect example

WeChat applet to achieve automatic video playback imitating GIF animation effect example

Demand background:

Insert GIF dynamic images into the mini program page, but GIF images are generally large in size. Instead, use the automatic video playback mode to simulate the effect of GIF images and enrich the page display. Automatically play videos, no control bar, no sound, and automatically loop.

Technical difficulties:

Because the WeChat mini program is on the same page, when there are multiple videos (no more than 3 videos are recommended), there will be lag or even crashes.
developers.weixin.qq.com/community/d…

plan:

Refer to the discussion plan of the mini program community. When the video does not appear in the visible area of ​​the screen, use a picture to take up the place, appear on the screen, replace the picture with the video, and play it automatically.

Code notes:

The video tag is controlled by wx:if, and the image tag is occupied by the visibility style.

<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:
        // Video width videoWidth: {
            type: Number,
            value: 0,
        },
        // Video height videoHeight: {
            type: Number,
            value: 0,
        },
        // Video poster/cover poster: {
            type: String,
            value: '',
        },
        // Video link videoUrl: {
            type: String,
            value: '',
        },
        // Whether to display the playback progress bar controls: {
            type: Boolean,
            value: false,
        },
        // Whether to display the middle play button showCenterPlayBtn: {
            type: Boolean,
            value: false,
        },
        // Is it muted: {
            type: Boolean,
            value: true,
        },
        // Whether to display the mute button showMuteBtn: {
            type: Boolean,
            value: true,
        },
        // Whether to enable gesture control progress enableProgressGesture: {
            type: Boolean,
            value: false,
        },
        // Whether to enable gesture control playback enablePlayGesture: {
            type: Boolean,
            value: false,
        },
        // direction: {
            type: Number,
            value: 0,
        },
        //Specify the start time of playback, in seconds showPlayTime: {
            type: Number,
            value: 0,
        },
        // Video id (unique identifier)
        videoId: {
            type: String,
            value: '',
        },
        // Whether to play play: {
            type: Boolean,
            value: false,
        },
        //Whether to loop playback loop: {
            type: Boolean,
            value: true,
        },
    },
    data: {
        paly: false, // Whether to play context: null, // The created video instance object},
    lifetimes:
        attached() {
            this.videObserve();
        },
    },
    methods: {
        // Monitor whether the video component enters the visible area videObserve() {
            this._observer = this.createIntersectionObserver({
                observeAll: true,
            });

            this._observer.relativeToViewport().observe(`#image_${this.data.videoId}`, (res) => {
                //res.intersectionRatio === 0 means no intersectionif (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,
                            },
                            () => {
                                // The delay is to wait for the wxml node to be created, and then play it after getting the node, otherwise the playback may fail setTimeout(() => {
                                    ctx.play();
                                }, 400);
                            }
                        );
                    }
                }
            });
        },
    },
});

Rendering

The video will be loaded and start playing only after it enters the visible area. When the video leaves the visible area, play=false is set, and the video tag is cleared, that is, the video is cleared.

Future optimization points

Currently, when the video starts to render, a black screen will appear. We will see if it can be changed to white later (white is more acceptable than black), and it also depends on the support of mini program video.

There is currently no limit on the number of videos that can be played simultaneously on one screen. If the video width and height are relatively small, there may be many videos on one screen, resulting in lag or crash.

Summarize

This is the end of this article about how to use WeChat mini-programs to automatically play videos and imitate GIF animations. For more related mini-programs that automatically play videos and imitate GIF animations, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Click on the picture to play the video automatically

<<:  Detailed explanation of the construction and interface management of Docker private warehouse

>>:  Examples of 4 methods for inserting large amounts of data in MySQL

Recommend

Solution to MySql service disappearance for unknown reasons

Solution to MySql service disappearance for unkno...

Example of using store in vue3 to record scroll position

Table of contents Overall Effect Listen for conta...

Access the MySQL database by entering the DOS window through cmd under Windows

1. Press win + R and type cmd to enter the DOS wi...

How to create users and manage permissions in MySQL

1. How to create a user and password 1. Enter the...

How to view and configure password expiration on Linux

With the right settings, you can force Linux user...

Detailed explanation of Linux index node inode

1. Introduction to inode To understand inode, we ...

How to use async and await correctly in JS loops

Table of contents Overview (Loop Mode - Common) D...

Common HTML tag writing errors

We better start paying attention, because HTML Po...

Example code for implementing an Upload component using Vue3

Table of contents General upload component develo...

Vue3.0 implements the encapsulation of the drop-down menu

Vue3.0 has been out for a while, and it is necess...

mysql installer web community 5.7.21.0.msi installation graphic tutorial

This article example shares the specific code for...

Vue3+script setup+ts+Vite+Volar project

Table of contents Create a vue + ts project using...