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

6 solutions to IDEA's inability to connect to the MySQL database

This article mainly introduces 6 solutions to the...

Sample code for implementing radar chart with vue+antv

1. Download Dependency npm install @antv/data-set...

Docker5 full-featured harbor warehouse construction process

Harbor is an enterprise-level registry server for...

Docker uses Supervisor to manage process operations

A Docker container starts a single process when i...

Practical method of deleting associated tables in MySQL

In the MySQL database, after tables are associate...

Complete steps to install mysql5.7 on Mac (with pictures and text)

I recently used a Mac system and was preparing to...

How to build a K8S cluster and install docker under Hyper-V

If you have installed the Win10 system and want t...

The difference and introduction of ARGB, RGB and RGBA

ARGB is a color mode, which is the RGB color mode...

Linux centOS installation JDK and Tomcat tutorial

First download JDK. Here we use jdk-8u181-linux-x...

Detailed steps to install MySQL on CentOS 7

In CentOS7, when we install MySQL, MariaDB will b...