npm-smooch-test
Version:
*Smooch adds beautifully simple messaging to your app to keep your users engaged and coming back.*
54 lines (44 loc) • 1.47 kB
JavaScript
;
exports.__esModule = true;
exports.isAudioSupported = isAudioSupported;
exports.playNotificationSound = playNotificationSound;
exports.stopNotificationSound = stopNotificationSound;
var _assets = require('./assets');
var notificationSoundPlaying = false;
var currentAudio = void 0;
function isAudioSupported() {
return !!global.Audio;
}
function onNotificationPlaying() {
notificationSoundPlaying = true;
}
function onNotificationPause() {
notificationSoundPlaying = false;
}
function onNotificationEnded() {
notificationSoundPlaying = false;
unloadAudio();
}
function unloadAudio() {
if (currentAudio) {
currentAudio.removeEventListener('playing', onNotificationPlaying);
currentAudio.removeEventListener('pause', onNotificationPause);
currentAudio.removeEventListener('ended', onNotificationEnded);
currentAudio = undefined;
}
}
function playNotificationSound() {
if (isAudioSupported() && !notificationSoundPlaying) {
currentAudio = new Audio(_assets.soundNotification);
currentAudio.addEventListener('playing', onNotificationPlaying);
currentAudio.addEventListener('pause', onNotificationPause);
currentAudio.addEventListener('ended', onNotificationEnded);
currentAudio.play();
}
}
function stopNotificationSound() {
if (currentAudio && notificationSoundPlaying) {
currentAudio.pause();
unloadAudio();
}
}