为什么 JavaScript .play() 不能在 iPhone Safari 上播放音频文件? [英] Why can't JavaScript .play() audio files on iPhone safari?

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

问题描述

我有一个 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?

推荐答案

iOS 禁用自动播放,而是要求将播放作为用户交互的一部分启动(例如,您可以在 touchstart 侦听器中开始播放).Apple 的开发者文档.IBM 的开发人员站点上还有这篇文章克服 iOS HTML5 音频限制,其中包含示例以及更多细节.

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.

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

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