检测音频是否在网站上播放

发布于 2025-02-04 02:05:44 字数 790 浏览 1 评论 0原文

这在某种程度上是这个问题的重复性不了解如何使用接受的答案。此外,这个问题很旧,也许现在有一种更好的方法。

我的用例:

我想测试一般是否与剧作家一起演奏音频。音频源不是DOM元素,因此检查媒体元素不是解决方案。

这就是代码的使用方式:

const isAudioPlaying = page.evaluate(() => {
    /* Code goes here */
})

编辑: 这就是媒体播放的代码在其最简单的形式中的样子:

function createAudioPlayer() {
    const audio = new Audio()

    return {
      setSource: (src) => audio.src = src,
      play: () => audio.play,
      pause: () => audio.pause,
      resume: () => audio.resume,
    }
}

该工厂仅将默认的音频构造函数用于播放。它永远不会像那样接触DOM。

This is somewhat a duplicate of this question, but I do not understand on how to use the accepted answer. Furthermore the question is quite old and maybe there is a better way available now.

My use case:

I want to test if audio in general is playing with Playwright. The audio source is not a DOM-Element, so checking media elements is not a solution.

This is how the code would get used:

const isAudioPlaying = page.evaluate(() => {
    /* Code goes here */
})

Edit:
And this is how the code of the media playback looks like in its most simple form:

function createAudioPlayer() {
    const audio = new Audio()

    return {
      setSource: (src) => audio.src = src,
      play: () => audio.play,
      pause: () => audio.pause,
      resume: () => audio.resume,
    }
}

This factory just uses the default Audio constructor for the playback. It never touches the DOM like that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文