This repository has been archived on 2023-09-06. You can view files and clone it, but cannot push or open issues or pull requests.
vn-softphone/static/js/audio.js

77 lines
1.3 KiB
JavaScript

let audio = {
audioLoop: null,
init: function() {
if (ua.audioInstance)
return;
ua.audioInstance = document.createElement('audio');
},
addTrack: function(track) {
if (ua.audioInstance)
this.stop();
this.init();
ua.audioInstance.src = this.getTrackPath(track);
},
addStream: function(stream) {
if (ua.audioInstance)
this.stop();
this.init();
ua.audioInstance.srcObject = stream;
},
play: function(loop = false) {
if (!ua.audioInstance)
return;
ua.audioInstance.volume = Softphone.getVolume();
ua.audioInstance.play();
if (loop) {
this.audioLoop = setInterval(function() {
ua.audioInstance.play();
}, 2000);
}
},
stop: function() {
if (!ua.audioInstance)
return;
if (this.audioLoop)
clearInterval(this.audioLoop);
ua.audioInstance.pause();
ua.audioInstance = null;
},
isPlaying: function() {
if (!ua.audioInstance.paused)
return true;
},
getTrackPath: function(track) {
return `/static/audio/${track}.wav`;
}
}