[番外] 来个音乐拨放器 Play! (续)

Data

data 起手式,记得要回传物件

data () {
    return {}
}

data 中大概会有这些内容

  data () {
    return {
      songList : [
        {
          name: 'Oceans',
          singer:'Seafret ',
          src: require('./assets/xxxx.mp3')
        },
         {
          name: 'No song without you',
          singer:'HONNE',
          src: require('./assets/xxxx.mp3')
        },
        {
          name: '我是第三首',
          singer:'我是第三首',
          src: require('./assets/xxxx.mp3')
        },

      ],
      index: 0, // 目前拨放第几首
      current: {}, // 目前拨放内容
      isPlaying: false, // 目前是否正在拨放
      isSingleLoop: false, // * 是否单首循环
      isRandomPlay: false, // * 是否随机拨放
      player: new Audio() 
    }
  },

参考 APP 音乐拨放器,还可以增加单首循环或是随机拨放的选项


Methods

play

用来控制拨放/暂停

ex:

play () {
    this.isPlaying? this.player.pause(): this.player.play();
    this.isPlaying = !this.isPlaying;
}
<button class="btn" @click="play">{{isPlaying? '暂停': '播放'}}</button>

changeSong

切换上下首,传入+1-1
ex:

 changeSong (value) {
    this.index = this.index + value;
    this.checkValidIndex();
    this.current = this.songList[this.index];
    this.player.src = this.current.src;
},
<button class="btn" @click="changeSong(-1)">上一首</button>

其他

在变更 index 後,要记得检查 index 是否有效

ex:

if (this.index > this.songList.length -1) {
    this.index = 0;
}
if (this.index < 0) {
    this.index = this.songList.length -1;
}

监听音乐 ended 事件

ex: 播完时,自动拨放下一首

this.player.addEventListener('ended', function () {
    this.index++;
    if (this.index > this.songList.length - 1) {
      this.index = 0;
    }

    this.current = this.songList[this.index];
    this.play(this.current);
}.bind(this));

每日一句:
有礼拜天晚上不忧郁的办法吗 /images/emoticon/emoticon02.gif


<<:  【Day18】Uart_TX 的实现

>>:  [Day19]虚拟机环境安装

LeetCode解题 Day19

115. Distinct Subsequences https://leetcode.com/pr...

Day5 参加职训(机器学习与资料分析工程师培训班),Python程序设计

上午: Python程序设计 初步介绍Python,此次课程设计为完全没学过该语言的情况,因此老师从...

Proxmox VE 群组管理与双因素认证

上一章我们介绍的帐号建立及权限指派的功能,但是当帐号跟客体机越来越多的时候,每次采用一对一的方式指...

.Net Core Web Api_笔记08_HTTP资源操作模式PATCH

在HTTP请求中 PUT 跟 PATCH 都代表更新 然後他们之间比较主要的差异在於 PUT 用在更...

Day7 if else实作

今天我们会实作两支程序,第一支程序要做让使用者输入两个数,若这两个数相起来是100,则显示a,是20...