2013-06-26 2 views
1

Я пытаюсь следовать учебному курсу онлайн, объединяя примеры. Я чувствую, что это должно играть mp3-файл. Я использую браузер Chrome, и он обновлен. У меня нет ошибок на консоли. Я не уверен, что мне нужно изменить или добавить, чтобы сделать эту работу.Базовый веб-аудио API, не воспроизводящий звук

<html> 

<head> 
<script type="text/javascript"> 
var context; 

var sound1Buffer = null; 
var url = 'https://dl.dropboxusercontent.com/u/1957768/SrtV2.mp3'; 


function init(){ 
    try { 
     window.AudioContext = window.AudioContext || window.webkitAudioContext; 
     context = new AudioContext(); 
    } 

    catch(e) { 
     alert("web Audio api is not supported!"); 
    } 
} 

window.addEventListener('load', init, false); 

function loadDogSound(url){ 

    var request = new XMLHttpRequest(); 
    request.open("GET", url, true); 
    request.responseType = 'arrayBuffer'; 

    //decode asynchronously 
    request.onload = function(){ 
     context.decodeAudioData(request.response, function(buffer){ 
      sound1Buffer = buffer; 

     }, onError); 
    } 
    request.send(); 
} 


function playSound(sound1Buffer){ 
    var source = context.createBufferSource(); 
    source.sound1Buffer = sound1Buffer; 

    source.connect(context.destination);  
    source.start(0); 
} 

</script> 
</head> 
<body> 
</body> 
</html> 

ответ

5

Вы никогда не звоните loadDogSound. Если вы называете это, вы обнаружите, что вы получите сообщение об ошибке:

Uncaught ReferenceError: onError is not defined 

Кроме того, вы никогда не называйте playSound.

Вот рабочий пример:

<html> 
<head> 
<script type="text/javascript"> 
    //That one url you wanted. 
    var url = 'https://dl.dropboxusercontent.com/u/1957768/SrtV2.mp3'; 

    /* --- set up web audio --- */ 
    //create the context 
    var context = new webkitAudioContext(); 
    //...and the source 
    var source = context.createBufferSource(); 
    //connect it to the destination so you can hear it. 
    source.connect(context.destination); 

    /* --- load up that buffer --- */ 
    //Basic start to ajax! (I say basic, yet i don't know it well.) 
    var request = new XMLHttpRequest(); 
    //open the request...? 
    request.open('GET', url, true); 
    //I don't even know. 
    request.responseType = 'arraybuffer'; 
    //Once the request has completed... do this 
    request.onload = function() { 
    context.decodeAudioData(request.response, function(response) { 
     /* --- play the sound AFTER we've gotten the buffer loaded --- */ 
     //set the buffer to the response we just received. 
     source.buffer = response; 
     //And off we go! .start(0) should play asap. 
     source.start(0); 
    }, function() { console.error('The request failed.'); }); 
    } 
    //Now that the request has been defined, actually make the request. (send it) 
    request.send(); 
</script> 
</head> 
<body> 
</body> 
</html> 
+0

функция 'Init()' работает без проблем, если браузер не поддерживает аудио API. Это заставило меня подумать, что все функции запускаются при загрузке в браузер. Мне нужно что-то изменить в 'window.addEventListener'? Каков правильный способ вызова 'loadDogSound' и' playSound'. Простите меня, но я все еще очень новичок в этом. – oxxi

+0

Эй, мне нравится общаться с веб-аудио API. Я мог бы помочь вам? Давай общаться? - http://chat.stackoverflow.com/rooms/32365/room-for-uber5001-and-oxxi – uber5001

Смежные вопросы