2013-06-26 5 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를 호출하지 않습니다. 브라우저가 오디오 API를 지원하지 않는 경우

<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

함수가'초기화하기()'문제없이 실행 :

다음은 작업 예입니다. 그래서 브라우저에로드 될 때 모든 기능이 실행된다고 생각하게되었습니다. 'window.addEventListener'에서 무엇인가를 변경해야합니까? 'loadDogSound'와'playSound'를 호출하는 올바른 방법은 무엇입니까? 용서 해주십시오.하지만 저는 아직도이 물건에 대해 아주 새롭습니다. – oxxi

+0

안녕하세요, 저는 웹 오디오 API를 망설이고 있습니다. 내가 도와 줄 수있어? 채팅 하죠? - http://chat.stackoverflow.com/rooms/32365/room-for-uber5001-and-oxxi – uber5001

관련 문제