admin管理员组文章数量:1302887
I have to load audio pletely before playing it. Player should play audio automatically after loading it pletely. 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?
I have to load audio pletely before playing it. Player should play audio automatically after loading it pletely. 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?
Share Improve this question edited Nov 17, 2015 at 11:39 user5066707 asked Nov 17, 2015 at 11:15 Niroj AdhikaryNiroj Adhikary 1,83519 silver badges31 bronze badges 6-
Have you tried to use
onload
event? I never used audio inHTML5
, but I'll use and I made a function to load a array with game musics that I didn't tested so far. – user5066707 Commented Nov 17, 2015 at 11:20 -
Try
onloadeddata()
– Alex Commented Nov 17, 2015 at 11:21 - already tried onloadeddata onloadedmetadata.First it must load plete audio and play it automatically – Niroj Adhikary Commented Nov 17, 2015 at 11:26
- FYI, most mobile devices don't allow an audio file to be played without any user interaction – A. Wolff Commented Nov 17, 2015 at 11:29
- its not for mobile devices.i need it on pc – Niroj Adhikary Commented Nov 17, 2015 at 11:31
2 Answers
Reset to default 7Try 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();
I used the method of @guest271314 to do a funtion that load every music link/URL that are in an object.
function LoadSounds(obj,ch,ca){
(function(r){
var i,
lo=0, // number of sounds loaded
len=0, // number of sounds to load
con={}; // an object with the sounds loaded
for(i in obj){
len++ // increase the number of sounds to load
}
for(i in obj){
(function(i){
r=0;
r=new XMLHttpRequest();
r.open("GET",obj[i],true);
r.responseType="blob";
r.onload=function(){
if(this.status==200){
lo++
con[i]=new Audio(URL.createObjectURL(this.response));
con[i].load();
ch(obj[i]);
if(lo==len)ca(con)
r=undefined;
}
}
r.send();
})(i);
}
i=undefined;
})(0);
}
Compressed...!
function LoadSounds(n,o,e){!function(t){var i,s=0,d=0,u={};for(i in n)d++;for(i in n)!function(i){t=0,t=new XMLHttpRequest,t.open("GET",n[i],!0),t.responseType="blob",t.onload=function(){200==this.status&&(s++,u[i]=new Audio(URL.createObjectURL(this.response)),u[i].load(),o(n[i]),s==d&&e(u),t=void 0)},t.send()}(i);i=void 0}(0)}
Using example...!
// Note that these musics in my example aren't in a valid link
var MyMusics={
Music1:'http://example./music.ogg',
BestEver:'http://example./bestever.ogg'
};
LOAD_SOUNDS(
MyMusics, // Musics object
function(){alert("+1 loaded");}, // function when a link music is loaded
function(){e.BestEver.play();} // function when all musics from links were loaded
);
本文标签: javascriptHow to load audio completely before playingStack Overflow
版权声明:本文标题:javascript - How to load audio completely before playing? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741743683a2395406.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论