我一直在試圖讓一個振盪器在IOS上的移動瀏覽器中播放(不會在Chrome或Safari中工作),我很掙扎。從我所做的研究中,我發現你必須在觸摸事件中創建振盪器(甚至可能是上下文)。我在桌面上工作的是一個振盪器,它連接到增益節點並在span元素上的mouseenter事件中播放聲音。然後,在鼠標事件時,它將與增益節點斷開連接,以便在下一次鼠標輸入事件時它將再次連接,從而在每次角色懸停時都能夠創建新的聲音。爲什麼在Safari Mobile中不能播放網絡音頻API振盪器?
$(".hover-letters span").on("touchend mouseenter", function(){
audioVariables($(this));
});
$(".hover-letters span").on("touchstart mouseout", function(){
oscillator.disconnect(gainNode);
});
function audioVariables(element){
resizeWindowWidth = parseInt($(window).width());
frequency = mouseX/(resizeWindowWidth/600);
type = element.parent().data("type");
sound();
}
var firstLetterInteraction = true;
function sound() {
if (firstLetterInteraction === true){
audioCtx = new(window.AudioContext || window.webkitAudioContext)();
oscillator = audioCtx.createOscillator();
gainNode = audioCtx.createGain();
oscillator.start();
}
oscillator.connect(gainNode);
gainNode.connect(audioCtx.destination);
oscillator.frequency.value = frequency;
oscillator.type = type;
firstLetterInteraction = false;
};
由於某些原因,聲音不會在IOS上播放,也不會顯示任何錯誤。我開始不知道這是可能的,甚至是例子,如這裏CaptureWiz例如:
How do I make Javascript beep?
和基於Web Audio API網站給出的例子:http://webaudioapi.com/samples/oscillator/不要移動爲我工作。任何人有任何想法?
您可以編輯您的問題向我們展示你已經到目前爲止已經試過,這樣我們就可以幫助可能調整代碼? – stefanobaghino
問題編輯以展示我已經試過到目前爲止 – bmacd