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

This article helps you understand PReact10.5.13 source code

Table of contents render.js part create-context.j...

Three ways to implement animation in CSS3

This is a test of the interviewee's basic kno...

How to add custom system services to CentOS7 systemd

systemd: The service systemctl script of CentOS 7...

MySQL: mysql functions

1. Built-in functions 1. Mathematical functions r...

SQL-based query statements

Table of contents 1. Basic SELECT statement 1. Qu...

How to build a redis cluster using docker

Table of contents 1. Create a redis docker base i...

Summary of the differences between global objects in nodejs and browsers

In Node.js, a .js file is a complete scope (modul...

Detailed explanation of Redis master-slave replication practice using Docker

Table of contents 1. Background 2. Operation step...

How to use Linux tr command

01. Command Overview The tr command can replace, ...

Understanding render in Vue scaffolding

In the vue scaffolding, we can see that in the ne...

How to build a Vue3 desktop application

In this article, we will look at how to develop a...

The difference and use of json.stringify() and json.parse()

1. Differences between JSON.stringify() and JSON....