简体   繁体   English

缓冲未在网络音频API JavaScript中加载

[英]Buffer not loading in web-audio api javascript

I've been learning (trying to :-D) Web Audio API.Here in the follwing code i want to play a list of songs in songs[] array. 我一直在学习(尝试:-D)Web Audio API。在下面的代码中,我想播放songs []数组中的歌曲列表。
But i'm getting errors...Please help.Thank you in advance. 但是我遇到了错误...请帮助。谢谢您。 Code:- 码:-

var sources = new Array();
var actx;
var songs = ['src1.mp3'];

function start() {
  console.log("WELCOME!!");
  try{
      actx = new AudioContext();
  }catch(e){
    console.log('WebAudio api is not supported!!');

  }
  var buffer_list =getBuffers(actx,songs);
  //console.log("hello");
  //console.log(buffer_list[1]);
  for (var x = 0;x<(buffer_list.length);x++){
    var i = actx.createBufferSource();
    sources[x] = i  ;
  }
  for (var x = 0;x<(buffer_list.length);x++){
    sources[x].buffer = buffer_list[x];

  }
  for(var x = 0;x<(buffer_list.length);x++){
    sources[x].connect(actx.destination);
  }
}

function getBuffers(actx,songs){
  var buffer_list = new Array();
  for (var x = 0;x<songs.length;x++){
    console.log(songs[x]);
      var request = new XMLHttpRequest();
      request.open('GET',songs[x],true);
      request.responseType = 'arraybuffer';
      //onload function for downloading,After sending request.
      request.onload = function(){
        var audioData = request.response;
        //console.log(audioData);
        if(audioData){
          actx.decodeAudioData(audioData,function(buffer){
            //console.log(buffer);
            console.log(x);
            buffer_list[x] = buffer;
          },
          function(e){
            console.log(e.err);
          });
      }else {
        console.error("problem!!");
      }
  }
  request.send();
}
console.log(buffer_list);
return buffer_list;

}

function play() {
  start();
  sources[0].start(0);
  //sources[1].start(0);
}
function stop(){
  sources[0].stop(0);
  //sources[1].stop(0);
}

When im debugging in web Console, i can see that the array buffer_list[] have the audio buffer in position 1 not in 0. I cheked then the value of x . 当我在Web Console中进行调试时,我可以看到数组buffer_list []的音频缓冲区的位置不是1,也不是0。我检查了x的值。 Its coming 1 not 0 . 其即将到来的1而不是0
But why? 但为什么? i've started x from 0 not 1 ?Please help.. And for testing purpose i've started with a single song in songs[] .. 我是从0开始而不是从1开始的x ,请提供帮助。为了测试目的,我从歌曲中的一首歌开始了[] ..

You can use async/await an Promise to await the completion of multiple asynchronous tasks 您可以使用async/await一个Promise来等待多个异步任务的完成

 var sources = new Array(); var actx; var songs = ["https://upload.wikimedia.org/wikipedia/commons/6/6e/Micronesia_National_Anthem.ogg"]; async function start() { console.log("WELCOME!!"); try { actx = new AudioContext(); } catch (e) { console.log('WebAudio api is not supported!!'); } var buffer_list = await getBuffers(actx, songs); //console.log("hello"); //console.log(buffer_list[1]); for (var x = 0; x < (buffer_list.length); x++) { var i = actx.createBufferSource(); sources[x] = i; } for (var x = 0; x < (buffer_list.length); x++) { sources[x].buffer = buffer_list[x]; } for (var x = 0; x < (buffer_list.length); x++) { sources[x].connect(actx.destination); } } async function getBuffers(actx, songs) { var buffer_list = new Array(); for (var x = 0; x < songs.length; x++) { await new Promise(function(resolve) { console.log(songs[x]); var request = new XMLHttpRequest(); request.open('GET', songs[x], true); request.responseType = 'arraybuffer'; //onload function for downloading,After sending request. request.onload = function() { var audioData = request.response; //console.log(audioData); if (audioData) { actx.decodeAudioData(audioData, function(buffer) { //console.log(buffer); console.log(x); buffer_list[x] = buffer; resolve() }, function(e) { console.log(e.err); }); } else { console.error("problem!!"); } } request.send(); }) } console.log(buffer_list); return buffer_list; } async function play() { await start(); sources[0].start(0); //sources[1].start(0); } async function stop() { sources[0].stop(0); //sources[1].stop(0); } play(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM