lookShipin2.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <template>
  2. <view class="video-container">
  3. <video
  4. id="myVideo"
  5. :src="videoUrl"
  6. controls
  7. @timeupdate="updateCurrentTime"
  8. @seeked="onVideoSeeked"
  9. ref="videoPlayer"
  10. ></video>
  11. <cover-view class="markers-container" @click="onMarkerClick">
  12. <block v-for="(marker, index) in markers" :key="index">
  13. <cover-view
  14. class="marker"
  15. :style="{ left: marker.position + 'px' }"
  16. :data-time="marker.time"
  17. >
  18. <!-- 标记点的内容,可以是时间、图标等 -->
  19. {{ marker.time.toFixed(2) }}s
  20. </cover-view>
  21. </block>
  22. </cover-view>
  23. </view>
  24. </template>
  25. <script>
  26. export default {
  27. data() {
  28. return {
  29. videoUrl: 'https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/2minute-demo.mp4', // 替换为你的视频URL
  30. currentTime: 0,
  31. markers: [
  32. // 初始标记点,可以根据需要动态添加
  33. // { time: 10, position: 0 }, // 示例标记点,时间单位为秒,位置根据视频宽度和视频时长计算
  34. ],
  35. }
  36. },
  37. onReady: function (res) {
  38. this.videoContext = uni.createVideoContext('myVideo')
  39. this.addMarker(10);
  40. },
  41. methods: {
  42. updateCurrentTime(event) {
  43. this.currentTime = event.detail.currentTime;
  44. // 这里可以添加逻辑来更新标记点的位置(如果标记点位置是动态计算的话)
  45. },
  46. onVideoSeeked(event) {
  47. // 视频跳转后的处理(如果需要的话)
  48. },
  49. addMarker(time) {
  50. // 计算标记点的位置
  51. const videoElement = this.$refs.videoPlayer;
  52. const videoDuration = videoElement.duration;
  53. const videoWidth = videoElement.$el.offsetWidth; // 获取视频元素的实际宽度
  54. const markerPosition = (time / videoDuration) * videoWidth;
  55. // 添加标记点到数组中
  56. this.markers.push({
  57. time,
  58. position: markerPosition,
  59. });
  60. // 如果需要,可以对markers数组进行排序
  61. },
  62. onMarkerClick(event) {
  63. // 获取点击的标记点的时间
  64. const target = event.target;
  65. const markerTime = parseFloat(target.dataset.time);
  66. // 跳转到对应的视频时间
  67. const videoElement = this.$refs.videoPlayer;
  68. console.log(videoElement)
  69. console.log(this.videoContext)
  70. this.videoContext.seek(markerTime);
  71. },
  72. }
  73. }
  74. </script>
  75. <style>
  76. .video-container {
  77. position: relative;
  78. width: 100%; /* 或者你想要的视频宽度 */
  79. }
  80. .markers-container {
  81. position: absolute;
  82. top: 0;
  83. left: 0;
  84. width: 100%;
  85. height: 100%;
  86. pointer-events: none; /* 防止标记容器干扰视频操作,但我们需要点击事件,所以稍后会在marker上设置pointer-events:auto */
  87. display: flex;
  88. align-items: center; /* 使标记点垂直居中 */
  89. }
  90. .marker {
  91. position: absolute;
  92. top: 10px; /* 或者你想要的标记点距视频顶部的距离 */
  93. background-color: rgba(255, 0, 0, 0.5);
  94. color: white;
  95. padding: 2px 5px;
  96. border-radius: 3px;
  97. font-size: 12px;
  98. white-space: nowrap;
  99. pointer-events: auto; /* 允许点击事件 */
  100. cursor: pointer; /* 鼠标悬停时显示手型光标 */
  101. }
  102. </style>