我試圖通過拼湊示例在線跟隨教程。我覺得這應該是播放MP3文件。我使用的是Chrome瀏覽器,並且它是最新的。我沒有在控制檯上發現任何錯誤。我不確定我需要更改或添加這項工作。 基本Web音頻API不播放MP3文件?
<script type="text/javascript">
//creating an audio context
window.addEventListener('load',init);
function init()
{
try
{
window.AudioContext = window.AudioContext || window.webkitAudioContext;
var context=new AudioContext();
}
catch(e)
{
alert("Your browser doesn't support Web Audio API");
}
loadSound();
playSound();
}
//loading sound into the created audio context
function loadSound()
{
//set the audio file's URL
var audioURL='audio files/song.mp3';
//creating a new request
var request = new XMLhttpRequest();
request.open("GET",audioURL,true);
request.responseType= 'arraybuffer';
request.onLoad funtion(){
//take the audio from http request and decode it in an audio buffer
var audioBuffer = null;
context.decodeAudioData(request.response, function(buffer){ audioBuffer= buffer;});
}
request.send();
}, onError);
//playing the audio file
function playSound(buffer) {
//creating source node
var source = audioContext.createBufferSource();
//passing in file
source.buffer = audioBuffer;
//start playing
source.start(0);
}
</script>
</head>
</html>
我想在上面的代碼中我犯的一個錯誤是,我沒有將參數傳遞給playSound()函數。我改正了它,現在它看起來像:\t playSound(audioBuffer);但它仍然不起作用。 :( –
'''''loadSound()'後面的'onError'我不確定,應該不會執行嗎? – DanielV