Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load audio completely before playing?

I have to load audio completely before playing it. Player should play audio automatically after loading it completely. There should not be pause button/user can not pause it. Here's my code, but unable to do so.

Hyper Text Markup Language:

<audio class="audio-player" onloadeddata="myOnLoadedData()" onprogress="myOnProgress()"  src="<?php echo $audio_link;?>" controls>

JavaScript:

function myOnLoadedData() {
    console.log('Loaded data'); 
    $('audio.audio-player').attr('autoplay','autoplay');
}
function myOnProgress(){
    console.log('downloading');
}

Or is there any other player that meets my requirement?

like image 745
Niroj Adhikary Avatar asked Oct 19 '22 22:10

Niroj Adhikary


1 Answers

Try using XMLHttpRequest to return Blob of audio file, new Audio() , .load() , .play()

var request = new XMLHttpRequest();
request.open("GET", "/path/to/audio/file/", true);
request.responseType = "blob";    
request.onload = function() {
  if (this.status == 200) {
    var audio = new Audio(URL.createObjectURL(this.response));
    audio.load();
    audio.play();
  }
}
request.send();
like image 199
guest271314 Avatar answered Oct 21 '22 17:10

guest271314