snappymail/dev/Common/Audio.js

128 lines
3 KiB
JavaScript
Raw Normal View History

2016-07-02 06:49:59 +08:00
import window from 'window';
2019-07-05 03:19:24 +08:00
import { bMobileDevice, bSafari } from 'Common/Globals';
2016-06-16 07:36:44 +08:00
import * as Links from 'Common/Links';
import * as Events from 'Common/Events';
2019-07-05 03:19:24 +08:00
import { trim } from 'Common/Utils';
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();
2016-06-07 05:57:52 +08:00
this.supported = !bMobileDevice && !bSafari && !!this.player && !!this.player.play;
2019-07-05 03:19:24 +08:00
if (this.supported && this.player && this.player.canPlayType) {
2015-11-15 08:23:16 +08:00
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/, '');
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);
Events.sub('audio.api.stop', stopFn);
2015-11-15 08:23:16 +08:00
}
}
createNewObject() {
const player = window.Audio ? new window.Audio() : null;
2019-07-05 03:19:24 +08:00
if (player && player.canPlayType && player.pause && player.play) {
2015-11-15 08:23:16 +08:00
player.preload = 'none';
player.loop = false;
player.autoplay = false;
player.muted = false;
}
return player;
}
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();
}
Events.pub('audio.stop');
}
pause() {
this.stop();
}
clearName(name = '', ext = '') {
2016-06-07 05:57:52 +08:00
name = trim(name);
2019-07-05 03:19:24 +08:00
if (ext && '.' + ext === name.toLowerCase().substr((ext.length + 1) * -1)) {
2016-06-07 05:57:52 +08:00
name = trim(name.substr(0, name.length - 4));
2015-11-15 08:23:16 +08:00
}
return '' === name ? 'audio' : name;
}
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();
Events.pub('audio.start', [this.clearName(name, 'mp3'), 'mp3']);
}
}
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');
Events.pub('audio.start', [name, 'ogg']);
}
}
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();
Events.pub('audio.start', [this.clearName(name, 'wav'), 'wav']);
}
}
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();