feat(视频播放器): 添加获取播放器实例方法和播放状态参数传递
为VideoPlayer组件添加getPlayer方法暴露播放器实例 在play/pause事件中传递payload参数 在StudyDetailPage中记录视频时长和播放位置
Showing
2 changed files
with
31 additions
and
5 deletions
| ... | @@ -113,10 +113,10 @@ const handleMounted = (payload) => { | ... | @@ -113,10 +113,10 @@ const handleMounted = (payload) => { |
| 113 | }; | 113 | }; |
| 114 | 114 | ||
| 115 | const handlePlay = (payload) => { | 115 | const handlePlay = (payload) => { |
| 116 | - emit("onPlay") | 116 | + emit("onPlay", payload) |
| 117 | }; | 117 | }; |
| 118 | const handlePause = (payload) => { | 118 | const handlePause = (payload) => { |
| 119 | - emit("onPause") | 119 | + emit("onPause", payload) |
| 120 | } | 120 | } |
| 121 | 121 | ||
| 122 | onBeforeUnmount(() => { | 122 | onBeforeUnmount(() => { |
| ... | @@ -138,6 +138,9 @@ defineExpose({ | ... | @@ -138,6 +138,9 @@ defineExpose({ |
| 138 | emit('onPlay', player.value); | 138 | emit('onPlay', player.value); |
| 139 | } | 139 | } |
| 140 | }, | 140 | }, |
| 141 | + getPlayer() { | ||
| 142 | + return player.value; | ||
| 143 | + } | ||
| 141 | }); | 144 | }); |
| 142 | </script> | 145 | </script> |
| 143 | 146 | ... | ... |
| ... | @@ -29,7 +29,7 @@ | ... | @@ -29,7 +29,7 @@ |
| 29 | <div v-if="course.course_type === 'audio'" class="w-full relative" | 29 | <div v-if="course.course_type === 'audio'" class="w-full relative" |
| 30 | style="border-bottom: 1px solid #F3F4F6;"> | 30 | style="border-bottom: 1px solid #F3F4F6;"> |
| 31 | <!-- 音频播放器 --> | 31 | <!-- 音频播放器 --> |
| 32 | - <AudioPlayer v-if="audioList.length" :songs="audioList" @play="onAudioPlay" @pause="onAudioPause" /> | 32 | + <AudioPlayer ref="audioPlayerRef" v-if="audioList.length" :songs="audioList" @play="onAudioPlay" @pause="onAudioPause" /> |
| 33 | </div> | 33 | </div> |
| 34 | <!-- 图片列表展示区域 --> | 34 | <!-- 图片列表展示区域 --> |
| 35 | <div v-if="course.course_type === 'image'" class="w-full relative"> | 35 | <div v-if="course.course_type === 'image'" class="w-full relative"> |
| ... | @@ -266,6 +266,7 @@ const newComment = ref(''); | ... | @@ -266,6 +266,7 @@ const newComment = ref(''); |
| 266 | const showCatalog = ref(false); | 266 | const showCatalog = ref(false); |
| 267 | const isPlaying = ref(false); | 267 | const isPlaying = ref(false); |
| 268 | const videoPlayerRef = ref(null); | 268 | const videoPlayerRef = ref(null); |
| 269 | +const audioPlayerRef = ref(null); | ||
| 269 | const showCommentPopup = ref(false); | 270 | const showCommentPopup = ref(false); |
| 270 | const popupComment = ref(''); | 271 | const popupComment = ref(''); |
| 271 | 272 | ||
| ... | @@ -288,13 +289,13 @@ const startPlay = async () => { | ... | @@ -288,13 +289,13 @@ const startPlay = async () => { |
| 288 | }; | 289 | }; |
| 289 | 290 | ||
| 290 | // 处理视频播放状态 | 291 | // 处理视频播放状态 |
| 291 | -const handleVideoPlay = () => { | 292 | +const handleVideoPlay = (video) => { |
| 292 | isPlaying.value = true; | 293 | isPlaying.value = true; |
| 293 | // 学习时长埋点开始 | 294 | // 学习时长埋点开始 |
| 294 | startAction(); | 295 | startAction(); |
| 295 | }; | 296 | }; |
| 296 | 297 | ||
| 297 | -const handleVideoPause = () => { | 298 | +const handleVideoPause = (video) => { |
| 298 | // 保持视频播放器可见,只在初始状态显示封面 | 299 | // 保持视频播放器可见,只在初始状态显示封面 |
| 299 | // 学习时长埋点结束 | 300 | // 学习时长埋点结束 |
| 300 | endAction(); | 301 | endAction(); |
| ... | @@ -704,6 +705,10 @@ const onAudioPause = (audio) => { | ... | @@ -704,6 +705,10 @@ const onAudioPause = (audio) => { |
| 704 | endAction(); | 705 | endAction(); |
| 705 | } | 706 | } |
| 706 | 707 | ||
| 708 | +// 记录视频时长和当前播放位置的变量 | ||
| 709 | +const videoDuration = ref(0); | ||
| 710 | +const currentPosition = ref(0); | ||
| 711 | + | ||
| 707 | /** | 712 | /** |
| 708 | * 开始操作 | 713 | * 开始操作 |
| 709 | * @param action | 714 | * @param action |
| ... | @@ -715,6 +720,11 @@ const startAction = (action, item) => { | ... | @@ -715,6 +720,11 @@ const startAction = (action, item) => { |
| 715 | clearInterval(window.actionTimer); | 720 | clearInterval(window.actionTimer); |
| 716 | } | 721 | } |
| 717 | 722 | ||
| 723 | + // 获取视频总时长(如果是视频播放) | ||
| 724 | + if (videoPlayerRef.value && videoPlayerRef.value.getPlayer()) { | ||
| 725 | + videoDuration.value = videoPlayerRef.value.getPlayer().duration(); | ||
| 726 | + } | ||
| 727 | + | ||
| 718 | // 生成唯一标识符 | 728 | // 生成唯一标识符 |
| 719 | let uuid = uuidv4(); | 729 | let uuid = uuidv4(); |
| 720 | console.warn('开始操作', uuid); | 730 | console.warn('开始操作', uuid); |
| ... | @@ -722,6 +732,13 @@ const startAction = (action, item) => { | ... | @@ -722,6 +732,13 @@ const startAction = (action, item) => { |
| 722 | // 设置定时器,持续执行操作 | 732 | // 设置定时器,持续执行操作 |
| 723 | window.actionTimer = setInterval(() => { | 733 | window.actionTimer = setInterval(() => { |
| 724 | console.warn('持续操作中', uuid); | 734 | console.warn('持续操作中', uuid); |
| 735 | + | ||
| 736 | + // 更新当前播放位置(如果是视频播放) | ||
| 737 | + if (videoPlayerRef.value && videoPlayerRef.value.getPlayer()) { | ||
| 738 | + currentPosition.value = videoPlayerRef.value.getPlayer().currentTime(); | ||
| 739 | + console.log('视频总时长:', videoDuration.value, '当前位置:', currentPosition.value); | ||
| 740 | + } | ||
| 741 | + | ||
| 725 | // 这里可以添加需要持续执行的具体操作 | 742 | // 这里可以添加需要持续执行的具体操作 |
| 726 | }, 1000); // 每秒执行一次,可以根据需求调整时间间隔 | 743 | }, 1000); // 每秒执行一次,可以根据需求调整时间间隔 |
| 727 | } | 744 | } |
| ... | @@ -732,6 +749,12 @@ const startAction = (action, item) => { | ... | @@ -732,6 +749,12 @@ const startAction = (action, item) => { |
| 732 | * @param item | 749 | * @param item |
| 733 | */ | 750 | */ |
| 734 | const endAction = (action, item) => { | 751 | const endAction = (action, item) => { |
| 752 | + // 在结束前记录最后的播放位置 | ||
| 753 | + if (videoPlayerRef.value && videoPlayerRef.value.player) { | ||
| 754 | + window.currentPosition = videoPlayerRef.value.player.currentTime(); | ||
| 755 | + console.log('结束时 - 视频总时长:', window.videoDuration, '最终位置:', window.currentPosition); | ||
| 756 | + } | ||
| 757 | + | ||
| 735 | // 清除定时器,停止执行startAction | 758 | // 清除定时器,停止执行startAction |
| 736 | if (window.actionTimer) { | 759 | if (window.actionTimer) { |
| 737 | clearInterval(window.actionTimer); | 760 | clearInterval(window.actionTimer); | ... | ... |
-
Please register or login to post a comment