为什么 JavaScript .play() 不能在 iPhone Safari 上播放音频文件?

IT技术 javascript ios iphone audio safari
2021-02-21 17:16:29

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

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

这在桌面浏览器上效果很好(在 Edge 和 Chrome 中测试),但它不能在 iPhone 上的 Safari 上播放。

我环顾了 Stack Overflow,几年前我找到了一些答案,除非您在全屏播放器中,否则无法从 Safari 播放音频。现在还是这样吗?

6个回答

iOS 禁用自动播放,而是要求将播放作为用户交互的一部分启动(例如,您可以在 touchstart 侦听器中开始播放)。Apple's developer documentation上有一些关于此的文档IBM 的开发人员站点上还有这篇文章克服 iOS HTML5 音频限制,其中包含示例和更多详细信息。

我想补充一点,iframe 也无法解决这个问题。我试过这个。
2021-05-08 17:16:29
死链接。唉...
2021-05-15 17:16:29

要添加到 xingliang cai 的回复中,这是我必须为我工作的代码示例(在下面编辑以在 iOS14 上工作,感谢@AndrewL!):

const soundEffect = new Audio();
soundEffect.autoplay = true;

// onClick of first interaction on page before I need the sounds
// (This is a tiny MP3 file that is silent and extremely short - retrieved from https://bigsoundbank.com and then modified)
soundEffect.src = "data:audio/mpeg;base64,SUQzBAAAAAABEVRYWFgAAAAtAAADY29tbWVudABCaWdTb3VuZEJhbmsuY29tIC8gTGFTb25vdGhlcXVlLm9yZwBURU5DAAAAHQAAA1N3aXRjaCBQbHVzIMKpIE5DSCBTb2Z0d2FyZQBUSVQyAAAABgAAAzIyMzUAVFNTRQAAAA8AAANMYXZmNTcuODMuMTAwAAAAAAAAAAAAAAD/80DEAAAAA0gAAAAATEFNRTMuMTAwVVVVVVVVVVVVVUxBTUUzLjEwMFVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVf/zQsRbAAADSAAAAABVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVf/zQMSkAAADSAAAAABVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV";

// later on when you actually want to play a sound at any point without user interaction
soundEffect.src = 'path/to/file.mp3';
不幸的是,这个答案不再适用于 iOS 14。
2021-04-24 17:16:29
优秀的。在 Angular 中也能很好地工作。
2021-04-26 17:16:29
谢谢,更新了我对您的解决方案的回答,@AndrewL!
2021-04-26 17:16:29
这在 Safari 版本 14.0.3 上不再适用于我:/ 返回经典 Unhandled Promise Rejection: AbortError: The operation was aborted.
2021-05-04 17:16:29
这救了我的命。谢谢你。
2021-05-19 17:16:29

移动端 IOS 默认禁用自动声音播放。所以要解决这个问题。如果用户单击按钮开关,您可以将启用/禁用开关按钮放在页面上的某个位置,并使用音频元素(“ audioElement ”)播放一些声音

之后,可以使用相同的“ audioElement ”通过更改其“ src ”属性并调用其“ play() ”方法来播放未来的声音,而无需任何进一步的用户交互。

我发现这个答案的第二部分很有用 - 您可以更改Audio(x).src和呼叫,play()但不能在play()没有用户交互的情况下创建新的 Audio 对象和呼叫
2021-05-11 17:16:29
令人惊叹的。非常感谢您对此进行调查和解决!new Audio()在用户交互上创建一个并存储它,然后更改该对象.src并对其进行调用.play()(在我的情况下使用 React 和 useRef)。干杯
2021-05-14 17:16:29

为了让@user2415116 的解决方案在 iOS 14 中工作,我这样做了:

const soundEffect = new Audio();
soundEffect.autoplay = true;

// onClick of first interaction on page before I need the sounds
// (This is a tiny MP3 file that is silent and extremely short - retrieved from https://bigsoundbank.com and then modified)
soundEffect.src = "data:audio/mpeg;base64,SUQzBAAAAAABEVRYWFgAAAAtAAADY29tbWVudABCaWdTb3VuZEJhbmsuY29tIC8gTGFTb25vdGhlcXVlLm9yZwBURU5DAAAAHQAAA1N3aXRjaCBQbHVzIMKpIE5DSCBTb2Z0d2FyZQBUSVQyAAAABgAAAzIyMzUAVFNTRQAAAA8AAANMYXZmNTcuODMuMTAwAAAAAAAAAAAAAAD/80DEAAAAA0gAAAAATEFNRTMuMTAwVVVVVVVVVVVVVUxBTUUzLjEwMFVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVf/zQsRbAAADSAAAAABVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVf/zQMSkAAADSAAAAABVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV";

// later on when you actually want to play a sound at any point without user interaction
soundEffect.src = 'path/to/file.mp3';

我用过这个:

useEffect(() => {
  window.addEventListener('touchstart', () => {
    document.getElementById('audio').muted = false
    document.getElementById('audio').play()
  })
})

只要用户滚动,声音就会播放