为什么刷新页面后我的Javascript音频不起作用? [英] Why does my Javascript audio not work after I refresh a page?

查看:41
本文介绍了为什么刷新页面后我的Javascript音频不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的javascript中有一个音频对象,并调用了play函数.当我第一次进入该页面或通过其他页面之一的链接进入该页面时,效果很好,但是当我进入带有音频的页面并且只想刷新页面时,出现音频错误,

I have an audio object in my javascript and a call to the play function. It works fine when the I go to the page for the first time or go there through a link from one of my other pages, BUT when I'm on the page with the audio and just want to refresh the page I get audio errors,

Uncaught (in promise) DOMException

我读了一篇关于某人的文章,该人的音频只有在刷新几次后才能工作(与我的问题相反).而且我已经读过有关该错误的信息.但是我要做的就是刷新页面,并使事情像第一次一样工作.

I have read a post about someone whose audio only works after they refresh it a few times (the opposite of my problem). And I've read about that error. But all I'm trying to do is refresh the page and have things work like they worked the first time.

console.log("playing test sound");
let test_sound = new Audio('/static/audio/to-the-point.mp3');
test_sound.play();

这在我第一次转到页面时有效,但是当我刷新页面时出现错误,并且无论刷新多少次,音频都无法工作...有什么想法吗?

This works when I first go to the page, but then gives me that error when I refresh it and the audio doesn't work no matter how many more times I refresh... any ideas?

在进行更多操作后,刷新时音频出现错误的时间非常不一致.有时,刷新会很好,而有时会出现错误.一旦获得错误,随后的刷新也将获得错误.也许是我的Chrome浏览器或Mac计算机?

upon playing around with it more, the times that the audio gets the error is very inconsistent upon refreshing. Sometimes, the refresh is fine other times it just gets an error. Once it gets the error though, subsequent refreshes also get it. Maybe it's my chrome browser or mac computer?

推荐答案

根据我自己的研究和@Kokodoko的帮助,看来您必须在允许音频之前与网页进行交互.这就是为什么当我从另一个页面访问时它对我有用,但是在刷新时却失败了的原因.

Upon my own research and help from @Kokodoko it appears that you have to interact with your webpage before audio is allowed. This is why it worked for me when I came from another page, but failed when I refreshed.

解决方案:使用户互动.不幸的是,在用户进行交互之前,没有真正的解决方法可以让音频播放.迫使用户在音频开始之前以某种方式进行交互,这是使音频正常工作的唯一方法.

The solution: make the user interact. Unfortunately, there is no true workaround for letting the audio play before the user has interacted. Forcing the user to interact in some way before the audio starts, is the only way to get the audio to work

这篇关于为什么刷新页面后我的Javascript音频不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆