浏览器中的HTML5可以在iOS锁定屏幕上播放连续音频吗? [英] Can HTML5 in the browser play continuous audio on iOS lock screen?

查看:65
本文介绍了浏览器中的HTML5可以在iOS锁定屏幕上播放连续音频吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力寻找答案,似乎没有一个一致的答案.如果我有一个网站尝试使用HTML 5音频元素连续播放多首歌曲(认为是播放列表),那么它可以在iOS锁定屏幕上继续运行吗?

I have been trying to track this down and there doesn't seem to be a consistent answer. If I have a website that tries to play multiple songs in a row (think playlist) using the HTML 5 audio element, can it continue to work on the iOS lock screen?

对于某些背景,此答案似乎表明它可能可能的.但随后

For some background, this answer seems to indicate it may be possible. But then this article suggests it is not.

我尝试遵循苹果推荐的最接近的示例,例如

I tried following the closest example of what Apple recommends, as found here, to replicate this. I am using plain, vanilla javascript and HTML, nothing fancy. I copied their example exactly and just substituted the audio tag for the video one, picking two random mp3 songs. All it does is wait for one song to end, then switch the src, load, and play the next track.

当我在网站上单击播放"时,我随后锁定了iPhone.第一首歌曲将播放,然后停止.它不会继续播放下一首歌曲.

When I hit Play on the website, I then lock the iPhone. The first song will play, then stop. It does not continue to the next song.

如果打开该页面的网站,它将正确过渡到下一首歌曲.在Android设备上,即使手机已锁定,它也会继续播放下一首歌曲.

If the website is open to the page, it will properly transition to the next song. On Android, it will continue to the next song even if the phone is locked.

我在iOS 11和12上进行了尝试.关于网站不在前台时如何停止javascript以及iOS如何要求用户交互才能播放音频(甚至从一首歌转到另一首歌),我已经读了许多不同的答案.因此,这似乎行不通.但是随后其他答案似乎表明这是可能的.

I tried this with iOS 11 and 12. Neither worked. I have read many differing answers about how javascript is stopped when the website isn't in the foreground, and how iOS requires user interaction to play audio (even going from one song to the next). So it doesn't seem like this would work. But then other answers out there seem to indicate this is possible.

是否有确定的是或否?我在这里做错什么了吗?还是不可能?

Is there a definitive yes or no? Am I doing something wrong here? Or is it just not possible?

推荐答案

存在多个问题,这引起了一些混乱.

There are multiple issues, which is causing some of the confusion.

如果第一首曲目是在获得用户许可的情况下开始的,那么通过脚本切换到新曲目的唯一方法是根据Apple的建议.处理 ended 事件并交换新的 src ,并在回调完成之前调用 .play().否则,您的代码将无权启动新音频.

If the first track was started with user permission, then the only way you can switch to a new track via script is with Apple's recommendation. Handle the ended event and swap in a new src, and call .play() before the callback completes. Otherwise, your code will not have permission to start the new audio.

假设您有一个30秒的音频文件,并且告诉浏览器仅通过网址末尾的#t = 5,10 之类的内容加载5到10秒.当您达到 10 时,将触发 paused 事件,而不是 end .因此,无法正常进行下一个曲目,因为Apple不能将此事件视为上一次用户交互的转播.

Say you have a 30-second audio file and you tell the browser to only load seconds 5 through 10, via something like #t=5,10 at the end of the URL. When you reach 10, a paused event will fire, not ended. Therefore, it isn't possible to go on to the next track normally, as this event is not "blessed" by Apple to count as a relay of the previous user interaction.

我通过让我的JavaScript反复检查 currentTime 来解决此问题,并且当它超过阈值时,请自行查找文件的 duration (结尾).这样可以使 end 正常启动.

I got around this by having my JavaScript check the currentTime repeatedly, and when it crossed a threshold, seek it to the duration (end) of the file myself. This allows ended to fire normally.

这是一个真正的调试问题,因为在任何模拟器中都不会出现.无论如何,您是对的,您的JavaScript将被暂停,因此,即使您遵循Apple的建议,也很不走运.或者,是吗?

This one is a real problem to debug, since it doesn't turn up in any of the simulators. In any case, you're right, your JavaScript is going to get suspended so even if you follow Apple's recommendations, you're out of luck. Or, are you?

黑客...在Web Audio上下文中设置ScriptProcessorNode.将缓冲区大小设置为512个样本之类.然后,在脚本过程中,为您的音频元素强制一个 timeupdate 事件.这样可以使您的JavaScript停滞不前.本质上,ScriptProcessorNode在这里必须具有特殊的特权.

A hack... set up a ScriptProcessorNode on a Web Audio context. Set the buffer size to like 512 samples or something. Then, on script process, force a timeupdate event for your Audio Element. This keeps your JavaScript there ticking away. ScriptProcessorNode has to have special privileges here, by its nature.

Apple :如果您阅读此书,请修复浏览器.如果我们不必为解决音频播放列表之类的简单操作而烦恼,那就太好了.其他浏览器几乎没有这种级别的问题.

Apple: If you read this, please fix your browser. It would be great if we didn't have to hack around things to make something as simple as an audio playlist to work. Other browsers don't have nearly this level of problems.

这篇关于浏览器中的HTML5可以在iOS锁定屏幕上播放连续音频吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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