通过iOS 5 HTML5应用中的javascript事件播放声音 [英] Play a sound via javascript event in iOS 5 HTML5 app

查看:88
本文介绍了通过iOS 5 HTML5应用中的javascript事件播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在新iPad的Mobile Safari iOS 5上开发HTML 5应用程序,在收到发送时显示用户提示。为了引起用户的注意,我们在调用警报功能时会发出如下声音:

Developing an HTML 5 application on new iPad's Mobile Safari iOS 5 that displays a user prompt when a "dispatch" is received. To bring attention to the user, we have a sound that plays like this when a alert function is called:

snd.src = snd.src;
snd.play();

不幸的是,Apple更改了播放声音的方法。有谁知道iOS 5中是否有更新解决了这个问题?

Unfortunately, Apple changed the methodology for playing sounds. Does anyone know if there are updates in iOS 5 that fixes this?

*做了大量研究并且已经意识到iOS 3中的旧hack不再有效。

*Did a lot of research and already aware that the old "hack" in iOS 3 no longer works.

推荐答案

声音需要iOS5中的用户输入。您可以使用隐形按钮初始化声音,该按钮在应用初始化时按一下后消失。触发后,您应该能够以编程方式调用 snd.play()。这是一种欺骗,但希望它有所帮助!

The sounds require user input in iOS5. You can initialize the sound with an "invisible" button that disappears after one press at the initialization of your app. Once triggered, you should be able to call snd.play() programatically. It's kind of a cheat, but hopefully it helps!

Javascript:

Javascript:

if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {

    document.write('<a id="init" ontouchstart="javascript:sndInit();"></a>');

    function sndInit(){
    snd.play();
    snd.pause();
    document.getElementById('init').style.display = 'none';
    }
}

CSS:

<style>
    #init {
        position: absolute;
        margin-right: 0px;
        margin-left: 0px;
        margin-top: 0px;
        margin-bottom: 0px;
        z-index: 1000;
    }
</style>

此外,Remy Sharp还有一些使用音频精灵的技巧这里

Also, Remy Sharp has some tips on working with audio sprites HERE.

这篇关于通过iOS 5 HTML5应用中的javascript事件播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆