当前位置: 首页 > Web前端 > JavaScript

audio.play报错,UnhandledRejection(NotAllowedError)-

时间:2023-03-27 01:04:50 JavaScript

展望:框架:使用umijs比较综合框架,react栈。问题展望:关于无法自动播放音频,这里不再赘述。我的问题是我在iPhone(v11.3.x)上点击了按钮(与页面交互),但是还是报UnhandledRejection(NotAllowedError):Therequestisnotallowedbytheuseragentortheplatforminthecurrent上下文,可能是因为用户拒绝了权限。问题排查:1、经过不断排查,交互中audio.play()没有问题。下面是正常播放{clickEgg(1)}}>playconstclickEgg=(index)=>{letaudioEle=document.getElementById('bg-music')audioEle。玩()};在报错的情况下,很多时候,我们需要请求接口播放某种声音。就是这个同步请求报错{clickEgg(1)}}>playletaudioEle=document.getElementById('bg-music')constclickEgg=async(index)=>{//这是一个请求constisLogin=awaitgetIsLoginAsync().catch(err=>{setShowLoginDialog(true)})if(!isLogin){setShowLoginDialog(true)return;}audioEle.play()};解决问题:{clickEgg(1)}}>PlayletaudioEle=document.getElementById('bg-music')constclickEgg=async(index)=>{audioEle.muted=trueletp=audioEle.play()//这里有请求constisLogin=awaitgetIsLoginAsync().catch(err=>{setShowLoginDialog(true)})if(!isLogin){setShowLoginDialog(true)return;}//为了兼容iphoneif(p!==undefined){p.then(()=>{audioEle.muted=falseaudioEle.pause()audioEle.currentTime=0;setTimeout(()=>{audioEle.play()},0)}).catch((e)=>{console.log(e)})}};参考:https://juejin.cn/post/684490...这篇文章写的很全面,很有参考价值