gpt4 book ai didi

javascript - 检测有关音频/视频play()命令的当前上下文

转载 作者:行者123 更新时间:2023-12-03 01:12:59 25 4
gpt4 key购买 nike

因此,当我们尝试使用myVideoElement.play()时,会在控制台中收到此错误:Uncaught (in promise) DOMException: The play method is not allowed by the user agent or the platform in the current context, possibly because the user denied permission.JavaScript是否可以检测或测试我们所处的上下文以防止例如调用play()?我不希望在控制台中显示警告或错误消息。

最佳答案

如果您担心控制台中的错误,则可以捕获该错误。

async function yourFunction()
{
try
{
await myVideoElement.play();
// if the script reaches here, it succeeded
console.log("success");
}
catch(error)
{
// if it failed it will come here, but will not put an error in the console,
// so you can handle it however you want or just ignore it and nothing will happen
}
}

yourFunction()
查看更多: https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/play

关于javascript - 检测有关音频/视频play()命令的当前上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63241448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com