Skip to content
Snippets Groups Projects
Verified Commit 5d2d2f50 authored by Eliot Berriot's avatar Eliot Berriot
Browse files

Fix #925: fallback to next available format when browser fails to decode audio

parent d44abf6e
Branches
No related tags found
No related merge requests found
Fallback to next available format when browser fails to decode audio (#925)
...@@ -262,7 +262,7 @@ export default { ...@@ -262,7 +262,7 @@ export default {
sourceErrors: 0, sourceErrors: 0,
progressInterval: null, progressInterval: null,
maxPreloaded: 3, maxPreloaded: 3,
preloadDelay: 15, preloadDelay: 5,
soundsCache: [], soundsCache: [],
soundId: null, soundId: null,
playTimeout: null, playTimeout: null,
...@@ -374,12 +374,18 @@ export default { ...@@ -374,12 +374,18 @@ export default {
this.$store.commit("player/isLoadingAudio", false) this.$store.commit("player/isLoadingAudio", false)
this.$store.dispatch("player/trackErrored") this.$store.dispatch("player/trackErrored")
}, },
getSound (trackData) { getSound (trackData, skippedFormats = []) {
let cached = this.getSoundFromCache(trackData) let cached = this.getSoundFromCache(trackData)
if (cached) { if (cached) {
return cached.sound return cached.sound
} }
let srcs = this.getSrcs(trackData) let srcs = this.getSrcs(trackData).filter((s) => {
return skippedFormats.indexOf(s.type) === -1
})
let srcsByUrl = {}
srcs.forEach(s => {
srcsByUrl[s.url] = s
})
let self = this let self = this
let sound = new Howl({ let sound = new Howl({
src: srcs.map((s) => { return s.url }), src: srcs.map((s) => { return s.url }),
...@@ -413,12 +419,23 @@ export default { ...@@ -413,12 +419,23 @@ export default {
self.$store.commit('player/errored', false) self.$store.commit('player/errored', false)
self.$store.commit('player/duration', this.duration()) self.$store.commit('player/duration', this.duration())
}, },
onloaderror: function (sound, error) { onloaderror: async function (sound, error) {
self.removeFromCache(this) self.removeFromCache(this)
if (this != self.currentSound) { if (this != self.currentSound) {
return return
} }
if (error === 4) {
console.log('Error while decoding:', sound, error)
if (skippedFormats.length === 0 && srcs.length > 1) {
skippedFormats.push(srcs[0].type)
console.log(`Playing ${srcs[0].type} failed, loading next format ${srcs[1].type}`)
await self.loadSound(trackData, null, skippedFormats)
console.log('Replacing current sound with alternative format')
return
}
} else {
console.log('Error while playing:', sound, error) console.log('Error while playing:', sound, error)
}
self.handleError({sound, error}) self.handleError({sound, error})
}, },
}) })
...@@ -579,9 +596,11 @@ export default { ...@@ -579,9 +596,11 @@ export default {
if (toKeep.length < self.maxPreloaded) { if (toKeep.length < self.maxPreloaded) {
toKeep.push(e) toKeep.push(e)
} else { } else {
if (e.sound) {
let src = e.sound._src let src = e.sound._src
e.sound.unload() e.sound.unload()
} }
}
}) })
this.soundsCache = _.reverse(toKeep) this.soundsCache = _.reverse(toKeep)
}, },
...@@ -596,7 +615,7 @@ export default { ...@@ -596,7 +615,7 @@ export default {
}) })
this.soundsCache = toKeep this.soundsCache = toKeep
}, },
async loadSound (newValue, oldValue) { async loadSound (newValue, oldValue, skippedFormats = []) {
let trackData = newValue let trackData = newValue
let oldSound = this.currentSound let oldSound = this.currentSound
if (oldSound && trackData !== oldValue) { if (oldSound && trackData !== oldValue) {
...@@ -611,7 +630,7 @@ export default { ...@@ -611,7 +630,7 @@ export default {
if (trackData === null) { if (trackData === null) {
this.handleError({}) this.handleError({})
} }
this.currentSound = this.getSound(trackData) this.currentSound = this.getSound(trackData, skippedFormats)
this.$store.commit('player/isLoadingAudio', true) this.$store.commit('player/isLoadingAudio', true)
if (this.playing) { if (this.playing) {
this.soundId = this.currentSound.play() this.soundId = this.currentSound.play()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment