gpt4 book ai didi

javascript - 如何为浏览器音频播放器添加静音/取消静音按钮?

转载 作者:行者123 更新时间:2023-11-30 15:46:38 25 4
gpt4 key购买 nike

所以我有我的代码,它工作得很好。唯一的问题是没有静音/取消静音按钮,我不知道如何添加它们。

<audio src="MUSIC URL" autoplay loop>
<p>Your browser does not support the audio element </p>
</audio>

最佳答案

我使用它,获取播放和暂停按钮 .png 并使用它们。然后创建一个名为 audio 的文件夹并将文件放在那里供您播放音乐。我将播放和暂停放在一个名为 images 的文件夹中。

<style type="text/css">
audio {visibility: hidden; height: 0; width: 0; position: absolute; top: -300px; left: -300px;}
#pauseplay {float: right; width: 48px; height: 48px; overflow: hidden; cursor: pointer}
</style>

<audio controls loop>
<source src="**your website url here**/audio/waves.ogg" type="audio/ogg">
<source src="**your website url here**/audio/waves.mp3" type="audio/mpeg">
</audio>
<img id="pauseplay" src="**your website url here**/images/play.png" alt="button" title="Play/Pause">
<script type="text/javascript">
(function(){
var playing = !!('ontouchstart' in window) || !!('ontouchstart' in document.documentElement) || !!window.ontouchstart || (!!window.Touch && !!window.Touch.length) || !!window.onmsgesturechange || (window.DocumentTouch && window.document instanceof window.DocumentTouch),
snd = document.getElementsByTagName('audio')[0],
ctrl = document.getElementById('pauseplay');
playing = !playing;
ctrl.title = playing? 'Pause' : 'Play';
if(playing){snd.autoplay = false; ctrl.src = '**your website url** here/images/pause.png';}
ctrl.addEventListener('click', function(){
if(playing){
snd.pause();
} else {
snd.play();
}
playing = !playing;
ctrl.title = playing? 'Pause' : 'Play';
ctrl.src = playing? '**your website url here**/images/pause.png' : '**your website url here**/images/play.png';
}, false);
})();
</script>

如果你想现场看 l ook at this site我做了。

关于javascript - 如何为浏览器音频播放器添加静音/取消静音按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39964279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com