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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4259928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 05:24:46  来源:igfitidea点击:

How can I autoplay media in iOS >= 4.2.1 Mobile Safari?

iphoneipadhtmliosmobile-safari

提问by David Gouldin

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.

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

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.

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

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

所有在 iOS 4.2.1 之前有效的漏洞似乎都已关闭。有任何想法吗?

采纳答案by Yu Jianrong

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

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

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

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

回答by bhups

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.

有一种方法可以在 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);

回答by Michael Johnston

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).

正如其他人在这里所说的,根据 Apple 文档,MobileSafari 不支持 video 标签中的 autoplay 属性。但是,在 iOS 6 中它确实有效。这似乎是 Apple 在 iOS 6.0.1(大概是在 iOS 6.1)中修复的错误。

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

如果您偶然发现它在 iOS 6.0 中工作,请不要依赖于在 iOS 6 中工作的自动播放。

-Michael

-迈克尔

回答by mshafrir

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.

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

回答by steve

This worked on the iPad until last nights 4.2 upgrade:

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

$(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...

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

回答by mousedown

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.

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

Would that work?

那行得通吗?

回答by Jeremiah Isaacson

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

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

回答by dkurland

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

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

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

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

——我需要的另一个功能,但苹果公司拒绝了。

回答by Ahammad Firoj

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();
    }
});