当javascript复选框被选中时播放音频
Play audio when checkbox is checked with javascript
我试图使一个短音频文件播放时,一个html复选框被选中。我可能漏掉了一些基本的东西。下面是我的代码:
<body>
<input type="checkbox" id="cena" onchange="myfunction()"></input><label for="cena"></label>
</label><script>
function myfunction(){
var audio = new audio('rusbtaudio.mp3');
audio.play();
}
</script>
</body>
rusbtaudio.mp3
与html文件在同一文件夹
<input>
元素自闭。使用.load()
、canplay
事件。用new Audio()
代替new audio()
<body>
<input type="checkbox" id="cena" onchange="myfunction(this)" />
<label for="cena"></label>
<script>
var audio = new Audio('rusbtaudio.mp3');
audio.oncanplay = function() {
if (document.getElementById("cena").checked) this.play()
}
function myfunction(el) {
if (el.checked) {
audio.load();
}
}
</script>
</body>
JavaScript:
var audio = new Audio('rusbtaudio.mp3'),
cenaCheckbox = document.getElementById('cena'),
myfunction = function () {
if (cenaCheckbox.checked) {
audio.play();
}
};
Html: <input type="checkbox" id="cena" onchange="myfunction();"/>
你好,你应该试试这个
<input type="checkbox" id="cena" onclick="myfunction()"></input><label
for="cena"></label>
</label>
<script>
function myfunction(){
var audio = new Audio('sel.mp3');
audio.play();
}
你的代码几乎是正确的!唯一的问题在这一行:
new audio('rusbtaudio.mp3');
只需将audio
更改为Audio
-查看文档
也有一些问题与标签,如多余的</input>
和不需要的第二个</label>
,但即使它将工作。
你的代码有以下错误Uncaught TypeError: audio is not a constructor, try this code, native JavaScript with HTML5 audio tag
HTML<body>
<audio id="audio1" hidden="hidden">Canvas not supported</audio>
<input type="hidden" id="audioFile" value="rusbtaudio.mp3" size="60" />
<input type="checkbox" id="playbutton" onclick="togglePlay();">Play
</body>
JavaScript <script>
// Create a couple of global variables to use.
var audioElm = document.getElementById("audio1"); // Audio element
// Alternates between play and pause based on the value of the paused property
function togglePlay() {
if (document.getElementById("audio1")) {
if (audioElm.paused == true) {
playAudio(audioElm); // if player is paused, then play the file
} else {
pauseAudio(audioElm); // if player is playing, then pause
}
}
}
function playAudio(audioElm) {
// Get file from text box and assign it to the source of the audio element
audioElm.src = document.getElementById('audioFile').value;
audioElm.play();
}
function pauseAudio(audioElm) {
audioElm.pause();
}
</script>
我认为在复选框中添加暂停功能是值得的(并非所有用户都喜欢背景音乐)。我做到了
<body>
<input type="checkbox" id="cena" onchange="myfunction(this)" />
<label for="cena"></label>
<script>
var audio = new Audio('rusbtaudio.mp3');
audio.oncanplay = function() {
if (document.getElementById("cena").checked) this.play()
}
function myfunction(el) {
if (el.checked) {
audio.load();
} else {
audio.pause(); //pause audio
}
}
</script>
</body>
相关文章:
- 使用HTMLMediaElement向后播放音频
- 播放音频时创建一个跳动的圆圈
- Javascript播放音频后其他没有音频崩溃
- 在播放完成之前再次单击播放音频文件
- 延迟后播放音频标签
- 如何使用Flash CC画布项目在iPad上播放音频
- 使用javascript播放音频(没有HTML5或可见播放器)
- 如何使用Bootstrap和Heroku为Rails应用程序添加隐藏的播放音频
- 检测iOS移动设备的JavaScript比自动播放音频
- 在手机和平板电脑浏览器上播放音频片段
- 需要帮助修复我的javascript"对于循环“;播放音频播放列表
- 自动播放音频播放列表SoundManager2 bar ui
- 单击链接时播放音频剪辑
- 使用 Redux 播放音频文件
- 在 iPad 上延迟后正在播放音频
- 关闭后,引导模式弹出窗口上的视频继续播放音频
- 无法更改用于播放音频的变量
- 我在 html5 音频标签中播放音频时收到 [对象媒体错误]
- 无法使用谷歌应用引擎播放音频.它在本地工作
- 在 javascript 数组中播放音频文件时遇到问题