简体   繁体   English

html5音频不是由iPhone / pod上的加速触发的

[英]html5 audio not triggered by acceleration on iPhone / pod

I want to trigger an audio file when I shake the iPhone. 我想在摇动iPhone时触发音频文件。 I can get audio to play with a button press. 按下按钮即可播放音频。 I can get audio to play on shake after I have first used the button press. 第一次使用按钮后,我可以摇动音频。 But on first entry to the page shake accelerometer does not play the audio. 但是在第一次进入页面时,震动加速度计不会播放音频。 Code is below and a little test page is at: 代码在下面,测试页面在以下位置:

pineappleapps.com/zdev/html5test ((needs iPhone or iPod iOS v4.2 to test) pineappleapps.com/zdev/html5test((需要iPhone或iPod iOS v4.2进行测试)

The scenarios are as follows: 方案如下:

1) refresh the page and tilt iPhone and play alert appears but no audio 1)刷新页面并倾斜iPhone,并出现播放警报,但没有声音

2) press baseball button and alert appears then audio plays 2)按棒球按钮,出现警报,然后播放音频

3) now tilt the phone and alert appears and then audio plays 3)现在倾斜手机并出现警报,然后播放音频

4) refresh the page again and tilt phone and again alert appears but no audio 4)再次刷新页面并再次倾斜手机,但没有声音提示

Must be something to do with the button initializing the audio. 必须与初始化音频的按钮有关。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Code: 码:

javascript: javascript:

function baseball()
{
    alert('play');
    document.getElementById('bbaudio').play();
    var t=setTimeout("enableShake()",3000);
}
function enableShake()
{
    document.getElementById("shakeE").innerHTML = '1';
}

var ax = 0; var ax = 0; var ay = 0; var ay = 0; var shakemax = 0; var shakemax = 0;

window.ondevicemotion = function(event) 
{
    ax = event.accelerationIncludingGravity.x;
    ay = event.accelerationIncludingGravity.y;
    az = event.accelerationIncludingGravity.z;

    if(ax > shakemax)
    {
        shakemax = ax;
    }

    var shake = document.getElementById("shake");
    shake.innerHTML = ax;

    if(ax > 8.0)
    {
        var se = document.getElementById("shakeE").innerHTML;
        if(se == '1')
        {
            document.getElementById("shakeE").innerHTML = '0';
            baseball();
        }

    }
}

html: HTML:

<br />
<br />
<input type="submit" onclick="baseball()" value="baseball"/>    
<br />
<br />
<br />
<br />      
<div id="shake">0</div> 
<div id="shakeE">1</div>
<audio id='bbaudio' src="baseball_hit.wav" controls="controls"></audio>

in this linked article , it says you can affect sounds by first loading them. 这篇链接文章中 ,它说您可以通过先加载声音来影响声音。 I don't know if this works for sure since i haven't started developing for iPhone just yet. 我不知道这是否行得通,因为我还没有开始为iPhone开发。 but i hope this helps! 但我希望这会有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM