如何在 iOS >= 4.2.1 Mobile Safari 中自动播放媒体?

发布于 2024-10-03 16:41:25 字数 339 浏览 0 评论 0原文

除了用户执行的单击事件的处理程序之外,我似乎无法在 iOS 4.2.1 上的 Mobile Safari 中播放音频媒体。即使如此,如果以任何异步方式(ajax、setTimeout 等)调用player.play(),它也不起作用。

我尝试在 player.play() 之前调用 player.load() 。我尝试在 dom 元素上触发点击事件,该元素的处理程序调用 player.play()。我尝试过使用音频和视频标签。

iOS 4.2.1 之前存在的所有漏洞似乎都已被修复。有什么想法吗?

I cannot seem to get audio media to play in Mobile Safari on iOS 4.2.1 in any situation other than the handler of a click event performed by the user. Even then, if player.play() is called in any asynchronous fashion (ajax, setTimeout, etc) it doesn't work.

I've tried calling player.load() before player.play(). I've tried triggering a click event on a dom element whose handler calls player.play(). I've tried using both audio and video tags.

All the loopholes that worked prior to iOS 4.2.1 seem to be closed. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

┼── 2024-10-10 16:41:25

从iOS 4.2.x开始,如果没有用户输入事件(例如touchstart),则不会开始媒体下载。

所以答案是,没有方法可以通过 JavaScript 或其他方式自动播放媒体。

Starting from iOS 4.2.x, the download of media will not be started if there isn’t a user-input event, like touchstart.

So the answer is no, there is no method to autoplay media by JavaScript or something else.

雪若未夕 2024-10-10 16:41:25

在 4.2.1 上有一种方法可以自动播放视频/音频文件。创建一个 iframe 并将其源设置为媒体文件的 URL,并将 iframe 附加到正文。将会自动播放。

var ifr=document.createElement("iframe");
ifr.setAttribute('src', "http://mysite.com/myvideo.mp4");
ifr.setAttribute('width', '1px');
ifr.setAttribute('height', '1px');
ifr.setAttribute('scrolling', 'no');
ifr.style.border="0px";
document.body.appendChild(ifr);

There is one way to play the video/audio file automatically on 4.2.1. Create an iframe and set its source to the media file's URL and append the iframe to the body. It'll be autoplay.

var ifr=document.createElement("iframe");
ifr.setAttribute('src', "http://mysite.com/myvideo.mp4");
ifr.setAttribute('width', '1px');
ifr.setAttribute('height', '1px');
ifr.setAttribute('scrolling', 'no');
ifr.style.border="0px";
document.body.appendChild(ifr);

活雷疯 2024-10-10 16:41:25

正如其他人所说,根据 Apple 文档,MobileSafari 不支持视频标签中的自动播放属性。然而,在 iOS 6 中它确实有效。这似乎是 Apple 在 iOS 6.0.1 中修复的一个错误(大概也在 iOS 6.1 中)。

如果您碰巧发现它在 iOS 6.0 中工作,请不要依赖在 iOS 6 中工作的自动播放功能。

-迈克尔

As others have said here and according to Apple documentation, MobileSafari does NOT support the autoplay attribute in the video tag. However, in iOS 6 it DOES work. This appears to be a bug which Apple fixed in iOS 6.0.1 (and presumably in iOS 6.1).

Don't rely on autoplay working in iOS 6 if you happened to stumble across it working in iOS 6.0.

-Michael

笑脸一如从前 2024-10-10 16:41:25

作为解决方法,我尝试将 load()/play() 代码绑定到单击事件,然后以编程方式触发单击事件。此方法适用于桌面版 Safari,但不适用于移动版 Safari。

As a workaround, I tried tying the load()/play() code to a click event, and then triggering the click event programmatically. This approach worked in Desktop Safari, but not Mobile Safari.

A君 2024-10-10 16:41:25

这在 iPad 上一直有效,直到昨晚 4.2 升级:

$(function() {
    var Beep = document.createElement('audio');
    Beep.setAttribute('src', 'beep-23.mp3');
    Beep.load();
    Beep.play();
}

我的 Beep.play(); 仍然适用于点击事件,但最初的 Beep.play() 停止工作。 ..

This worked on the iPad until last nights 4.2 upgrade:

$(function() {
    var Beep = document.createElement('audio');
    Beep.setAttribute('src', 'beep-23.mp3');
    Beep.load();
    Beep.play();
}

My Beep.play(); still works for a click event, but the initial Beep.play() stopped working...

抚笙 2024-10-10 16:41:25

这个怎么样?

使用 Sound Manager 2、预加载和创建音频精灵从第一次用户交互开始缓存它,然后在需要时播放。

那行得通吗?

How about this?

Create an audio sprite with Sound Manager 2, preload & cache it from the first user interaction, and then play as and when needed.

Would that work?

云巢 2024-10-10 16:41:25

使用耳机插孔的 iPhone 5 (iOS 6) 支持自动播放(在我的测试中)。

Autoplay is supported (in my tests) with an iPhone 5 (iOS 6) using the headphone jack.

赠我空喜 2024-10-10 16:41:25

在我运行 IOS 6 的第三代 iPad 上,HTML5 标签的自动播放突然开始工作了! (我已经安装了 IOS 6 几个星期了,我很确定我立即测试了这个功能,但直到今晚才发现它可以工作。)我不记得看到过 Apple 的任何通知,尽管我确实看到过移动 Safari 已被授予支持文件上传的能力

<form><input type="file" .../></form>

,这是我需要的另一个功能,但苹果拒绝了该功能。

On my 3rd-gen iPad running IOS 6, autoplay of HTML5 tags has suddenly begun working! (I've had IOS 6 installed for a couple of weeks, and am pretty sure I tested this feature right away, but only noticed it working tonight.) I don't recall seeing any notice of this from Apple, although I did see that mobile Safari had been granted the ability to support file uploads via

<form><input type="file" .../></form>

-- another feature I needed but which Apple had withheld.

度的依靠╰つ 2024-10-10 16:41:25

请尝试此代码,它适用于两个版本,您需要在页面中动态添加音频控件。它正在工作。

var isiPad = false;
if (navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/webOS/i)) { 
    isiPad = true; 
}
$(function() { 
    if (isiPad) { 
        var audioElement2 = document.createElement('audio'); 
        audioElement2.setAttribute('src', 'img/bgsound.mp3'); 
        audioElement2.controls = true; 
        audioElement2.loop = true; 
        audioElement2.load();
        audioElement2.play();
    }
});

Please Try this Code it is working both version, you need to add the audio control dynamically in page. it is working.

var isiPad = false;
if (navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/webOS/i)) { 
    isiPad = true; 
}
$(function() { 
    if (isiPad) { 
        var audioElement2 = document.createElement('audio'); 
        audioElement2.setAttribute('src', 'img/bgsound.mp3'); 
        audioElement2.controls = true; 
        audioElement2.loop = true; 
        audioElement2.load();
        audioElement2.play();
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文