Avoid using media element with empty src attribute (#10364)

Whenever the client starts up, the following is emitted in the Firefox
console:

Invalid URI. Load of media resource  failed.
All candidate resources failed to load. Media load paused.

This happens because the <source/> tags are preinitialized with a src
attribute of "".

So what we're doing instead is initialize the <audio/> element without
any children and add the children as soon as the first audio file needs
to be played. This also has the advantage that we can determine at
runtime whether the browser supports Ogg/Vorbis or whether we should
fall back to MPEG layer 3 so only one source element is needed.

Signed-off-by: aszlig <aszlig@nix.build>
This commit is contained in:
aszlig
2018-05-18 17:01:27 +02:00
committed by Matteo Pagliazzi
parent de9883c3ac
commit d3a0348ac7

View File

@@ -44,8 +44,6 @@ div
router-view router-view
app-footer app-footer
audio#sound(autoplay, ref="sound") audio#sound(autoplay, ref="sound")
source#oggSource(type="audio/ogg", :src="sound.oggSource")
source#mp3Source(type="audio/mp3", :src="sound.mp3Source")
</template> </template>
<style lang='scss' scoped> <style lang='scss' scoped>
@@ -220,10 +218,9 @@ export default {
selectedItemToBuy: null, selectedItemToBuy: null,
selectedSpellToBuy: null, selectedSpellToBuy: null,
sound: { audioSource: null,
oggSource: '', audioSuffix: null,
mp3Source: '',
},
loading: true, loading: true,
currentTipNumber: 0, currentTipNumber: 0,
bannerHidden: false, bannerHidden: false,
@@ -260,10 +257,21 @@ export default {
} }
let file = `/static/audio/${theme}/${sound}`; let file = `/static/audio/${theme}/${sound}`;
this.sound = {
oggSource: `${file}.ogg`, if (this.audioSuffix === null) {
mp3Source: `${file}.mp3`, this.audioSource = document.createElement('source');
}; if (this.$refs.sound.canPlayType('audio/ogg')) {
this.audioSuffix = '.ogg';
this.audioSource.type = 'audio/ogg';
} else {
this.audioSuffix = '.mp3';
this.audioSource.type = 'audio/mp3';
}
this.audioSource.src = file + this.audioSuffix;
this.$refs.sound.appendChild(this.audioSource);
} else {
this.audioSource.src = file + this.audioSuffix;
}
this.$refs.sound.load(); this.$refs.sound.load();
}); });