为什么iPhone Safari上没有JavaScript .play()音频文件? [英] Why can't JavaScript .play() audio files on iPhone safari?

查看:386
本文介绍了为什么iPhone Safari上没有JavaScript .play()音频文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JavaScript网络应用程序可以像这样定期播放一些音频:

I've got a JavaScript web app working that plays some audio periodically like this:

var SOUND_SUCCESS = new Audio('success.mp3');
SOUND_SUCCESS.play();

这适用于桌面浏览器(在Edge和Chrome中测试过),但它不能播放iPhone上的Safari。

This works great on desktop browsers (tested in Edge and Chrome), but it doesn't play on Safari on iPhone.

我看了看Stack Overflow,几年前我发现一些答案,除非你'不能播放Safari的音频在那个全屏播放器中。这仍然是这种情况吗?

I've looked around Stack Overflow, and I found some answers from a couple of years ago that it's not possible to play audio from Safari unless you're in that full screen player. Is this still the case?

推荐答案

iO禁用自动播放,而是要求播放作为用户交互的一部分启动(例如,你可以在touchstart监听器中开始播放)。关于 Apple的开发人员文档<有一些关于此的文档/ A>。还有一篇文章克服iOS HTML5音频限制在IBM的开发者网站上,其中包含示例和更多细节。

iOs disables autoplay, instead requiring that play be initiated as part of a user interaction (e.g., you can start playback within a touchstart listener). There's a bit of documentation about this on Apple's developer documentation. There's also this article Overcoming iOS HTML5 audio limitations on IBM's developer site that has examples and more detail.

这篇关于为什么iPhone Safari上没有JavaScript .play()音频文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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