2017-06-12 32 views
2

我一直在試圖讓一個振盪器在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/不要移動爲我工作。任何人有任何想法?

+1

您可以編輯您的問題向我們展示你已經到目前爲止已經試過,這樣我們就可以幫助可能調整代碼? – stefanobaghino

+0

問題編輯以展示我已經試過到目前爲止 – bmacd

回答

-1

這適用於振盪器

<script> 
    var audioCtx = new (window.AudioContext || window.webkitAudioContext)(); 
    var oscillator = audioCtx.createOscillator(); 
    var gainNode = audioCtx.createGain(); 
    oscillator.connect(gainNode); 
    gainNode.connect(audioCtx.destination); 
    oscillator.type = 'sine'; 
    oscillator.frequency.value = 300; 
    gainNode.gain.value = 7.5; 
</script> 

<button onclick="oscillator.start();">play</button> 
+0

此代碼不會對iOS的Safari瀏覽器工作11 https://bl.ocks.org/clhenrick/raw/1d5d49c7a6573ad6a7095518c393ae0b/e6161675fe94c2469883820c6a6cf0fd8651d213/ – clhenrick

+0

我將在後面對其進行編輯,謝謝你這是iOS 11之前的舊帖子 – user7951676

相關問題