当前位置:首页 > VUE

vue怎么实现课程视频

2026-02-24 13:46:53VUE

Vue 实现课程视频功能

使用 HTML5 video 标签嵌入视频

在 Vue 组件中可以直接使用 HTML5 的 <video> 标签来嵌入视频文件。这种方法适合本地存储或服务器直链的视频文件。

<template>
  <div>
    <video controls width="100%">
      <source src="/path/to/video.mp4" type="video/mp4">
      您的浏览器不支持 HTML5 视频
    </video>
  </div>
</template>

集成第三方视频平台

对于托管在 YouTube、腾讯云点播等平台的视频,可以使用它们的嵌入代码或 SDK。以 YouTube 为例:

<template>
  <div>
    <iframe 
      width="100%" 
      height="500"
      :src="'https://www.youtube.com/embed/' + videoId"
      frameborder="0"
      allowfullscreen>
    </iframe>
  </div>
</template>

<script>
export default {
  data() {
    return {
      videoId: 'YOUR_VIDEO_ID'
    }
  }
}
</script>

使用视频播放器组件

专业的视频播放器组件如 Video.js 或 vue-video-player 提供更多功能:

vue怎么实现课程视频

  1. 安装 vue-video-player:

    npm install vue-video-player
  2. 在组件中使用:

    vue怎么实现课程视频

    
    <template>
    <video-player 
     :options="playerOptions"
     @play="onPlayerPlay"
     @pause="onPlayerPause">
    </video-player>
    </template>
import { videoPlayer } from 'vue-video-player' export default { components: { videoPlayer }, data() { return { playerOptions: { autoplay: false, controls: true, sources: [{ type: "video/mp4", src: "/path/to/video.mp4" }] } } } } ```

实现视频列表和播放控制

创建视频列表组件,实现点击切换视频功能:

<template>
  <div>
    <div class="video-list">
      <div 
        v-for="(video, index) in videos" 
        :key="index"
        @click="changeVideo(video)">
        {{ video.title }}
      </div>
    </div>
    <video-player :options="currentPlayerOptions"></video-player>
  </div>
</template>

<script>
export default {
  data() {
    return {
      videos: [
        { title: '课程1', src: '/path/to/video1.mp4' },
        { title: '课程2', src: '/path/to/video2.mp4' }
      ],
      currentVideo: null
    }
  },
  computed: {
    currentPlayerOptions() {
      return {
        autoplay: false,
        controls: true,
        sources: [{
          type: "video/mp4",
          src: this.currentVideo?.src || ''
        }]
      }
    }
  },
  methods: {
    changeVideo(video) {
      this.currentVideo = video
    }
  }
}
</script>

添加视频进度保存功能

使用 localStorage 保存视频播放进度:

// 在播放器事件中添加进度保存
onPlayerTimeUpdate(player) {
  localStorage.setItem(
    `videoProgress_${this.videoId}`, 
    player.currentTime()
  )
}

// 组件创建时恢复进度
mounted() {
  const savedTime = localStorage.getItem(`videoProgress_${this.videoId}`)
  if(savedTime) {
    this.$refs.player.currentTime(parseFloat(savedTime))
  }
}

响应式设计调整

通过 CSS 确保视频在不同设备上显示正常:

.video-container {
  position: relative;
  padding-bottom: 56.25%; /* 16:9 比例 */
  height: 0;
  overflow: hidden;
}

.video-container iframe,
.video-container video {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

标签: 课程视频
分享给朋友:

相关文章

vue webrtc实现视频

vue webrtc实现视频

Vue 与 WebRTC 实现视频通信 WebRTC(Web Real-Time Communication)是一种支持浏览器实时音视频通信的技术。结合 Vue.js 框架,可以快速构建视频通话应用。…

vue实现课程购买

vue实现课程购买

Vue 实现课程购买功能 在 Vue 中实现课程购买功能需要结合前端界面设计、状态管理、API 调用等环节。以下是具体实现方法: 前端界面设计 创建课程列表和购买按钮,使用 Vue 组件化开发。课程…

vue实现视频功能

vue实现视频功能

Vue 实现视频播放功能 使用 Vue 实现视频功能可以通过原生 HTML5 <video> 标签或第三方库(如 video.js)来实现。以下是两种常见方法: 使用原生 HTML5 &…

vue实现视频上传

vue实现视频上传

Vue 实现视频上传的方法 使用原生 input 元素和 FormData 在 Vue 中可以通过原生 input 元素结合 FormData 实现视频上传功能。创建一个文件选择输入框,监听文件变化事…

vue实现视频滑入播放

vue实现视频滑入播放

实现视频滑入播放的步骤 监听滚动事件 在Vue组件中,通过mounted钩子添加滚动事件监听器,使用IntersectionObserverAPI检测视频元素是否进入视口。这种方法性能优于传统的滚动事…

vue怎么实现视频列表

vue怎么实现视频列表

实现视频列表的基本步骤 使用Vue.js实现视频列表需要结合组件化开发和数据绑定。通过v-for指令渲染列表,结合视频播放组件或HTML5的video标签展示内容。 数据准备与绑定 创建视频数据数组…