每周一组件之- MP3组件的自我管理

<template>

<div class="player">

<audio

class="hidden"

ref="audio"

@pause="pause"

@play="play"

@timeupdate="updateTime"

@ended="ended"

:src="src"

controls="controls"

controlslist="nodownload"></audio>

<div @click="palyOrPause" class="player-circle" >

<div

class="horn"

:class="[hornStatusClassName]"

></div>

</div>

</div>

</template>

 

<script>

// 组件

let MP3Instance = null;

export default {

name: 'Player',

props: {

src: {

type: String,

default: 'https://jfile.koolearn.com/upload/video/1606547540041.mp3'

}

},

components: {

},

data() {

return {

hornStatusClassName: 'horn1', // 播放时候的css className

isPlaying: false // 记录播放的状态

}

},

methods: {

palyOrPause() {

if (this.isPlaying) {

this.pause()

} else {

this.play()

}

},

pause() {

this.$refs.audio && this.$refs.audio.pause();

this.isPlaying = false;

},

updateTime(e) {

// 时间更新

// this.rate = this.currentTime * 100 / this.durationTime

// 如果已经播放完毕,则标志 hasDone下次点击播放的时候直接不让点击

// this.currentTime = e.target.currentTime || 0; // 获取audio当前播放时间

},

play() {

if (MP3Instance && this != MP3Instance && MP3Instance.$data.isPlaying) {

MP3Instance.$refs.audio.pause();

MP3Instance.$data.isPlaying = false;

}

this.$refs.audio.play();

this.isPlaying = true;

MP3Instance = this;

},

ended() {

this.pause && this.pause();

clearInterval(this.time);

this.time = null;

},

_changeHornStatusClassName() {

let i = 1;

this.time = setInterval(() => {

i++;

if (i == 4) {

i = 1;

}

this.hornStatusClassName = `horn${i}`

}, 500);

}

},

computed: {

},

watch: {

isPlaying(val) {

const _this = this;

if (val) {

_this._changeHornStatusClassName();

} else {

clearInterval(this.time);

this.hornStatusClassName = 'hone1';

}

}

},

mounted() {

 

}

}

</script>

 

<!-- Add "scoped" attribute to limit CSS to this component only -->

<style scoped lang="scss">

.player-circle{

width: 50px;

height: 23px;

line-height: 23px;

border-radius: 13px;

border:1px solid #b0c0c8;

display: inline-block;

display: flex;

justify-content: center;

align-items: center;

cursor: pointer;

&:hover{

background-color: rgb(242, 249, 247)

}

.horn{

width: 14px;

height: 14px;

background:url("./img/laba1.png") center center no-repeat;

background-size: 100% 100%;

}

.horn1{

background:url("./img/laba1.png") center center no-repeat;

background-size: 100% 100%;

}

.horn2{

background:url("./img/laba2.png") center center no-repeat;

background-size: 100% 100%;

}

.horn3{

background:url("./img/laba3.png") center center no-repeat;

background-size: 100% 100%;

}

&:hover{

.horn{

background:url("./img/laba3.png") center center no-repeat;

background-size: 100% 100%;

}

}

}

.hidden{

display: none;

}

</style>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值