Fetch upload data from tracks when missing to avoid unecessary transcoding

This commit is contained in:
Eliot Berriot 2019-01-15 15:45:53 +01:00
parent ebbf2b06a6
commit 11728bbbc4
No known key found for this signature in database
GPG Key ID: DD6965E2476E5C27
1 changed files with 55 additions and 39 deletions

View File

@ -7,6 +7,7 @@ import {mapState} from 'vuex'
import _ from '@/lodash' import _ from '@/lodash'
import url from '@/utils/url' import url from '@/utils/url'
import {Howl} from 'howler' import {Howl} from 'howler'
import axios from 'axios'
// import logger from '@/logging' // import logger from '@/logging'
@ -19,6 +20,7 @@ export default {
}, },
data () { data () {
return { return {
trackData: this.track,
sourceErrors: 0, sourceErrors: 0,
sound: null, sound: null,
isUpdatingTime: false, isUpdatingTime: false,
@ -27,42 +29,16 @@ export default {
}, },
mounted () { mounted () {
let self = this let self = this
this.sound = new Howl({ if (!this.trackData.uploads.length || this.trackData.uploads.length === 0) {
src: this.srcs.map((s) => { return s.url }), // we don't have upload informations for this track, we need to fetch it
format: this.srcs.map((s) => { return s.type }), axios.get(`tracks/${this.trackData.id}/`).then((response) => {
autoplay: false, self.trackData = response.data
loop: false, self.setupSound()
html5: true, }, error => {
preload: true, self.$emit('errored', {})
volume: this.volume, })
onend: function () { } else {
self.ended() this.setupSound()
},
onunlock: function () {
if (this.$store.state.player.playing) {
self.sound.play()
}
},
onload: function () {
self.$store.commit('player/isLoadingAudio', false)
self.$store.commit('player/resetErrorCount')
self.$store.commit('player/errored', false)
self.$store.commit('player/duration', self.sound.duration())
let node = self.sound._sounds[0]._node;
node.addEventListener('progress', () => {
self.updateBuffer(node)
})
},
onloaderror: function (sound, error) {
console.log('Error while playing:', sound, error)
self.$emit('errored', {sound, error})
},
})
if (this.autoplay) {
self.$store.commit('player/isLoadingAudio', true)
this.sound.play()
this.$store.commit('player/playing', true)
this.observeProgress(true)
} }
}, },
destroyed () { destroyed () {
@ -78,7 +54,7 @@ export default {
looping: state => state.player.looping looping: state => state.player.looping
}), }),
srcs: function () { srcs: function () {
let sources = this.track.uploads.map(u => { let sources = this.trackData.uploads.map(u => {
return { return {
type: u.extension, type: u.extension,
url: this.$store.getters['instance/absoluteUrl'](u.listen_url), url: this.$store.getters['instance/absoluteUrl'](u.listen_url),
@ -90,7 +66,7 @@ export default {
sources.push({ sources.push({
type: 'mp3', type: 'mp3',
url: url.updateQueryString( url: url.updateQueryString(
this.$store.getters['instance/absoluteUrl'](this.track.listen_url), this.$store.getters['instance/absoluteUrl'](this.trackData.listen_url),
'to', 'to',
'mp3' 'mp3'
) )
@ -111,6 +87,46 @@ export default {
} }
}, },
methods: { methods: {
setupSound () {
let self = this
this.sound = new Howl({
src: this.srcs.map((s) => { return s.url }),
format: this.srcs.map((s) => { return s.type }),
autoplay: false,
loop: false,
html5: true,
preload: true,
volume: this.volume,
onend: function () {
self.ended()
},
onunlock: function () {
if (this.$store.state.player.playing) {
self.sound.play()
}
},
onload: function () {
self.$store.commit('player/isLoadingAudio', false)
self.$store.commit('player/resetErrorCount')
self.$store.commit('player/errored', false)
self.$store.commit('player/duration', self.sound.duration())
let node = self.sound._sounds[0]._node;
node.addEventListener('progress', () => {
self.updateBuffer(node)
})
},
onloaderror: function (sound, error) {
console.log('Error while playing:', sound, error)
self.$emit('errored', {sound, error})
},
})
if (this.autoplay) {
self.$store.commit('player/isLoadingAudio', true)
this.sound.play()
this.$store.commit('player/playing', true)
this.observeProgress(true)
}
},
updateBuffer (node) { updateBuffer (node) {
// from https://github.com/goldfire/howler.js/issues/752#issuecomment-372083163 // from https://github.com/goldfire/howler.js/issues/752#issuecomment-372083163
let range = 0; let range = 0;
@ -178,7 +194,7 @@ export default {
this.sound.seek(0) this.sound.seek(0)
this.sound.play() this.sound.play()
} else { } else {
this.$store.dispatch('player/trackEnded', this.track) this.$store.dispatch('player/trackEnded', this.trackData)
} }
} }
}, },