Vue实现视频播放(一):使用原生方法写一个视频播放组件

看的是黑马程序员的教程,记录下来当做笔记。

HTML:

使用了video标签

该标签的事件:
ontimeupdate:在视频进行播放的时候持续触发,我们可以监听这个事件,并在这个事件中获取视频的当前播放时间;
oncanplay:在视频信息加载完毕之后触发
总时长可以通过oncanplay获取,但是当前播放时间需要在ontimeupdate事件中持续更新,当视频信息加载完毕之后,会自动的触发oncanplay事件。
该标签的属性:
currrentTime:可以获取到当前视频播放到的时间,以秒做为单位;
duration:可以获取到视频的总时长,以秒做为单位;

<template>
  <div class="video">
    <video ref="myvideo" @canplay="getTotal" @timeupdate="timeupdate" @click="showControls">
      <source src="https://video.pearvideo.com/mp4/short/20200209/cont-1650197-14888002-hd.mp4"/>
    </video>
    <transition
            enter-active-class="animated fadeIn slow"
            leave-active-class="animated fadeOut slow"
      >
    <div class="controls" v-show="isShow">
      <div class="con_left">
        <!--播放与暂停-->
        <span :class="{'icon-play3':!isPaused,'icon-pause2':isPaused}" @click="togglePlay"></span>
        <!--停止-->
        <span class="icon-stop2" @click="stopPlay"></span>
        <!--播放时间/总时长-->
        <span>{{currentTime}}/{{totalTime}}</span>
      </div>

      <div class="con_right">
        <!--声音-->
        <span :class="{'icon-volume-low':!isMuted,'icon-volume-mute2':isMuted}" @click="toggleMuted"></span>
        <!--全屏-->
        <span class="icon-loop" @click="toggleFullScreen"></span>
      </div>
    </div>
    </transition>
  </div>
</template>

JS:

具体代码的作用可以看注释。

<script>
  //动画 npm i animate.css
  import "animate.css";
  // 引入字体样式文件
  import '../styles/style.css'
  export default {
    name: "Drive",
    data () {
      return {
        myvideo: null,

        //标记当前的播放状态
        isPaused: false,
        //标记当前是否静音
        isMuted: false,
        //当前播放时间
        currentTime: '00:00',
        //总时长
        totalTime: '00:00',
        //标记控制面板是否可见
        isShow: true,
        //开始时间,毫秒为单位
        startTime: 0
      }
    },
    mounted () {
      //获取播放器元素
      this.myvideo = this.$refs.myvideo

      //记住起始时间,这个时间可以作为时间间隔的参照
      this.startTime = Date.now()

      //开启一个定时器
      setInterval(() => {
        if(Date.now() - this.startTime > 5000) {
          this.isShow = false
        }
      },1000)
    },
    methods :{
      //播放与暂停
      togglePlay() {
        //console.log("click")

        //视频标签(video)原生方法:
        //play():让视频播放
        //pause():让视频暂停

        //修改当前的播放状态
        this.isPaused = !this.isPaused
        if(this.isPaused) {
          this.myvideo.play()
        }else {
          this.myvideo.pause()
        }

      },
      //停止播放
      stopPlay() {
        //没有提供原生的stop方法
        //this.myvideo.stop()
        this.myvideo.pause()
        //currentTime这个属性就是用来标记当前视频播放到的时间,以秒为单位
        this.myvideo.currentTime = 0
        //重置播放状态为初始值
        this.isPaused = false
      },
      //时间格式化处理
      timeFormat(time) {
        let minute = Math.floor((time % 3600) / 60)
        let second = Math.floor(time % 60)
        minute = minute < 10 ? "0" + minute : minute
        second = second < 10 ? "0" + second : second
        return `${minute}:${second}`
      },
      //获取总时长
      getTotal() {
        console.log(this.myvideo.duration)
        this.totalTime = this.timeFormat(this.myvideo.duration)
      },
      //获取当前视频播放到的时间
      timeupdate() {
        this.currentTime = this.timeFormat(this.myvideo.currentTime)
      },
      //静音操作
      toggleMuted() {
        //修改图标
        this.isMuted = !this.isMuted
        //静音
        this.myvideo.muted = !this.myvideo.muted
      },
      //全屏切换
      toggleFullScreen(event) {
        const myvideo = this.$refs.myvideo
        //如果当前是全屏状态,就退出全屏,否则进入全屏状态
        //获取当前的全屏状态
        let isFullscreen = document.webkitIsFullScreen || document.fullscreen
        if(!isFullscreen) {
          const inFun = myvideo.requestFullscreen || myvideo.webkitRequestFullScreen
          //让当前播放器进入全屏状态
          inFun.call(myvideo)
        }else {
          const exitFun = document.exitFullscreen || document.webkitExitFullScreen
          //退出全屏状态要使用document
          exitFun.call(document)
        }
      },
      //显示控制面板
      showControls() {
        this.isShow = true
        // 一定要记得更新起始时间的参照
        this.startTime = Date.now()
      }
    }
  }
</script>
  • 3
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
好的,下面是一个简单的Vue原生兼容安卓和H5的短视频滑动播放器的实现: 1. 首先需要安装 `vue-touch` 插件,它可以让我们方便地处理触摸事件。可以使用以下命令安装: ``` npm install vue-touch ``` 2. 在Vue组件中引入 `vue-touch` 插件: ```javascript import VueTouch from 'vue-touch'; Vue.use(VueTouch); ``` 3. 定义一个 `SwipePlayer` 组件,该组件包含一个视频播放器和一个滑动条: ```html <template> <div class="swipe-player"> <video :src="source" ref="videoRef"></video> <div ref="sliderRef" class="slider"></div> </div> </template> ``` 4. 在组件的 `mounted` 方法中,初始化滑动条,并添加滑动事件处理程序: ```javascript mounted() { // 初始化滑动条 const slider = this.$refs.sliderRef; slider.style.width = '0%'; // 添加滑动事件处理程序 this.$swipeRight(this.handleSwipeRight); this.$swipeLeft(this.handleSwipeLeft); }, methods: { // 处理向右滑动事件 handleSwipeRight() { const video = this.$refs.videoRef; const slider = this.$refs.sliderRef; // 获取当前视频播放进度 const currentTime = video.currentTime; const duration = video.duration; // 计算新的播放进度 const newTime = Math.max(currentTime - 5, 0); const percent = (newTime / duration) * 100; // 更新视频播放进度和滑动条 video.currentTime = newTime; slider.style.width = `${percent}%`; }, // 处理向左滑动事件 handleSwipeLeft() { const video = this.$refs.videoRef; const slider = this.$refs.sliderRef; // 获取当前视频播放进度 const currentTime = video.currentTime; const duration = video.duration; // 计算新的播放进度 const newTime = Math.min(currentTime + 5, duration); const percent = (newTime / duration) * 100; // 更新视频播放进度和滑动条 video.currentTime = newTime; slider.style.width = `${percent}%`; } } ``` 5. 在 `data` 中定义视频的源地址: ```javascript data() { return { source: 'http://example.com/video.mp4' }; } ``` 6. 最后,样式可以根据需要进行自定义: ```css .swipe-player { position: relative; width: 100%; height: 300px; } .swipe-player video { width: 100%; height: 100%; object-fit: cover; } .swipe-player .slider { position: absolute; bottom: 0; left: 0; width: 100%; height: 5px; background-color: #ddd; z-index: 1; } ``` 这样就完成了一个简单的Vue原生兼容安卓和H5的短视频滑动播放器。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值