Struggling to pass the value from checkMp3SizeAndDuration method function to data() {return { time: '' } }
Check out the code snippet below:
data () {
return {
time: ''
}
},
methods: {
checkMp3SizeAndDuration () {
const files = document.getElementById('file').files
const file = files[0]
const reader = new FileReader()
const audio = document.createElement('audio')
reader.onload = function (e) {
audio.src = e.target.result
let time = ''
audio.onloadedmetadata = () => {
const seconds = audio.duration
const duration = moment.duration(seconds, 'seconds')
const hours = duration.hours()
if (hours > 0) { time = `${hours}:` }
time = `${ duration.minutes() }:${duration.seconds()}`
console.log(time) <-- Example time log: 3:51
this.time = time // Not working here..
}
audio.addEventListener('onerror', function () {
alert('Cannot get duration of this file.')
}, false)
}
reader.readAsDataURL(file)
}
}
Take a look at the jsfiddle for more details: https://jsfiddle.net/xzktcrd2/