fixed audio player with safari

This commit is contained in:
2018-04-25 12:14:31 +02:00
parent 2004adee70
commit fff698433f
2 changed files with 62 additions and 16 deletions
@@ -749,7 +749,7 @@
this.fid; this.fid;
this.audio = new Audio(); this.audio = new Audio();
// audio events // audio events
this.audio_events = ["loadedmetadata","canplay","playing","pause","timeupdate","ended","error"]; this.audio_events = ["loadedmetadata","playing","pause","timeupdate","ended","error"]; // ,"canplay"
// UI dom objects // UI dom objects
this.$container = $('<div id="audio-player">'); this.$container = $('<div id="audio-player">');
@@ -907,6 +907,8 @@
setSRC(url){ setSRC(url){
console.log('AudioPlayer setSRC : url', url); console.log('AudioPlayer setSRC : url', url);
this.audio.src = url; this.audio.src = url;
// need to trigger play right now to prevent safari blocking auto-play
this.play();
}, },
onLoadedmetadata(){ onLoadedmetadata(){
var rem = parseInt(this.audio.duration, 10), var rem = parseInt(this.audio.duration, 10),
@@ -916,29 +918,50 @@
this.updateLoadingBar(); this.updateLoadingBar();
}, },
updateLoadingBar(){ updateLoadingBar(){
console.log("updateLoadingBar");
// var that = this;
// if (this.audio.readyState === 4){ // if (this.audio.readyState === 4){
if(this.audio.buffered.length>0){ if(this.audio.buffered.length>0){
// debugger;
this.$loader.css({ this.$loader.css({
'width':parseInt((100 * this.audio.buffered.end(0) / this.audio.duration), 10)+'%' 'width':parseInt((100 * this.audio.buffered.end(0) / this.audio.duration), 10)+'%'
}); });
if( this.audio.buffered.end(0) < this.audio.duration ){ if( this.audio.buffered.end(0) < this.audio.duration ){
// loop through this function until file is fully loaded // loop through this function until file is fully loaded
var that = this; window.requestAnimationFrame(this.updateLoadingBar.bind(this));
window.requestAnimationFrame(that.updateLoadingBar.bind(that));
}else{ }else{
//console.log('Audio fully loaded'); console.log('Audio fully loaded');
} }
}else{
// as audio buffer is not ready, recall updateLoadingBar in next frame
window.requestAnimationFrame(this.updateLoadingBar.bind(this));
} }
}, },
onCanplay(){ // onCanplay(){
this.play(); // // this.play();
}, // },
onError(){ onError(){
console.warn("Audio Error " + this.audio.error.code + "; details: " + this.audio.error.message); console.warn("Audio Error " + this.audio.error.code + "; details: " + this.audio.error.message);
}, },
play(){ play(){
this.clearTimeOutToHide(); this.clearTimeOutToHide();
this.audio.play(); // this.audio.play();
// Not really needed has we trigger play right after setting the source https://stackoverflow.com/questions/47431439/mp4-video-safari-showing-unhandled-promise-rejection-object-domerror-in-c
var promise = this.audio.play();
// console.log('promise', promise);
if (promise !== undefined) {
promise.catch(error => {
// Auto-play was prevented
// Show a UI element to let the user manually start playback
console.warn('autoplay failed, ask for playing', error);
}).then(() => {
// Auto-play started
console.log('autoplay succeed');
});
}
}, },
playPrevious(){ playPrevious(){
if(this.currentHistoricIndex > 0){ if(this.currentHistoricIndex > 0){
@@ -749,7 +749,7 @@
this.fid; this.fid;
this.audio = new Audio(); this.audio = new Audio();
// audio events // audio events
this.audio_events = ["loadedmetadata","canplay","playing","pause","timeupdate","ended","error"]; this.audio_events = ["loadedmetadata","playing","pause","timeupdate","ended","error"]; // ,"canplay"
// UI dom objects // UI dom objects
this.$container = $('<div id="audio-player">'); this.$container = $('<div id="audio-player">');
@@ -907,6 +907,8 @@
setSRC(url){ setSRC(url){
console.log('AudioPlayer setSRC : url', url); console.log('AudioPlayer setSRC : url', url);
this.audio.src = url; this.audio.src = url;
// need to trigger play right now to prevent safari blocking auto-play
this.play();
}, },
onLoadedmetadata(){ onLoadedmetadata(){
var rem = parseInt(this.audio.duration, 10), var rem = parseInt(this.audio.duration, 10),
@@ -916,29 +918,50 @@
this.updateLoadingBar(); this.updateLoadingBar();
}, },
updateLoadingBar(){ updateLoadingBar(){
console.log("updateLoadingBar");
// var that = this;
// if (this.audio.readyState === 4){ // if (this.audio.readyState === 4){
if(this.audio.buffered.length>0){ if(this.audio.buffered.length>0){
// debugger;
this.$loader.css({ this.$loader.css({
'width':parseInt((100 * this.audio.buffered.end(0) / this.audio.duration), 10)+'%' 'width':parseInt((100 * this.audio.buffered.end(0) / this.audio.duration), 10)+'%'
}); });
if( this.audio.buffered.end(0) < this.audio.duration ){ if( this.audio.buffered.end(0) < this.audio.duration ){
// loop through this function until file is fully loaded // loop through this function until file is fully loaded
var that = this; window.requestAnimationFrame(this.updateLoadingBar.bind(this));
window.requestAnimationFrame(that.updateLoadingBar.bind(that));
}else{ }else{
//console.log('Audio fully loaded'); console.log('Audio fully loaded');
} }
}else{
// as audio buffer is not ready, recall updateLoadingBar in next frame
window.requestAnimationFrame(this.updateLoadingBar.bind(this));
} }
}, },
onCanplay(){ // onCanplay(){
this.play(); // // this.play();
}, // },
onError(){ onError(){
console.warn("Audio Error " + this.audio.error.code + "; details: " + this.audio.error.message); console.warn("Audio Error " + this.audio.error.code + "; details: " + this.audio.error.message);
}, },
play(){ play(){
this.clearTimeOutToHide(); this.clearTimeOutToHide();
this.audio.play(); // this.audio.play();
// Not really needed has we trigger play right after setting the source https://stackoverflow.com/questions/47431439/mp4-video-safari-showing-unhandled-promise-rejection-object-domerror-in-c
var promise = this.audio.play();
// console.log('promise', promise);
if (promise !== undefined) {
promise.catch(error => {
// Auto-play was prevented
// Show a UI element to let the user manually start playback
console.warn('autoplay failed, ask for playing', error);
}).then(() => {
// Auto-play started
console.log('autoplay succeed');
});
}
}, },
playPrevious(){ playPrevious(){
if(this.currentHistoricIndex > 0){ if(this.currentHistoricIndex > 0){