Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript报警不';不要播放声音文件_Javascript - Fatal编程技术网

Javascript报警不';不要播放声音文件

Javascript报警不';不要播放声音文件,javascript,Javascript,我现在正在使用localhost,我的笔记本电脑。我是唯一的用户 我为一个朋友做了一个在线考试。他问我是否可以在考试结束前5分钟发出铃声或留言,这样学生们就不用等太久再按“发送”键,否则他们将无法发送答案 我有这个javascript。有人告诉我这对他有用。看: 拒绝许可 我无法让Firefox重复此错误消息 我打不开 console.log({h})ReferenceError:未定义h 这是Firefox设置问题吗?我能换这个吗 或者以其他方式播放声音?因为指向型浏览器不允许播放声音,除非用

我现在正在使用localhost,我的笔记本电脑。我是唯一的用户

我为一个朋友做了一个在线考试。他问我是否可以在考试结束前5分钟发出铃声或留言,这样学生们就不用等太久再按“发送”键,否则他们将无法发送答案

我有这个javascript。有人告诉我这对他有用。看:

拒绝许可

我无法让Firefox重复此错误消息

我打不开

console.log({h})ReferenceError:未定义h

这是Firefox设置问题吗?我能换这个吗

或者以其他方式播放声音?

因为指向型浏览器不允许播放声音,除非用户与页面交互

解决此问题的一种方法是添加一个按钮,用户需要单击该按钮才能同意响铃提醒声音

例如:

//javascript
让startBtn=document.getElementById(“开始”)
startBtn.onclick=函数(e){
e、 预防默认值()
let count=document.getElementById(“count”)
let interval=window.setInterval(函数(){
count.textContent=--count.textContent;
如果(count.textContent==0){
间隔时间;
让bell=document.getElementById(“bell”)
bell.play()
}
}, 1000)
}

您的浏览器不支持音频元素。

5


5秒后提醒我,您的JSFIDLE对我有效,但在初次运行时无效。只有当我按下跑步按钮时。这是因为浏览器不会播放声音,除非您与页面交互(以避免烦人)。尝试添加一个播放声音的按钮。真的没有办法运行它吗?问题是,学生们正全神贯注于考试,他们可能不会注意时间。在临场考试中,监考人会说,“还剩5分钟!”我已经加载了视频开始的网页,没有我的同意或点击。谢谢!伟大的我试过你的代码,它会播放!在开始考试之前,我们将通知学生点击这个按钮,该按钮位于他们填写姓名和学号的部分之后。然后我会在55分钟后将其设置为播放。我还不知道该怎么做,但我想我能解决它!再次感谢!
<script>
const d = new Date();

// if later than h, play the sound. It's past 11am here now!
// set the minutes later if this works
var h = d.getHours();
console.log({h});
if (h > 7) {
    // I put a hidden input on the page, with the value of the sound file
    // <input type="hidden" id="musicloc" size="55" value="mp3/fullstop.mp3" /> 
    var sound = document.getElementById("musicloc").value;
    playSound(sound);
}

// this should make an audio element and then play the sound            
function playSound(soundFile) {
    var audioElement = document.createElement('audio');
    // maybe here the problem? What is 'src'?
    audioElement.setAttribute('src', soundFile);
    audioElement.play();

</script>
NotAllowedError: The play method is not allowed by the user agent or the platform in the current context, possibly because the user