Leverage new transcode endpoint in player

This commit is contained in:
Eliot Berriot 2018-02-18 23:50:08 +01:00
parent 1cfdf31e00
commit d15fefe730
No known key found for this signature in database
GPG Key ID: DD6965E2476E5C27
2 changed files with 32 additions and 11 deletions

View File

@ -0,0 +1,10 @@
export default {
formats: [
// 'audio/ogg',
'audio/mpeg'
],
formatsMap: {
'audio/ogg': 'ogg',
'audio/mpeg': 'mp3'
}
}

View File

@ -1,21 +1,20 @@
<template> <template>
<audio <audio
ref="audio" ref="audio"
:src="url"
@error="errored" @error="errored"
@progress="updateLoad"
@loadeddata="loaded" @loadeddata="loaded"
@durationchange="updateDuration"
@timeupdate="updateProgress" @timeupdate="updateProgress"
@ended="ended" @ended="ended"
preload> preload>
<source v-for="src in srcs" :src="src.url" :type="src.type">
</audio> </audio>
</template> </template>
<script> <script>
import {mapState} from 'vuex' import {mapState} from 'vuex'
import backend from '@/audio/backend'
import url from '@/utils/url' import url from '@/utils/url'
import formats from '@/audio/formats'
// import logger from '@/logging' // import logger from '@/logging'
@ -34,31 +33,43 @@ export default {
volume: state => state.player.volume, volume: state => state.player.volume,
looping: state => state.player.looping looping: state => state.player.looping
}), }),
url: function () { srcs: function () {
let file = this.track.files[0] let file = this.track.files[0]
if (!file) { if (!file) {
this.$store.dispatch('player/trackErrored') this.$store.dispatch('player/trackErrored')
return null return []
} }
let path = backend.absoluteUrl(file.path) let sources = [
{type: file.mimetype, url: file.path}
]
formats.formats.forEach(f => {
if (f !== file.mimetype) {
let format = formats.formatsMap[f]
let url = `/api/v1/trackfiles/transcode/?track_file=${file.id}&to=${format}`
sources.push({type: f, url: url})
}
})
if (this.$store.state.auth.authenticated) { if (this.$store.state.auth.authenticated) {
// we need to send the token directly in url // we need to send the token directly in url
// so authentication can be checked by the backend // so authentication can be checked by the backend
// because for audio files we cannot use the regular Authentication // because for audio files we cannot use the regular Authentication
// header // header
path = url.updateQueryString(path, 'jwt', this.$store.state.auth.token) sources.forEach(e => {
e.url = url.updateQueryString(e.url, 'jwt', this.$store.state.auth.token)
})
} }
return path return sources
} }
}, },
methods: { methods: {
errored: function () { errored: function () {
this.$store.dispatch('player/trackErrored') this.$store.dispatch('player/trackErrored')
}, },
updateLoad: function () { updateDuration: function (e) {
this.$store.commit('player/duration', this.$refs.audio.duration)
}, },
loaded: function () { loaded: function () {
this.$refs.audio.volume = this.volume
if (this.isCurrent) { if (this.isCurrent) {
this.$store.commit('player/duration', this.$refs.audio.duration) this.$store.commit('player/duration', this.$refs.audio.duration)
if (this.startTime) { if (this.startTime) {