DEMO——音頻播放封裝

1.Player封裝

<!-- 封裝音頻播放器 -->
<template>
  <div class="play-container" @mouseenter="onEnterTd" @mouseleave="onLeaveTd">
    <svg viewBox="5 5 100 100" :class="isSvg?'svg':'svg-none'">
         <path d="M 5,55 a 55,55 0 1,1 0,1 z" id="circle" />
             <text>
                 <textPath xlink:href="#circle"> circular reasoning works because </textPath>
             </text>
    </svg>
    <el-image class="bg-img" :src="bgList[id]"></el-image> 
    <div class="play" @click="pause">
        <i class="el-icon-video-play" style="font-size:40px" v-show="!isPlay"></i>
        <i class="el-icon-video-pause" style="font-size:40px" v-show="isPlay"></i>
    </div>
  </div>
</template>
<script>
import play1 from "@/assets/play1.jpg"
import song1 from "@/assets/lucky.mp3"
import song2 from "@/assets/cricket.mp3"
import song3 from "@/assets/badluck.mp3"
import song4 from "@/assets/grasshopper.mp3"
import song5 from "@/assets/frog.mp3"
import song6 from "@/assets/ghost.mp3"
export default {
    props: {
      id: {
          type: Number,
          default: null
      },
    },
  data () {
    return {
        bgList:[play1,play1,play1,play1,play1,play1],
        isPlay:false,
        mp3List:[song1,song2,song3,song4,song5,song6],
        canplay:false,
        audio: ""阳藻,
        isSvg:false
    };
  },
  created(){
    this.audio = document.createElement("audio");
    this.audio.src = this.mp3List[this.id]
    let that = this
    this.audio.addEventListener(
      "canplay",
      function () {
        console.log("加載成功");
        that.canplay = true;
      },
      false
    ),
    this.audio.addEventListener(
          "ended",
          function () {
            that.audio.pause(); // 暫停
            that.isPlay = false;
          },
          false
    ) 
},
methods: {
    pause() {
        if (this.audio !== null && this.canplay) {
          this.loading = true;
          if (this.audio.paused) {
            this.audio.play(); // 播放
            this.isPlay = true;
          } else {
            this.audio.pause(); // 暫停
            this.isPlay = false;
            console.log("暫停被調(diào)用了");
          }
        } else {
            this.$message({
            showClose: true,
            message: "音樂還沒有加載成功呢!",
            type: "warning",
          });
        }
    },
    //鼠標(biāo)進(jìn)入的事件湿硝。
    onEnterTd(e) {
      this.isSvg = true
    },
    //鼠標(biāo)離開的事件蚕捉。
    onLeaveTd(e) {
      this.isSvg = false
    },
  }
}
</script>
<style lang='less' scoped>
.play-container{
    position: relative;
    width: 150px;
    height: 150px;
    
    .bg-img{
        position: absolute;
        left:0;
        top: 0;
        width: 150px;
        height: 150px;
        border-radius: 50%;
    }
   .play{
        position: absolute;
        left: 31%;
        top: 31%;
    }
}

.play-container path { fill: none; }
.play-container .svg { 
  display: block; 
  overflow: visible; 
  animation: myfirst 6s infinite linear;
}
.play-container .svg-none {
  display: none; 
  overflow: visible;
}
@keyframes myfirst
{
    from {transform:rotate(0deg);}
    to {transform:rotate(360deg);}
}
@-webkit-keyframes myfirst
{
    from {transform:rotate(0deg);}
    to {transform:rotate(360deg);}
}
</style>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末奏篙,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌秘通,老刑警劉巖为严,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異肺稀,居然都是意外死亡第股,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進(jìn)店門话原,熙熙樓的掌柜王于貴愁眉苦臉地迎上來夕吻,“玉大人,你說我怎么就攤上這事稿静∷蠊冢” “怎么了?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵改备,是天一觀的道長控漠。 經(jīng)常有香客問我,道長悬钳,這世上最難降的妖魔是什么盐捷? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮默勾,結(jié)果婚禮上碉渡,老公的妹妹穿的比我還像新娘。我一直安慰自己母剥,他們只是感情好滞诺,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著环疼,像睡著了一般习霹。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上炫隶,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天淋叶,我揣著相機(jī)與錄音,去河邊找鬼伪阶。 笑死煞檩,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的栅贴。 我是一名探鬼主播斟湃,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼檐薯!你這毒婦竟也來了桐早?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎哄酝,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體祷膳,經(jīng)...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡陶衅,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了直晨。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片搀军。...
    茶點(diǎn)故事閱讀 40,137評論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖勇皇,靈堂內(nèi)的尸體忽然破棺而出罩句,到底是詐尸還是另有隱情,我是刑警寧澤敛摘,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布门烂,位于F島的核電站,受9級特大地震影響兄淫,放射性物質(zhì)發(fā)生泄漏屯远。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一捕虽、第九天 我趴在偏房一處隱蔽的房頂上張望慨丐。 院中可真熱鬧,春花似錦泄私、人聲如沸房揭。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽捅暴。三九已至,卻和暖如春斩松,著一層夾襖步出監(jiān)牢的瞬間伶唯,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工惧盹, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留乳幸,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓钧椰,卻偏偏與公主長得像粹断,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子嫡霞,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,086評論 2 355

推薦閱讀更多精彩內(nèi)容