1.audio標簽
1 <audio @play="ready" @error="error" ref="audio" :src="currentSong.url"></audio>
2.中間圓cd和播放/暫停按鈕動畫
1 playIcon(){ 2 return this.playing ? ' iconfont icon-pause-' : 'iconfont icon-bofang1' 3 }, 4 cdCls(){ 5 return this.playing ? 'play' :'play pause' 6 }
3.通過mutations來設置播放/暫停----上一首/下一首-----min/normal播放
1 methods:{ 2 ...mapMutations({ 3 setFullScreen:'SET_FULL_SCREEN', 4 setPlayingState:'SET_PLAYING_STATE', 5 setCurrentIndex:'SET_CURRENT_INDEX', 6 }), 7 mini(){//當電機及此按鈕的時候,將全屏顯示轉換為“迷你播放” 8 this.setFullScreen(false) 9 },
4.監聽currentsong的變化,當currentsong發生變化的時候,讓歌曲播放;
1 currentSong(){//一點擊歌曲進來的時候是播放狀態 2 this.$nextTick(()=>{ 3 this.$refs.audio.play() 4 }) 5 },
5.監聽playing的播放狀態
1 playing(newPlaying){//監聽是否在播放---切換播放暫停狀態 2 console.log(newPlaying) 3 const audio = this.$refs.audio 4 newPlaying ? audio.play() : audio.pause() 5 }
6.上一曲/下一曲切換:
1 prev(){//點擊上一首/下一首切換其實就是切換歌曲的播放索引,讓其+1/-1;audio由一個ready屬性,來確認是否准備好播放,來阻止用戶的連續多次點擊; 2 if (!this.songReady) { 3 return 4 } 5 let index = this.currentIndex -1 6 if(index ===1){ 7 index = this.playlist.length -1 8 } 9 this.setCurrentIndex(index) 10 if(!this.playing){ 11 this.togglePlaying() 12 } 13 }, 14 next(){ 15 if (!this.songReady) { 16 return 17 } 18 let index = this.currentIndex + 1 19 if (index === this.playlist.length){ 20 index = 0 21 } 22 this.setCurrentIndex(index) 23 if(!this.playing){//當暫停時候點擊播放的情況 24 this.togglePlaying() 25 } 26 }, 27 ready(){ 28 this.songReady = true 29 },