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';
|
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) {
|
2020-07-28 23:20:14 +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/, '');
|
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);
|
|
|
|
|
|
|
|
Events.sub('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 window.Audio() : null;
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
Events.pub('audio.stop');
|
|
|
|
}
|
|
|
|
|
|
|
|
pause() {
|
|
|
|
this.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
clearName(name = '', ext = '') {
|
2020-08-07 00:24:46 +08:00
|
|
|
name = name.trim();
|
2019-07-05 03:19:24 +08:00
|
|
|
if (ext && '.' + ext === name.toLowerCase().substr((ext.length + 1) * -1)) {
|
2020-08-07 00:24:46 +08:00
|
|
|
name = name.substr(0, name.length - 4).trim();
|
2015-11-15 08:23:16 +08:00
|
|
|
}
|
|
|
|
|
2020-07-28 23:20:14 +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();
|
|
|
|
|
|
|
|
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();
|