基于vue.js的音樂播放器

興趣乃學習的動力冈在,想自己動手寫個音樂播放器瓷产,查了網(wǎng)上一些博客站玄,最終東拼西湊寫了一個。網(wǎng)上有網(wǎng)易云的接口濒旦,這里不多贅述了株旷。

整體項目代碼挺多的,在這里就只挑選音樂播放和切換音樂來記錄了。
https://github.com/gloryin2016/manageplatform

把搜索后獲取的音樂列表晾剖,將其存入store锉矢;通過index來切換不同的歌曲。

播放器

HTML的audio屬性參考http://www.reibang.com/p/1fe701c9179f

//進度條直接用的element的slider齿尽,修改樣式顏色即可
<div class="ap-play-track" ref="track">
            <el-slider
              class="ap-play-track"
              @change="SetCurrentTime"
              v-model="processWidth"
            ></el-slider>
<div>
<audio :src="songInfo.url" id="audio"></audio>

data

data() {
    return {
      playing: false,
      index: 0, // 當前播放歌曲在列表中的下標
      currentTime: "00:00", // 當前播放時間
      totalTime: "00:00", // 總播放時間
      bufferedScaleX: 0, // 緩存進度
      processWidth: 0, //播放百分比
      progressScaleX: 0, // 播放進度
      thumbTranslateX: 0, // 進度條滑塊位置
      silderBoxX: 0, //進度條滑塊位置-->采用element后的
      volume: 50, // 音量
      error: "", // 報錯內(nèi)容
      playType: '1', // 播放類型:1-列表循環(huán)沽损,2-隨機播放,3-單曲循環(huán)
//播放列表
      songList: [
        {
          albumId: 75612550,
          albumTitle: "辭.九門回憶",
          artistsName: "解憂草",
          cover: "",
          finalTime: "04:00",
          id: 1347524822,
          index: 6,
          mvId: 0,
          name: "辭.九門回憶",
          sort: "07",
          url: "https://music.163.com/song/media/outer/url?id=1347524822.mp3",
        },
        {
          albumId: 85511857,
          albumTitle: "謫仙",
          artistsName: "伊格賽聽",
          cover: "",
          finalTime: "02:0",
          id: 1421256202,
          index: 0,
          mvId: 0,
          name: "謫仙",
          sort: "01",
          url: "https://music.163.com/song/media/outer/url?id=1421256202.mp3",
        },
        {
          albumId: 35172219,
          albumTitle: "君の名は - 黃昏之時",
          artistsName: "Frank_Jiang",
          cover:
            "https://p1.music.126.net/YppJiMHyrLc7tDkj6jUttg==/109951162858188597.jpg",
          finalTime: "03:00",
          id: 459116892,
          index: 5,
          mvId: 0,
          name: "黃昏之時(FRANKOWO Bootleg)",
          sort: "06",
          url: "https://music.163.com/song/media/outer/url?id=459116892.mp3",
        },
      ],
//當前播放歌曲
      songInfo: {
        albumId: 75612550,
        albumTitle: "辭.九門回憶",
        artistsName: "解憂草/冰幽",
        cover: "",
        finalTime: "04:00",
        id: 1347524822,
        index: 6,
        mvId: 0,
        name: "辭.九門回憶",
        sort: "07",
        url: "https://music.163.com/song/media/outer/url?id=1347524822.mp3",
      },
      lyricsObjArr: [],//歌詞
      lyricIndex: 0,歌詞索引
    };
  },

JS

  mounted() {
    audio = document.getElementById("audio");
    this.Init();
  },
  method: {
    Init(){
       this.songInfo = this.songList[0];
       this.audioInit();
    }
    //播放與暫停
    play() {
      if (this.playing) {
        // 播放中,點擊則為暫停
        this.playing = false;
        audio.pause();
      } else {
        // 暫停中,點擊則為播放
        this.playing = true;
        audio.play();
      }
    },
    audioInit() {
      let _this = this;
      let progressL = this.$refs.track.offsetWidth; // 進度條總長
      // 播放位置改變時觸發(fā)[注意:播放和調(diào)整指示定位時都會觸發(fā)](主要事件)
      audio.addEventListener("timeupdate", () => {
        // 當前播放時間
        _this.currentTime = _this.timeToString(audio.currentTime);
        let compareTime = audio.currentTime;
        for (let i = 0; i < _this.lyricsObjArr.length; i++) {
          if (compareTime > parseInt(_this.lyricsObjArr[i].time)) {
            const index = _this.$refs.lyric[i].dataset.index;
            if (i === parseInt(index)) {
              _this.lyricIndex = i;
            }
          }
        }
        // 總播放時間
        _this.totalTime = _this.timeToString(audio.duration);

        // 當前播放進度百分比
        let precent = audio.currentTime / audio.duration || 0;

        // 當前播放進度
        _this.progressScaleX = precent.toFixed(3);
        _this.processWidth = precent.toFixed(2) * 100;

        // 當前緩存進度
        // 已緩存時間
        let buffered = audio.buffered.length
          ? audio.buffered.end(audio.buffered.length - 1)
          : 0;
        _this.bufferedScaleX = (buffered / audio.duration).toFixed(3);

        // 當前進度按鈕位置
        _this.thumbTranslateX = (precent * progressL).toFixed(3);
      });

      // 音頻或視頻能夠不停頓地一直播放
      audio.addEventListener("canplaythrough", () => {
        console.log("canplaythrough");
      });

      // 音頻或視頻的時長已改變
      audio.addEventListener("durationchange", () => {
        console.log("durationchange");
        _this.totalTime = _this.timeToString(audio.duration);
      });

      // 在音頻或視頻終止加載時觸發(fā)循头,包括終止當前播放(未加載完)進行下一首播放時也會觸發(fā)
      audio.addEventListener("abort", () => {
        console.log("abort");
      });

      // 在音頻或視頻加載發(fā)生錯誤時觸發(fā)
      audio.addEventListener("error", () => {
        console.log("error");
        console.log("-----networkState---------", audio.networkState);
        console.log("-----readyState---------", audio.readyState);
        switch (audio.networkState) {
          case "0":
            _this.error = "尚未初始化";
            break;
          case "1":
            _this.error = "正在下載數(shù)據(jù)";
            break;
          case "3":
            _this.error = "未找到資源";
            break;
        }
        audio.readyState == "0" && (_this.error = "音頻地址錯誤");

        setTimeout(() => {
          _this.error = "";
        }, 3000);
      });

      // 播放結(jié)束
      audio.addEventListener(
        "ended",
        () => {
          console.log("ended");
          switch (parseInt(_this.playType)) {
            case 1: // 列表循環(huán)
              _this.index =
                _this.index + 1 >= _this.songList.length ? 0 : _this.index + 1;
              break;
            case 2: // 隨機播放
              _this.index = Math.floor(Math.random() * _this.songList.length);
              break;
            case 3: // 單曲循環(huán)
              break;
          }
          _this.songInfo = _this.songList[_this.index];
          this.$store.dispatch("setSongIndex", _this.index); //在vuex存入當前播放歌曲的index
          this.GetLyric(_this.songInfo.id);//獲取歌詞的接口
          _this.thumbSlide = true;
          setTimeout(() => {
            audio.play();
          }, 100);
          // 解決因為transition的回彈bug
          setTimeout(() => {
            _this.thumbSlide = false;
          }, 1000);
        },
        true
      );
    },
  },
    // 秒值轉(zhuǎn)字符串
    timeToString(param) {
      param = parseInt(param);
      let hh = "",
        mm = "",
        ss = "";
      if (param >= 0 && param < 60) {
        param < 10 ? (ss = "0" + param) : (ss = param);
        return "00:" + ss;
      } else if (param >= 60 && param < 3600) {
        mm = parseInt(param / 60);
        mm < 10 ? (mm = "0" + mm) : mm;
        param - parseInt(mm * 60) < 10
          ? (ss = "0" + String(param - parseInt(mm * 60)))
          : (ss = param - parseInt(mm * 60));
        return mm + ":" + ss;
      }
    },
//上下首封裝
    skipFn(type) {
      switch (parseInt(this.playType)) {
        case 2: // 隨機播放
          this.index = Math.floor(Math.random() * this.songList.length);
          break;
        default:
          if (type == "skipBack") {
            this.index - 1 >= 0 ? this.index-- : 0;
          } else {
            this.index =
              this.index + 1 >= this.songList.length
                ? this.songList.length - 1
                : this.index + 1;
          }
          break;
      }
      this.songInfo = this.songList[this.index];
      this.$store.dispatch("setSongIndex", this.index);
      this.playing = true;
      setTimeout(() => {
        this.totalTime = "00:00";
        audio.play();
      }, 100);
    },
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末绵估,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子卡骂,更是在濱河造成了極大的恐慌国裳,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件全跨,死亡現(xiàn)場離奇詭異缝左,居然都是意外死亡,警方通過查閱死者的電腦和手機螟蒸,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進店門盒使,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人七嫌,你說我怎么就攤上這事少办。” “怎么了诵原?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵英妓,是天一觀的道長。 經(jīng)常有香客問我绍赛,道長蔓纠,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任吗蚌,我火速辦了婚禮腿倚,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘蚯妇。我一直安慰自己敷燎,他們只是感情好,可當我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布箩言。 她就那樣靜靜地躺著硬贯,像睡著了一般。 火紅的嫁衣襯著肌膚如雪陨收。 梳的紋絲不亂的頭發(fā)上饭豹,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天,我揣著相機與錄音,去河邊找鬼拄衰。 笑死它褪,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的肾砂。 我是一名探鬼主播列赎,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼镐确!你這毒婦竟也來了包吝?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤源葫,失蹤者是張志新(化名)和其女友劉穎诗越,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體息堂,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡嚷狞,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了荣堰。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片床未。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖振坚,靈堂內(nèi)的尸體忽然破棺而出薇搁,到底是詐尸還是另有隱情,我是刑警寧澤渡八,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布啃洋,位于F島的核電站,受9級特大地震影響屎鳍,放射性物質(zhì)發(fā)生泄漏宏娄。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一逮壁、第九天 我趴在偏房一處隱蔽的房頂上張望孵坚。 院中可真熱鬧,春花似錦窥淆、人聲如沸卖宠。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至秉氧,卻和暖如春眷昆,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工亚斋, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留作媚,地道東北人。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓帅刊,卻偏偏與公主長得像纸泡,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子赖瞒,可洞房花燭夜當晚...
    茶點故事閱讀 44,592評論 2 353