Merge pull request #19 from nikandlv/development

Duration and progress update with some minor adjustments
master
Nikan Dalvand 2019-07-25 23:49:24 +04:30 committed by GitHub
commit 98d32e187b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 5 deletions

View File

@ -1,13 +1,17 @@
<template> <template>
<section class="player"> <section class="player">
<img @load="onArtworkLoad" class="artwork" src="/static/demo/starboy.png" /> <img @load="onArtworkLoad" class="artwork" src="/static/demo/artwork.jpg" />
<div> <div>
<p class="title">Perfidia</p> <p class="title">Don't Let Me Down</p>
<p class="artist">Nat King Cole</p> <p class="artist">The Chainsmokers</p>
</div> </div>
<div class="visualizer"> <div class="visualizer">
<div id="visualizer"></div> <div id="visualizer"></div>
</div> </div>
<div class="duration">
<p class="progress active">00:00</p>
<p>{{duration}}</p>
</div>
<div class="control-buttons"> <div class="control-buttons">
<div class="play-pause-button" @click="togglePlay"> <div class="play-pause-button" @click="togglePlay">
<svg v-show="!playingStatus" xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 48 48"><path d="M24 4C12.95 4 4 12.95 4 24s8.95 20 20 20 20-8.95 20-20S35.05 4 24 4zm-4 29V15l12 9-12 9z"/></svg> <svg v-show="!playingStatus" xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 48 48"><path d="M24 4C12.95 4 4 12.95 4 24s8.95 20 20 20 20-8.95 20-20S35.05 4 24 4zm-4 29V15l12 9-12 9z"/></svg>
@ -20,9 +24,11 @@
<script> <script>
import WaveSurfer from 'wavesurfer.js' import WaveSurfer from 'wavesurfer.js'
import color from 'dominant-color' import color from 'dominant-color'
import { formatSeconds } from '../utility/DateTime'
export default { export default {
name: 'PlayerBar', name: 'PlayerBar',
mounted () { mounted () {
let progress = this.$el.getElementsByClassName('progress')[0]
this.wavesurfer = WaveSurfer.create({ this.wavesurfer = WaveSurfer.create({
container: '#visualizer', container: '#visualizer',
waveColor: '#c3c3c3', waveColor: '#c3c3c3',
@ -35,10 +41,17 @@ export default {
responsive: true responsive: true
}) })
this.wavesurfer.load('/static/demo/music.mp3') this.wavesurfer.load('/static/demo/music.mp3')
this.wavesurfer.on('ready', () => {
this.duration = formatSeconds(this.getDuration())
})
this.wavesurfer.on('audioprocess', (amount) => {
progress.innerHTML = formatSeconds(amount)
})
}, },
data () { data () {
return { return {
playingStatus: false playingStatus: false,
duration: '00:00'
} }
}, },
methods: { methods: {
@ -46,6 +59,7 @@ export default {
let img = this.$el.getElementsByTagName('img')[0] let img = this.$el.getElementsByTagName('img')[0]
color(img.src, (_, color) => { color(img.src, (_, color) => {
this.wavesurfer.setProgressColor(`#${color}`) this.wavesurfer.setProgressColor(`#${color}`)
this.wavesurfer.setCursorColor(`#${color}`)
}) })
}, },
play () { play () {
@ -70,11 +84,14 @@ export default {
this.wavesurfer.skip(offset) this.wavesurfer.skip(offset)
}, },
getVolume () { getVolume () {
this.wavesurfer.getVolume() return this.wavesurfer.getVolume()
}, },
setVolume (volume) { setVolume (volume) {
this.wavesurfer.setVolume(volume) this.wavesurfer.setVolume(volume)
}, },
getDuration () {
return this.wavesurfer.getDuration()
},
togglePlay () { togglePlay () {
this.wavesurfer.playPause() this.wavesurfer.playPause()
this.playingStatus = this.isPlaying() this.playingStatus = this.isPlaying()
@ -102,4 +119,5 @@ section.player
height: 64px height: 64px
wave wave
overflow-x: hidden !important overflow-x: hidden !important
cursor: e-resize
</style> </style>

View File

@ -0,0 +1,11 @@
export function formatSeconds (input) {
var secondNumber = parseInt(input, 10)
var hours = Math.floor(secondNumber / 3600)
var minutes = Math.floor(secondNumber / 60) % 60
var seconds = secondNumber % 60
return [hours, minutes, seconds]
.map(v => v < 10 ? '0' + v : v)
.filter((v, i) => v !== '00' || i > 0)
.join(':')
}