使用Mocha在浏览器中测试音频需要用户交互

问题描述

我在浏览器中运行了一些Mocha测试,到目前为止,这些测试都运行良好-但我现在正尝试将其扩展以测试某些WebAudio。不幸的是,当我运行测试时,控制台中显示以下内容

The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.

从浏览器中肯定是正确的行为,但它使我的测试无法继续进行。

有没有办法解决这个问题?我当时想在页面上放置一个按钮,并在点击处理程序后面添加mocha.run();命令。

有没有更优雅的方式做到这一点?

解决方法

您提到的错误消息是在Chrome中登录的错误消息,这就是为什么我猜您正在使用Chrome进行测试的原因。您可以设置--autoplay-policy标志来更改Chrome中的默认行为。

--autoplay-policy=no-user-gesture-required

这将允许AudioContext在没有任何用户交互的情况下启动。

请查看standardized-audio-context的测试配置,以获取使用Mocha和Karma进行测试的完整示例。

https://github.com/chrisguttandin/standardized-audio-context/blob/master/config/karma/config-unit.js#L136