iPhone / pod 上的加速未触发 html5 音频
我想在摇动 iPhone 时触发音频文件。我可以通过按下按钮来播放音频。第一次按下按钮后,我可以在摇动时播放音频。但第一次进入页面时,震动加速度计不会播放音频。代码如下,一个小测试页面位于:
pineappleapps.com/zdev/html5test((需要iPhone或iPod iOS v4.2来测试)
场景如下:
1)刷新页面并倾斜iPhone并出现播放警报,但没有音频
2) 按棒球按钮,出现警报,然后播放音频
3) 现在倾斜手机,出现警报,然后播放音频
4) 再次刷新页面,倾斜手机,再次出现警报,但没有音频
一定与按钮有关初始化音频。任何帮助将不胜感激。
代码:
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 ay = 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:
<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>
I want to trigger an audio file when I shake the 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)
The scenarios are as follows:
1) refresh the page and tilt iPhone and play alert appears but no audio
2) press baseball button and alert appears then audio plays
3) now tilt the phone and alert appears and then audio plays
4) refresh the page again and tilt phone and again alert appears but no audio
Must be something to do with the button initializing the audio. Any help would be greatly appreciated.
Code:
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 ay = 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:
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这篇链接的文章中,说你可以通过首先加载声音来影响声音。我不知道这是否确实有效,因为我还没有开始为 iPhone 进行开发。但我希望这有帮助!
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. but i hope this helps!