2014-12-24 111 views
0

我得到這個「遺漏的類型錯誤:無法讀取屬性nullaudioStuff.js的‘的addEventListener’:39(匿名函數)」網絡音頻API意外語法標記,聲音不會玩

在這一行:

document.querySelector('.play').addEventListener('click', start); 

我不明白,語法似乎爲我是好的,任何想法?

進出口運行通過與8000端口紅寶石服務器的應用程序,我開始使用此代碼:

ruby -r webrick -e "s = WEBrick::HTTPServer.new(:Port => 8000, :DocumentRoot => Dir.pwd); trap('INT') { s.shutdown }; s.start" 

這是全碼:

function stop(){ 
 
    source.stop(context.currentTime); // stop the source immediately 
 
} 
 

 
// Load the Sound with XMLHttpRequest 
 
function start() { 
 
// Note: this will load asynchronously 
 
var request = new XMLHttpRequest(); 
 
request.open("GET", tune.wav, true); request.responseType = "arraybuffer"; // Read as binary data 
 

 
// Asynchronous callback 
 
request.onload = function() { 
 
var data = request.response; 
 
audioRouting(data); 
 
}; 
 

 
request.send(); 
 
} 
 

 

 
// Create Buffered Sound Source 
 
function audioRouting(data) { 
 
    source = context.createBufferSource(); // Create sound source 
 
    context.decodeAudioData(data, function(buffer){ // Create source buffer from raw binary 
 
    source.buffer = buffer; // Add buffered data to object 
 
    source.connect(context.destination); // Connect sound source to output 
 
    playSound(source); // Pass the object to the play function 
 
    }); 
 
} 
 

 

 
// Tell the Source when to play 
 
function playSound() { 
 
source.start(context.currentTime); // play the source immediately 
 
    } 
 

 

 

 
document.querySelector('.play').addEventListener('click', start); 
 
document.querySelector('.stop-button').addEventListener('click', stop);

+0

你確定這段JavaScript代碼是有效的?我試圖解析它,我得到無與倫比的「{」 –

+0

你可以利用http://jslint.com/的解析這段JavaScript代碼來得到一些建議。 –

回答

2

你有頁面加載後添加事件偵聽器。
因此document.querySelector('.play')將返回null,因此錯誤
Uncaught TypeError: Cannot read property 'addEventListener' of nullaudioStuff.js:39 (anonymous function)

它沒有找到elememt「玩」類。

$(document).ready(funtion(){ 
    document.querySelector('.play').addEventListener('click', start); 
    document.querySelector('.stop-button').addEventListener('click', stop); 
});