2017-10-15 84 views
0

我試圖做出各種按鈕,當它們被點擊時將改變音頻文件的位置(右/左)。第一個音頻「風」工作正常。它通過單擊播放並根據按下哪個按鈕來更改位置。然而,第二個音頻「教堂鈴聲」不改變位置。我真的不確定這是如何工作的,所以我嘗試改變每個事物的名稱以給它一個唯一的標識符,但它不起作用。如何使用Web Audio API更改點擊音頻位置?

<script> 
 
    
 

 
let audioContext = new AudioContext(); 
 

 
// Create a (1st-order Ambisonic) Songbird scene. 
 
let songbird = new Songbird(audioContext); 
 

 
// Send songbird's binaural output to stereo out. 
 
songbird.output.connect(audioContext.destination); 
 

 
// Set room acoustics properties. 
 
let dimensions = { 
 
    width : 4, 
 
    height : 2.5, 
 
    depth : 3 
 
}; 
 
let materials = { 
 
    left : 'plaster-rough', 
 
    right : 'plaster-rough', 
 
    front : 'plaster-rough', 
 
    back : 'plaster-rough', 
 
    down : 'plywood-panel', 
 
    up : 'plaster-rough' 
 
}; 
 
songbird.setRoomProperties(dimensions, materials); 
 

 
// Create an audio element. Feed into audio graph. 
 
let audioElement = document.createElement('audio'); 
 
audioElement.src = 'wind.ogg'; 
 

 

 
let audioElementSource = 
 
    audioContext.createMediaElementSource(audioElement); 
 

 
// Create a Source, connect desired audio input to it. 
 
let source = songbird.createSource(); 
 
audioElementSource.connect(source.input); 
 

 

 
// The source position is relative to the origin 
 
// (center of the room). 
 
source.setPosition(0.0, 8.9, 0); 
 

 
// Playback the audio. 
 
function wind() { 
 
    
 
audioElement.play(); 
 
audioElement.loop = true; 
 

 
} 
 

 

 
function right1() { 
 
source.setPosition(-0.9, 8.9, 0); 
 
} 
 
function right2() { 
 
source.setPosition(-2, 8.9, -1); 
 
} 
 
function right3() { 
 
source.setPosition(-1, 8.9, -2); 
 
} 
 
function right4() { 
 
source.setPosition(0, 8.9, -3); 
 
} 
 
function right5() { 
 
source.setPosition(1, 8.9, -2); 
 
} 
 
function right6() { 
 
source.setPosition(2, 8.9, -1); 
 
} 
 
function right7() { 
 
source.setPosition(0.9, 8.9, 0); 
 
} 
 
function right8() { 
 
source.setPosition(0, 8.9, 0); 
 
} 
 

 

 
    </script> 
 
    
 
<!-- Church Bell --> 
 
    <script> 
 

 
let audioContext1 = new AudioContext(); 
 

 
// Create a (1st-order Ambisonic) Songbird scene. 
 
let songbird1 = new Songbird(audioContext1); 
 

 
// Send songbird's binaural output to stereo out. 
 
songbird1.output.connect(audioContext1.destination); 
 

 
// Set room acoustics properties. 
 
let dimensions = { 
 
    width : 4, 
 
    height : 2.5, 
 
    depth : 3 
 
}; 
 
let materials = { 
 
    left : 'plaster-rough', 
 
    right : 'plaster-rough', 
 
    front : 'plaster-rough', 
 
    back : 'plaster-rough', 
 
    down : 'plywood-panel', 
 
    up : 'plaster-rough' 
 
}; 
 
songbird1.setRoomProperties(dimensions, materials); 
 

 
// Create an audio element. Feed into audio graph. 
 
    
 
let audioElement1 = document.createElement('audio'); 
 
audioElement1.src = 'churchbells.ogg'; 
 

 

 
let audioElementSource1 = 
 
    audioContext1.createMediaElementSource(audioElement1); 
 

 
// Create a Source, connect desired audio input to it. 
 
let source1 = songbird1.createSource(); 
 
audioElementSource1.connect(source1.input); 
 

 
// The source position is relative to the origin 
 
// (center of the room). 
 
source1.setPosition(-99, 8.9, 0); 
 
function churchbells() { 
 
// Playback the audio. 
 
audioElement1.play(); 
 
} 
 
function churchbellsright1() { 
 

 
source1.setPosition(99, 8.9, 0); 
 
} 
 

 
    </script>
<script src="https://raw.githubusercontent.com/google/songbird/master/build/songbird.js"></script> 
 

 
    <button onclick="play();">play</button> 
 

 
    <button onclick="right1();">right 1</button> 
 
    <button onclick="right2();">right 2</button> 
 
    <button onclick="right3();">right 3</button> 
 
    <button onclick="right4();">right 4</button> 
 
    <button onclick="right5();">right 5</button> 
 
    <button onclick="right6();">right 6</button> 
 
    <button onclick="right7();">right 7</button> 
 
    <button onclick="right8();">right 8</button> 
 
    <button onclick="churchbellsright1();">right 8</button>

回答

0

您已經定義了play函數內部的功能right1right2等。這意味着它們將不可用於onclick處理程序。在函數中定義的任何東西只能在該函數中使用。所以你需要在play函數之外定義它們。

您還需要在該函數之外聲明source,以便right1函數可以訪問該函數。嘗試這樣的:

// declare source outside of play 
let source; 

function play() { 
    // Start of the play() code 

    // Assign songbird.createSource() to the source variable we already declared 
    source = songbird.createSource(); 
    audioElementSource.connect(source.input); 

    // The rest of the play() code 
} 

function right1() { 
    source.setPosition(-0.9, 8.9, 0); 
} 
// define right2, right3, etc here. 
+0

這確實使它的工作,謝謝。但是,我意識到我擁有的第二個音頻不能以相同的方式工作。我用代碼更新了我原來的問題。我對此很新,所以我不知道如何解決這個問題。我嘗試改變第二個音頻(教堂鈴聲)中的所有名稱,給它一個唯一的標識符(source1 vs source,audioelement1 vs audioelement等),但它不起作用。如果我把教堂鈴聲功能放在最上面,它會播放音頻,但不會改變位置。當它放置在代碼中時,音頻和位置都不起作用。任何想法如何做一個以上的工作? – user2454060

+0

爲'source','play'和'right1'等創建另一個不同名稱的副本應該可以工作。下面是我提出的一個簡單示例,它每次播放兩個:https://gist.github.com/tfogo/b80a3e4ea184c0c653f0ee1b1349a4dc它從'play()'返回源,以便您可以用不同的音樂文件調用它,並且可以將其分配給不同的變量。這不是最乾淨的代碼,但它的工作原理。最好讓你的函數可以重用,而不是一遍又一遍地複製它們,但內容略有不同。 – tfogo

+0

它看起來像工作。非常感謝,我很放心,我不知道我是如何克服這個問題的。 – user2454060

相關問題