mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-09-01 21:49:21 +03:00
Testing van be done at https://snappymail.eu/demo/
This commit is contained in:
parent
0ec37b7e90
commit
542d9c91e9
10 changed files with 215 additions and 270 deletions
|
|
@ -1,128 +1,128 @@
|
|||
import * as Links from 'Common/Links';
|
||||
|
||||
class Audio {
|
||||
notificator = null;
|
||||
player = null;
|
||||
let notificator = null,
|
||||
player = null,
|
||||
canPlay = type => player && !!player.canPlayType(type).replace('no', ''),
|
||||
|
||||
supported = false;
|
||||
supportedMp3 = false;
|
||||
supportedOgg = false;
|
||||
supportedWav = false;
|
||||
supportedNotification = false;
|
||||
audioCtx = AudioContext || window.webkitAudioContext,
|
||||
|
||||
constructor() {
|
||||
this.player = this.createNewObject();
|
||||
|
||||
// Safari can't play without user interaction
|
||||
this.supported = !!this.player && !!this.player.play;
|
||||
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/, '');
|
||||
this.supportedNotification = this.supported && this.supportedMp3;
|
||||
play = (url, name) => {
|
||||
if (player) {
|
||||
player.src = url;
|
||||
player.play();
|
||||
name = name.trim();
|
||||
dispatchEvent(new CustomEvent('audio.start', {detail:name.replace(/\.([a-z0-9]{3})$/, '') || 'audio'}));
|
||||
}
|
||||
},
|
||||
|
||||
if (!this.player || (!this.supportedMp3 && !this.supportedOgg && !this.supportedWav)) {
|
||||
this.supported = false;
|
||||
this.supportedMp3 = false;
|
||||
this.supportedOgg = false;
|
||||
this.supportedWav = false;
|
||||
this.supportedNotification = false;
|
||||
}
|
||||
|
||||
if (this.supported && this.player) {
|
||||
const stopFn = () => this.stop();
|
||||
|
||||
this.player.addEventListener('ended', stopFn);
|
||||
this.player.addEventListener('error', stopFn);
|
||||
|
||||
addEventListener('audio.api.stop', stopFn);
|
||||
}
|
||||
}
|
||||
|
||||
createNewObject() {
|
||||
createNewObject = () => {
|
||||
try {
|
||||
const player = window.Audio ? new Audio() : null;
|
||||
if (player && player.canPlayType && player.pause && player.play) {
|
||||
const player = new Audio;
|
||||
if (player.canPlayType && player.pause && player.play) {
|
||||
player.preload = 'none';
|
||||
player.loop = false;
|
||||
player.autoplay = false;
|
||||
player.muted = false;
|
||||
return player;
|
||||
}
|
||||
|
||||
return player;
|
||||
} catch (e) {} // eslint-disable-line no-empty
|
||||
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
return null;
|
||||
},
|
||||
|
||||
// The AudioContext is not allowed to start.
|
||||
// It must be resumed (or created) after a user gesture on the page. https://goo.gl/7K7WLu
|
||||
// Setup listeners to attempt an unlock
|
||||
unlockEvents = [
|
||||
'click','dblclick',
|
||||
'contextmenu',
|
||||
'auxclick',
|
||||
'mousedown','mouseup',
|
||||
'pointerup',
|
||||
'touchstart','touchend',
|
||||
'keydown','keyup'
|
||||
],
|
||||
unlock = () => {
|
||||
if (audioCtx) {
|
||||
console.log('AudioContext ' + audioCtx.state);
|
||||
audioCtx.resume();
|
||||
}
|
||||
unlockEvents.forEach(type => document.removeEventListener(type, unlock, true));
|
||||
// setTimeout(()=>Audio.playNotification(1),1);
|
||||
};
|
||||
|
||||
if (audioCtx) {
|
||||
audioCtx = audioCtx ? new audioCtx : null;
|
||||
audioCtx.onstatechange = unlock;
|
||||
}
|
||||
unlockEvents.forEach(type => document.addEventListener(type, unlock, true));
|
||||
|
||||
/**
|
||||
* Browsers can't play without user interaction
|
||||
*/
|
||||
|
||||
const SMAudio = new class {
|
||||
supported = false;
|
||||
supportedMp3 = false;
|
||||
supportedOgg = false;
|
||||
supportedWav = false;
|
||||
|
||||
constructor() {
|
||||
player || (player = createNewObject());
|
||||
|
||||
this.supported = !!player;
|
||||
if (player) {
|
||||
this.supportedMp3 = !!canPlay('audio/mpeg;');
|
||||
this.supportedWav = !!canPlay('audio/wav; codecs="1"');
|
||||
this.supportedOgg = !!canPlay('audio/ogg; codecs="vorbis"');
|
||||
|
||||
const stopFn = () => this.pause();
|
||||
player.addEventListener('ended', stopFn);
|
||||
player.addEventListener('error', stopFn);
|
||||
addEventListener('audio.api.stop', stopFn);
|
||||
}
|
||||
}
|
||||
|
||||
paused() {
|
||||
return this.supported ? !!this.player.paused : true;
|
||||
return !player || player.paused;
|
||||
}
|
||||
|
||||
stop() {
|
||||
if (this.supported && this.player.pause) {
|
||||
this.player.pause();
|
||||
}
|
||||
|
||||
dispatchEvent(new CustomEvent('audio.stop'));
|
||||
this.pause();
|
||||
}
|
||||
|
||||
pause() {
|
||||
this.stop();
|
||||
}
|
||||
|
||||
clearName(name = '', ext = '') {
|
||||
name = name.trim();
|
||||
if (ext && '.' + ext === name.toLowerCase().substr((ext.length + 1) * -1)) {
|
||||
name = name.substr(0, name.length - 4).trim();
|
||||
}
|
||||
|
||||
return name || 'audio';
|
||||
player && player.pause();
|
||||
dispatchEvent(new CustomEvent('audio.stop'));
|
||||
}
|
||||
|
||||
playMp3(url, name) {
|
||||
if (this.supported && this.supportedMp3) {
|
||||
this.player.src = url;
|
||||
this.player.play();
|
||||
|
||||
dispatchEvent(new CustomEvent('audio.start', {detail:this.clearName(name, 'mp3')}));
|
||||
}
|
||||
this.supportedMp3 && play(url, name);
|
||||
}
|
||||
|
||||
playOgg(url, name) {
|
||||
if (this.supported && this.supportedOgg) {
|
||||
this.player.src = url;
|
||||
this.player.play();
|
||||
|
||||
name = this.clearName(name, 'oga');
|
||||
name = this.clearName(name, 'ogg');
|
||||
|
||||
dispatchEvent(new CustomEvent('audio.start', {detail:name}));
|
||||
}
|
||||
this.supportedOgg && play(url, name);
|
||||
}
|
||||
|
||||
playWav(url, name) {
|
||||
if (this.supported && this.supportedWav) {
|
||||
this.player.src = url;
|
||||
this.player.play();
|
||||
|
||||
dispatchEvent(new CustomEvent('audio.start', {detail:this.clearName(name, 'wav')}));
|
||||
}
|
||||
this.supportedWav && play(url, name);
|
||||
}
|
||||
|
||||
playNotification() {
|
||||
if (this.supported && this.supportedMp3) {
|
||||
if (!this.notificator) {
|
||||
this.notificator = this.createNewObject();
|
||||
this.notificator.src = Links.sound('new-mail.mp3');
|
||||
playNotification(silent) {
|
||||
if ('running' == audioCtx.state && (this.supportedMp3 || this.supportedOgg)) {
|
||||
if (!notificator) {
|
||||
notificator = createNewObject();
|
||||
notificator.src = Links.sound('new-mail.'+ (this.supportedMp3 ? 'mp3' : 'ogg'));
|
||||
}
|
||||
|
||||
if (this.notificator && this.notificator.play) {
|
||||
this.notificator.play();
|
||||
if (notificator) {
|
||||
notificator.volume = silent ? 0.01 : 1;
|
||||
notificator.play();
|
||||
}
|
||||
} else {
|
||||
console.log('No audio: ' + audioCtx.state);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default new Audio();
|
||||
export default SMAudio;
|
||||
|
|
|
|||
|
|
@ -197,16 +197,6 @@ export const MessageSelectAction = {
|
|||
Unflagged: 6
|
||||
};
|
||||
|
||||
/**
|
||||
* @enum {number}
|
||||
*/
|
||||
export const DesktopNotification = {
|
||||
Allowed: 0,
|
||||
NotAllowed: 1,
|
||||
Denied: 2,
|
||||
NotSupported: 9
|
||||
};
|
||||
|
||||
/**
|
||||
* @enum {number}
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue