使用 HTML5 在 iPad 上自动播放音频文件 [英] Autoplay audio files on an iPad with HTML5

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

问题描述

我正在尝试让音频文件在 iPad 上的 Safari 中自动播放.如果我在 Mac 上使用 Safari 访问该页面,那就没问题了.在 iPad 上,自动播放不起作用.

I'm trying to get an audio file to autoplay in Safari on an iPad. If I go to the page using Safari on my Mac, it's fine. On the iPad, autoplay does not work.

推荐答案

更新: 这是一个 hack,它不再适用于 IOS 4.X 及更高版本.这个适用于 IOS 3.2.X.

UPDATE: This is a hack and it's not working anymore on IOS 4.X and above. This one worked on IOS 3.2.X.

这不是真的.Apple 不想在 iPad 上自动播放视频和音频,因为您可以在移动网络上使用大量流量.我不会对在线内容使用自动播放.对于离线 HTML 站点,这是一个很棒的功能,这就是我使用它的目的.

It's not true. Apple doesn't want to autoplay video and audio on IPad because of the high amout of traffic you can use on mobile networks. I wouldn't use autoplay for online content. For Offline HTML sites it's a great feature and thats what I've used it for.

这是一个javascript假点击"解决方案:http://www.rolaplaca.com/examples/html5AutoPlay/

Here is a "javascript fake click" solution: http://www.roblaplaca.com/examples/html5AutoPlay/

复制&来自网站的粘贴代码:

Copy & Pasted Code from the site:

<script type="text/javascript"> 
        function fakeClick(fn) {
            var $a = $('<a href="#" id="fakeClick"></a>');
                $a.bind("click", function(e) {
                    e.preventDefault();
                    fn();
                });

            $("body").append($a);

            var evt, 
                el = $("#fakeClick").get(0);

            if (document.createEvent) {
                evt = document.createEvent("MouseEvents");
                if (evt.initMouseEvent) {
                    evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
                    el.dispatchEvent(evt);
                }
            }

            $(el).remove();
        }

        $(function() {
            var video = $("#someVideo").get(0);

            fakeClick(function() {
                video.play();
            });
        });

        </script> 

这不是我的消息来源.我前段时间发现了这个,并在装有 IOS 3.2.X 的 iPad 和 iPhone 上测试了代码.

This is not my source. I've found this some time ago and tested the code on an IPad and IPhone with IOS 3.2.X.

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

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