snappymail/dev/Common/Audio.js

129 lines
3 KiB
JavaScript
Raw Normal View History

2016-06-16 07:36:44 +08:00
import * as Links from 'Common/Links';
2015-11-15 08:23:16 +08:00
2019-07-05 03:19:24 +08:00
class Audio {
2016-09-10 06:38:16 +08:00
notificator = null;
player = null;
2016-06-28 04:54:38 +08:00
2016-09-10 06:38:16 +08:00
supported = false;
supportedMp3 = false;
supportedOgg = false;
supportedWav = false;
supportedNotification = false;
2016-06-28 04:54:38 +08:00
2016-09-10 06:38:16 +08:00
constructor() {
2015-11-15 08:23:16 +08:00
this.player = this.createNewObject();
2020-08-07 22:28:30 +08:00
// Safari can't play without user interaction
this.supported = !!this.player && !!this.player.play;
2019-07-05 03:19:24 +08:00
if (this.supported && this.player && this.player.canPlayType) {
this.supportedMp3 = !!this.player.canPlayType('audio/mpeg;').replace(/no/, '');
this.supportedWav = !!this.player.canPlayType('audio/wav; codecs="1"').replace(/no/, '');
this.supportedOgg = !!this.player.canPlayType('audio/ogg; codecs="vorbis"').replace(/no/, '');
2015-11-15 08:23:16 +08:00
this.supportedNotification = this.supported && this.supportedMp3;
}
2019-07-05 03:19:24 +08:00
if (!this.player || (!this.supportedMp3 && !this.supportedOgg && !this.supportedWav)) {
2015-11-15 08:23:16 +08:00
this.supported = false;
this.supportedMp3 = false;
this.supportedOgg = false;
this.supportedWav = false;
this.supportedNotification = false;
}
2019-07-05 03:19:24 +08:00
if (this.supported && this.player) {
2016-08-30 06:10:24 +08:00
const stopFn = () => this.stop();
this.player.addEventListener('ended', stopFn);
this.player.addEventListener('error', stopFn);
addEventListener('audio.api.stop', stopFn);
2015-11-15 08:23:16 +08:00
}
}
createNewObject() {
2019-07-12 02:45:47 +08:00
try {
const player = window.Audio ? new Audio() : null;
2019-07-12 02:45:47 +08:00
if (player && player.canPlayType && player.pause && player.play) {
player.preload = 'none';
player.loop = false;
player.autoplay = false;
player.muted = false;
}
return player;
} catch (e) {} // eslint-disable-line no-empty
2015-11-15 08:23:16 +08:00
2019-07-12 02:45:47 +08:00
return null;
2015-11-15 08:23:16 +08:00
}
paused() {
return this.supported ? !!this.player.paused : true;
}
stop() {
2019-07-05 03:19:24 +08:00
if (this.supported && this.player.pause) {
2015-11-15 08:23:16 +08:00
this.player.pause();
}
dispatchEvent(new CustomEvent('audio.stop'));
2015-11-15 08:23:16 +08:00
}
pause() {
this.stop();
}
clearName(name = '', ext = '') {
name = name.trim();
2019-07-05 03:19:24 +08:00
if (ext && '.' + ext === name.toLowerCase().substr((ext.length + 1) * -1)) {
name = name.substr(0, name.length - 4).trim();
2015-11-15 08:23:16 +08:00
}
return name || 'audio';
2015-11-15 08:23:16 +08:00
}
playMp3(url, name) {
2019-07-05 03:19:24 +08:00
if (this.supported && this.supportedMp3) {
2015-11-15 08:23:16 +08:00
this.player.src = url;
this.player.play();
dispatchEvent(new CustomEvent('audio.start', {detail:this.clearName(name, 'mp3')}));
2015-11-15 08:23:16 +08:00
}
}
playOgg(url, name) {
2019-07-05 03:19:24 +08:00
if (this.supported && this.supportedOgg) {
2015-11-15 08:23:16 +08:00
this.player.src = url;
this.player.play();
name = this.clearName(name, 'oga');
name = this.clearName(name, 'ogg');
dispatchEvent(new CustomEvent('audio.start', {detail:name}));
2015-11-15 08:23:16 +08:00
}
}
playWav(url, name) {
2019-07-05 03:19:24 +08:00
if (this.supported && this.supportedWav) {
2015-11-15 08:23:16 +08:00
this.player.src = url;
this.player.play();
dispatchEvent(new CustomEvent('audio.start', {detail:this.clearName(name, 'wav')}));
2015-11-15 08:23:16 +08:00
}
}
playNotification() {
2019-07-05 03:19:24 +08:00
if (this.supported && this.supportedMp3) {
if (!this.notificator) {
2015-11-15 08:23:16 +08:00
this.notificator = this.createNewObject();
this.notificator.src = Links.sound('new-mail.mp3');
}
2019-07-05 03:19:24 +08:00
if (this.notificator && this.notificator.play) {
2015-11-15 08:23:16 +08:00
this.notificator.play();
}
}
}
}
2016-09-10 06:38:16 +08:00
export default new Audio();