音频播放iPad上 [英] Playing audio on iPad

查看:168
本文介绍了音频播放iPad上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个网站,有一个客户端聊天的,但是,我们还是无法在iPad上(iOS 5中)。

I'm working on a website that has a chat for a client, however, we're experiencing problems with audio in iPad (iOS 5).

目标其实是在iPad上与IE7的支持是preferred。

The target is in fact the iPad with support for IE7 is preferred.

我已经试过这些方法:

HTML5

<audio id="notification" preload="auto">
    <source src="audio/notification.ogg" type="audio/ogg" />
    <source src="audio/notification.mp3" type="audio/mpeg" />
</audio>

随着一些JavaScript

With some javascript

var el = document.getElementById('notification');
el.play();


一些JavaScript函数我偷的地方这实际上是一个功能两种不同的方法。请注意,脚本是一个子目录,这样的路径是正确的。


Some javascript function I stole somewhere which in fact are two different methods in one function. Please note the script is in a subdir, so the path is correct.

function notify() {
    var url = '../audio/notification.mp3';
    var a = document.createElement('audio');

    if(!!(a.canPlayType && a.canPlayType('audio/mpeg').replace(/no/, ''))) {
        var sound = new Audio(url);
        sound.load();
        sound.play();
    } else {
        $('#notification').remove();
        var sound = $('<embed id="notification" type="audio/mpeg" src="'+url+'" loop="false" hidden="true" autostart="true" />');
        $(body).append(sound);
    }
}

这两种方法似乎并没有工作。难道我做错了什么?

Both methods doesn't seem to work. Am I doing something wrong?

推荐答案

那么,答案是有点明显。

Well, the answer was somewhat obvious.

经过大量的时间支出做研究等等,我发现<一个href=\"http://developer.apple.com/library/safari/#documentation/AudioVideo/Conceptual/Using_HTML5_Audio_Video/Device-SpecificConsiderations/Device-SpecificConsiderations.html\">an Safari浏览器的官方文档中的文章说:

After a lot of time spending doing research etc, I've found an article in the official documentation of Safari saying:

在野生iOS上​​(对于所有设备,包括ipad公司),其中用户可以是蜂窝网络中,每个数据单元被充电,preLOAD和自动播放被禁止。没有数据加载,直到用户启动它。这意味着JavaScript的play()和load()的方法也没有改变,直到用户启动播放,除非play()或load()方法是由用户操作触发。换句话说,用户启动的播放按钮的作品,但在onload =播放()事件不会。

In Safari on iOS (for all devices, including iPad), where the user may be on a cellular network and be charged per data unit, preload and autoplay are disabled. No data is loaded until the user initiates it. This means the JavaScript play() and load() methods are also inactive until the user initiates playback, unless the play() or load() method is triggered by user action. In other words, a user-initiated Play button works, but an onLoad="play()" event does not.

所以,基本上,你不能没有用户在第一次触发它发动的声音。作为解决方案,我创建了一个静音按钮即关在默认情况下,所以你必须点击它起着通知声音。后来我可以使用JavaScript来播放声音,而无需用户交互。

So, basically, you can't launch a sound without the user triggering it at first. As solution I created a mute button that is off on default, so you have to click it which plays the notification sound. Afterwards I can use Javascript to play the sound without user interaction.

感谢您的Safari为这个伟大的未来。非常感谢。

Thank you Safari for this great future. Thanks a lot.

这篇关于音频播放iPad上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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