如何在 iOS >= 4.2.1 Mobile Safari 中自动播放媒体?
除了用户执行的单击事件的处理程序之外,我似乎无法在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
从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.
在 4.2.1 上有一种方法可以自动播放视频/音频文件。创建一个 iframe 并将其源设置为媒体文件的 URL,并将 iframe 附加到正文。将会自动播放。
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.
正如其他人所说,根据 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
作为解决方法,我尝试将 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.
这在 iPad 上一直有效,直到昨晚 4.2 升级:
我的
Beep.play();
仍然适用于点击事件,但最初的Beep.play()
停止工作。 ..This worked on the iPad until last nights 4.2 upgrade:
My
Beep.play();
still works for a click event, but the initialBeep.play()
stopped working...这个怎么样?
使用 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?
使用耳机插孔的 iPhone 5 (iOS 6) 支持自动播放(在我的测试中)。
Autoplay is supported (in my tests) with an iPhone 5 (iOS 6) using the headphone jack.
在我运行 IOS 6 的第三代 iPad 上,HTML5 标签的自动播放突然开始工作了! (我已经安装了 IOS 6 几个星期了,我很确定我立即测试了这个功能,但直到今晚才发现它可以工作。)我不记得看到过 Apple 的任何通知,尽管我确实看到过移动 Safari 已被授予支持文件上传的能力
,这是我需要的另一个功能,但苹果拒绝了该功能。
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
-- another feature I needed but which Apple had withheld.
请尝试此代码,它适用于两个版本,您需要在页面中动态添加音频控件。它正在工作。
Please Try this Code it is working both version, you need to add the audio control dynamically in page. it is working.