技术栈是Vue2、Element UI;
要实现的功能是:使用按钮实现视频的播放、停止、停止后继续播放、播放完成后重新播放功能
import { Button } from 'element-ui';
playAudio()
函数。Audio
API来控制音频的播放、暂停、继续播放和重新播放功能。可以通过new Audio('audio_file_url')
方法创建一个新的音频对象,通过设置相关的属性和调用对应的方法来控制音频的状态和行为。Audio
对象的currentTime
属性中即可。以下实例为关键代码,演示了如何在Vue Element UI中实现播放、暂停、继续播放和重新播放功能:
<template>
<div>
<el-button @click="playAudio">播放</el-button>
<el-button @click="pauseAudio">暂停</el-button>
<el-button @click="resumeAudio">继续播放</el-button>
<el-button @click="restartAudio">重新播放</el-button>
</div>
</template>
<script>
import { Button } from 'element-ui';
export default {
components: {
Button,
},
data() {
return {
audio: null, // 当前音频对象
isPlaying: false, // 音频是否正在播放
currentTime: 0, // 当前播放时间
};
},
methods: {
playAudio() {
if (!this.audio) {
this.audio = new Audio('audio_file_url');
this.audio.addEventListener('ended', () => {
// 播放完成后重新播放
this.currentTime = 0;
this.isPlaying = false;
this.audio.currentTime = 0;
});
}
this.audio.play();
this.isPlaying = true;
},
pauseAudio() {
if (this.audio) {
this.audio.pause();
this.isPlaying = false;
}
},
resumeAudio() {
if (this.audio && !this.isPlaying) {
this.audio.currentTime = this.currentTime;
this.audio.play();
this.isPlaying = true;
}
},
restartAudio() {
if (this.audio) {
this.audio.currentTime = 0;
this.audio.play();
this.isPlaying = true;
}
},
},
};
</script>
原文地址:https://blog.csdn.net/I_nur/article/details/130318196
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_19972.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!