简体   繁体   English

如何处理Uncaught(在promise中)DOMException:play()请求被pause()调用中断

[英]How to handle Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause()

Below is my code in aspx page to allow playing audio's of wav format in the browser but with my current code I am unable to play wav audios in Chrome browser but it works in Firefox. 下面是我在aspx页面中的代码,允许在浏览器中播放wav格式的音频但是使用我当前的代码我无法在Chrome浏览器中播放wav音频,但它可以在Firefox中使用。 How can I handle this exception? 我该如何处理这个异常?

<script>
    window.onload = function () { document.getElementById("audio").play(); }
    window.addEventListener("load", function () { document.getElementById("audio").play(); });
</script>

<body>
    <audio id='audio' controls autoplay>
        <source src="Sounds/DPM317.wav" type="audio/wav" />
        Your browser does not support the audio element.
    </audio>
</body>

For Chrome they changed autoplay policy, so you can read about here : 对于Chrome,他们更改了自动播放政策,因此您可以在此处阅读:

var promise = document.querySelector('audio').play();

if (promise !== undefined) {
    promise.then(_ => {
        // Autoplay started!
    }).catch(error => {
        // Autoplay was prevented.
        // Show a "Play" button so that user can start playback.
    });
}

I don't know if this is still actual for you, but I still leave my comment so maybe it will help somebody else. 我不知道这对你来说是否仍然存在,但我仍然留下我的评论,所以也许它会帮助别人。 I had same issue, and the solution proposed by @dighan on bountysource.com/issues/ solved it for me. 我有同样的问题,@ dighan在bountysource.com/issues/上提出的解决方案为我解决了这个问题。

So here is the code that solved my problem: 所以这是解决我的问题的代码:

var media = document.getElementById("YourVideo");
const playPromise = media.play();
if (playPromise !== null){
    playPromise.catch(() => { media.play(); })
}

It still throws an error into console, but at least the video is playing :) 它仍然向控制台抛出错误,但至少视频正在播放:)

Try using a callback like this with the catch block. 尝试使用catch块进行这样的回调。

document.getElementById("audio").play().catch(function() {
    // do something
});
  1. All new browser support video to be auto-played with being muted only so please put 所有新的浏览器支持视频只能自动播放,请放置

<video autoplay muted="muted" loop id="myVideo"> <source src="https://wrglob.net/Coastline-3581.mp4" type="video/mp4"> </video>

Something like this 像这样的东西

  1. URL of video should match the SSL status if your site is running with https then video URL should also in https and same for HTTP 如果您的网站使用https运行,则视频的网址应与SSL状态匹配,然后视频网址也应采用https,HTTP也应相同

muted="muted"属性添加到HTML5标签解决了我的问题

I am using Chrome version 75. 我使用的是Chrome 75版。

add the muted property to video tag 将静音属性添加到视频标记

<video id="myvid" muted>

then play it using javascript and set muted to false 然后使用javascript播放它并将muted设置为false

var myvideo = document.getElementById("myvid");
myvideo.play();
myvideo.muted = false;

edit: need user interaction (at least click anywhere in the page to work) 编辑:需要用户交互(至少点击页面中的任意位置才能工作)

I second Shobhit Verma, and I have a little note to add : in his post he told that in Chrome (Opera for myself) the players need to be muted in order for the autoplay to succeed... And ironically, if you elevate the volume after load, it will still play... It's like all those anti-pop-ups mechanic that ignore invisible frame slid into your code... php-echoed html and javascript is : 10-second setTimeout onLoad of body tag that rises volume to maximum, video with autoplay and muted='muted' (yeah that $muted_code part is = "muted='muted") 我是第二个Shobhit Verma,我有一个小小的补充说:在他的帖子中他告诉我在Chrome(自己为Opera)中玩家需要静音才能使自动播放成功...而且具有讽刺意味的是,如果你提升了加载后的音量,它仍然会播放...就像所有那些忽略隐形框架的反弹出机制滑入你的代码...... php-echoed html和javascript是:10秒setTimeout onLoad的body标签上升音量到最大,带自动播放的视频和静音='静音'(是的,$ muted_code部分=“静音='静音”)

echo "<body style='margin-bottom:0pt; margin-top:0pt; margin-left:0pt; margin-right:0pt' onLoad=\"setTimeout(function() {var vid = document.getElementById('hourglass_video'); vid.volume = 1.0;},10000);\">";
    echo "<div id='hourglass_container' width='100%' height='100%' align='center' style='text-align:right; vertical-align:bottom'>";
    echo "<video autoplay {$muted_code}title=\"!!! Pausing this video will immediately end your turn!!!\" oncontextmenu=\"dont_stop_hourglass(event);\" onPause=\"{$action}\" id='hourglass_video' frameborder='0' style='width:95%; margin-top:28%'>";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 视频错误 - 未捕获(承诺)DOMException:调用 pause() 中断了 play() 请求 - Video error - Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause() 如何防止错误“未捕获(承诺)DOMException:播放()请求被暂停()调用中断。”? - How do I prevent the error "Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause()."? Vegas 2滑块加载到两个元素的错误-未捕获(承诺)DOMException:play()请求被对pause()的调用中断了 - Vegas 2 slider loading to two elements bug - Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause() 调用play()中断了play()请求 - The play() request was interrupted by a call to pause() DOMException: play() 请求被中断 - DOMException: The play() request was interrupted 如何修复“Uncaught(in promise)DOMException:play()” - how to fix “Uncaught (in promise) DOMException: play() ” 如何防止“播放()请求被暂停()调用中断”错误? - How to prevent "The play() request was interrupted by a call to pause()" error? AbortError:play()请求被对pause()的调用中断了 - AbortError: The play() request was interrupted by a call to pause() 如何在ajax调用中避免Uncaught(in promise)DOMException? - How to avoid Uncaught (in promise) DOMException on ajax call? 如何处理“未捕获(承诺)DOMException:play() 失败,因为用户没有先与文档交互。”? - How to handle “Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first.”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM