Vue uses the video tag to implement video playback

Vue uses the video tag to implement video playback

This article shares the specific code of Vue using the video tag to implement video playback for your reference. The specific content is as follows

Project requirements: Dynamically display the video scroll bar, prohibit video downloading, update the current duration every 5 seconds during playback, and pause the video to display a prompt every 10 minutes.
Previously, when playing videos, the vue-video-player component was basically used because it has complete encapsulation functions and good playback source compatibility.
Through the requirements of this project, I also studied the properties and methods of the video tag in depth. Let me share the details with you here.

The specific usage is as follows

 <template>
  <!-- Video component -->
  <div id="common-video" class="h-100">
    <div :class="{ isShow: control }" class="h-100">
      <video
        ref="myVideo"
        :poster="poster"
        :src="src"
        :controls="controls"
        oncontextmenu="return false"
        @timeupdate="timeupdate"
        controlslist="nodownload"
        class="video-box"
      ></video>
      <img
        src="@/assets/images/playbtn.png"
        alt=""
        @click="operateVideo"
        class="pointer operate-btn"
        :class="{ 'fade-out': videoState }"
      />
    </div>
  </div>
</template>

<script>
export default {
  name: "CommonVideo",
  data() {
    return {
      videoState: false, // Video playback status // Study time studyTime: {
        currentTime: 0, // Current learning time duration: 0 // Total time},
      timer: {}, // timer pauseTimer: {} // pause timer };
  },
  /**
   * @param poster display image* @param src video resource* @param controls whether to display controls* @param control control* @param videoData basic video data*/
  props: {
    poster:
      type: String,
      required: false,
      default: ""
    },
    src: {
      type: String,
      required: true
    },
    controls: {
      type: Boolean,
      required: false,
      default: true
    },
    control: {
      type: Boolean,
      required: false,
      default: false
    },
    videoData: {
      type: Object,
      required: true
    }
  },
  mounted() {
    // Listen for video playback this.$refs.myVideo.addEventListener("play", () => {
      console.log("video is playing");
      this.openTimer();
    });
    // Listen for video pause this.$refs.myVideo.addEventListener("pause", () => {
      console.log("video is stopped");
      this.closeTimer();
    });
  },
  methods: {
    // Open the timer openTimer() {
      this.timer = setInterval(() => {
        this.$emit("videoStudyTime", this.studyTime);
      }, 5000);
    },
    // Close the timer closeTimer() {
      clearInterval(this.timer);
      this.$emit("videoStudyTime", this.studyTime);
    },
    // Open the pause timer openPauseTimer() {
      this.pauseTimer = setInterval(() => {
        this.hintOperate();
      }, 600000);
    },
    // Close the pause timer closePauseTimer() {
      clearInterval(this.pauseTimer);
    },
    // Hint operation hintOperate() {
      this.operateVideo();
      this.$alert("Please click to confirm and continue learning", "Prompt", {
        confirmButtonText: "Confirm",
        confirmButtonClass: "hint-btn",
        showClose: false,
        callback: action => {
          this.$refs.myVideo.currentTime = this.videoData.currentTime;
          this.operateVideo();
          this.openPauseTimer();
        }
      });
    },
    // Get the current playback position timeupdate(e) {
      this.studyTime.currentTime = e.target.currentTime;
      this.studyTime.duration = e.target.duration ? e.target.duration : 0;
    },
    // Operate video playback and pause operateVideo() {
      if (!this.src) {
        this.$message({ message: "No video resources, please check other videos!" });
        return false;
      }
      if (this.$refs.myVideo.paused) {
        this.$refs.myVideo.play();
        this.videoState = true;
      } else {
        this.$refs.myVideo.pause();
        this.videoState = false;
      }
    }
  },
  watch:
    //Monitoring operation videoData(val, oldVal) {
      const { currentTime, duration } = val;
      if (currentTime && duration && currentTime < duration) {
        this.hintOperate();
      }
    }
  }
};
</script>

<style lang="less">
#common-video {
  position: relative;
  .video-box {
    box-sizing: border-box;
    border: 0;
    display: block;
    width: 100%;
    height: 100%;
    outline: none !important;
  }
  .isShow {
    //Progress bar video::-webkit-media-controls-timeline {
      display: none;
    }
  }
  video::-webkit-media-controls-play-button {
    visibility: hidden;
  }
  .operate-btn {
    display: block;
    width: 60px;
    height: 60px;
    position: absolute;
    top: calc(50% - 30px);
    left: calc(50% - 30px);
  }
  .operate-btn:hover {
    opacity: 0.8;
  }
  .fade-out {
    opacity: 0;
  }
}
</style>

Note:

1. Use the isShow attribute with CSS style to dynamically display the video scroll bar
2. Use the video tag's oncοntextmenu="return false" attribute to prohibit downloading
3. Use the @timeupdate="timeupdate" method of the video tag to monitor the progress of video playback
4. Use the ref attribute of vue to bind the monitoring event to the video tag to implement other functions, such as duration statistics, delay prompts, dynamic display of icon play buttons, etc.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to use the vue-video-player plugin for vue video playback
  • Vue uses video.js for video playback
  • Example of how to play m3u8 video stream with Vue combined with Video.js
  • Vue video playback pause code
  • Detailed configuration of vue-video-player video player
  • vue + typescript + video.js to realize streaming video monitoring function
  • How to solve the m3u8 video playback format through vue video.js
  • vue-dplayer video player example code
  • Vue+video.js implements video playlist
  • How to use h5 video tag in Vue to play local video in pop-up window

<<:  MySQL optimization tutorial: large paging query

>>:  Implementation of Nginx filtering access logs of static resource files

Recommend

Nodejs implements intranet penetration service

Table of contents 1. Proxy in LAN 2. Intranet pen...

Compile CPP files using G++ in Ubuntu

When I used g++ to compile the cpp file for the f...

Summary of MySQL time statistics methods

When doing database statistics, you often need to...

How to improve Idea startup speed and solve Tomcat log garbled characters

Table of contents Preface Idea startup speed Tomc...

How to Rename a Group of Files at Once on Linux

In Linux, we usually use the mv command to rename...

Detailed explanation of keepAlive usage in Vue front-end development

Table of contents Preface keep-avlive hook functi...

Example of converting JS one-dimensional array into three-dimensional array

Today I saw a friend asking a question in the Q&a...

Basic HTML directory problem (difference between relative path and absolute path)

Relative path - a directory path established based...

Nginx implements https website configuration code example

https base port 443. It is used for something cal...

Vue project code splitting solution

Table of contents background Purpose Before split...

TypeScript learning notes: type narrowing

Table of contents Preface Type Inference Truth va...

Detailed introduction to the MySQL installation tutorial under Windows

Table of contents 1. Some concepts you need to un...

Detailed explanation of vuex persistence in practical application of vue

Table of contents vuex persistence Summarize vuex...

Super simple implementation of Docker to build a personal blog system

Install Docker Update the yum package to the late...

Vue local component data sharing Vue.observable() usage

As components become more detailed, you will enco...