我一直试图在IOS上使用移动浏览器播放振荡器(在Chrome或Safari中无法工作),我正在努力。根据我所做的研究,我发现你必须在触摸事件上创建振荡器(甚至可能是上下文)。我在桌面上工作的是一个振荡器,它连接到增益节点并在span元素上的mouseenter事件上播放声音。然后在mouseout事件中,它与增益节点断开连接,以便在下一个mouseenter事件中它将再次连接,从而能够在每次角色悬停时创建新声音。
$(".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/在移动设备上对我不起作用。有人有什么想法吗?
答案 0 :(得分:-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>