Skip to content
Snippets Groups Projects
Commit 2a6e113e authored by Louis Wicket (wil)'s avatar Louis Wicket (wil)
Browse files

[FIX] mail: fallback extension to mp3 when ogg fails to be played


closes odoo/odoo#106055

Signed-off-by: default avatarAlexandre Kühn (aku) <aku@odoo.com>
parent 488b7123
Branches
Tags
No related merge requests found
......@@ -2,6 +2,7 @@
import { registerModel } from '@mail/model/model_core';
import { attr } from '@mail/model/model_field';
import { clear } from '@mail/model/model_field_command';
registerModel({
name: 'SoundEffect',
......@@ -21,15 +22,14 @@ registerModel({
}
if (!this.audio) {
const audio = new window.Audio();
const ext = audio.canPlayType("audio/ogg; codecs=vorbis") ? ".ogg" : ".mp3";
audio.src = this.path + this.filename + ext;
this.update({ audio });
audio.src = this.source;
}
this.audio.pause();
this.audio.currentTime = 0;
this.audio.loop = loop;
this.audio.volume = volume !== undefined ? volume : this.defaultVolume;
Promise.resolve(this.audio.play()).catch(()=>{});
Promise.resolve(this.audio.play()).catch(e => this._onAudioPlayError(e));
},
/**
* Resets the audio to the start of the track and pauses it.
......@@ -40,6 +40,29 @@ registerModel({
this.audio.currentTime = 0;
}
},
/**
* @private
* @param {DOMException} error
*/
_onAudioPlayError(error) {
if (!this.exists()) {
return;
}
this.update({ audioPlayError: error });
// error on play can trigger fallback to .mp3; retry in case this
// solved the problem.
Promise.resolve(this.audio.play()).catch(() => {});
},
/**
* @private
*/
_onSourceChanged() {
if (!this.audio) {
return;
}
this.audio.src = this.source;
this.audio.load();
},
},
fields: {
/**
......@@ -50,12 +73,26 @@ registerModel({
* then cached.
*/
audio: attr(),
audioPlayError: attr(),
/**
* The default volume to play this sound effect, when unspecified.
*/
defaultVolume: attr({
default: 1,
}),
extension: attr({
compute() {
if (!this.audio) {
return clear();
}
// If the device has tried to play audio and failed, perhaps ogg
// is not supported -> fallback to mp3.
if (this.audioPlayError && this.audioPlayError.name === 'NotSupportedError') {
return '.mp3';
}
return this.audio.canPlayType('audio/ogg; codecs=vorbis') ? '.ogg' : '.mp3';
},
}),
/**
* Name of the audio file.
*/
......@@ -69,5 +106,19 @@ registerModel({
default: '/mail/static/src/audio/',
identifying: true,
}),
source: attr({
compute() {
if (!this.extension) {
return clear();
}
return `${this.path}${this.filename}${this.extension}`;
},
}),
},
onChanges: [
{
dependencies: ['source'],
methodName: '_onSourceChanged',
},
],
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment