【问题标题】:How do I use a bolean to stop one music while the other plays如何使用布尔值在另一种音乐播放时停止一种音乐
【发布时间】:2021-10-19 23:23:51
【问题描述】:

我有两个使用 js 的按钮播放和停止的冥想音乐。问题是如果一个播放,另一个在点击时也会播放。

我希望一个在单击另一个时停止播放。我正在学习布尔值。有没有办法使用布尔值来做到这一点?

<div class="main-box">
  
<div class="slot1"> 
  
<p class="line line1">this is the long music</p>

<audio id=rollSound loop>
  <source src="sounds/long-medi.mp3" type="audio/mpeg">
  <source src="sounds/long-medi.ogg" type="audio/ogg">
  Your browser does not support the sound files. Please proceed by using other solutions.
  <!--the obove text will only show if the users' browser does not play the two files. -->
</audio>
<button id=play>play/stop</button>

<script>
const rollSound = document.getElementById('rollSound');
const btn = document.getElementById('play');

btn.addEventListener("click", e => rollSound.paused ? rollSound.play() : rollSound.pause());
                                                                    
</script>
</div>



<div class="slot2"> 
  <p class="line line2">this is for short music</p>

  <audio id=shortsound loop>
  <source src="sounds/short-medi.mp3" type="audio/mpeg">
  <source src="sounds/short-medi.ogg" type="audio/ogg">
  Your browser does not support the sound files. Please proceed by using other solutions.
</audio>
<button id=roll>play/stop</button>

<script>
const shortsound = document.getElementById('shortsound');
const btn2 = document.getElementById('roll');

btn2.addEventListener("click", e => shortsound.paused ? shortsound.play() : shortsound.pause());



</script>

<script>
const rollSoundEl = document.getElementById('rollSound');
const shortsoundEl = document.getElementById('shortsound');
const btn = document.getElementById('play');
const btn2 = document.getElementById('roll');

function playOneSound(soundToPlay, soundToPause) {
    soundToPlay.paused ? soundToPlay.play() : soundPlay.pause()
    soundToPause.pause()
}

btn.addEventListener("click", () => playOneSound(rollSoundEl, shortsoundEl));
btn2.addEventListener("click", () => playOneSound(shortsoundEl, rollSoundEl));

  </script>

</div>
  




</div> <!--main-box-->

【问题讨论】:

    标签: javascript html audio boolean


    【解决方案1】:

    声明变量

    const rollSoundEl = document.getElementById('rollSound');
    const shortsoundEl = document.getElementById('shortsound');
    const sounds = {shortsound: shortsoundEl,rollsound: rollSoundEl}
    const btn = document.getElementById('play');
    const btn2 = document.getElementById('roll');
    

    写逻辑

    function playOneSound(soundToPlay, soundToPause) {
        const soundEl = sounds[soundToPlay]
        const soundToPauseEl = sounds[soundToPause]
        soundEl.paused ? soundEl.play() : soundEl.pause()
        soundToPauseEl.pause()
    }
    

    实现逻辑

    btn.addEventListener("click", () => playOneSound('rollsound', 'shortsound'));
    btn2.addEventListener("click", () => playOneSound('shortsound', 'rollsound'));
    

    另一种方式

    声明变量

    const rollSoundEl = document.getElementById('rollSound');
    const shortsoundEl = document.getElementById('shortsound');
    const btn = document.getElementById('play');
    const btn2 = document.getElementById('roll');
    

    写逻辑

    function playOneSound(soundToPlay, soundToPause) {
        soundToPlay.paused ? soundToPlay.play() : soundPlay.pause()
        soundToPause.pause()
    }
    

    实现逻辑

    btn.addEventListener("click", () => playOneSound(rollSoundEl, shortsoundEl));
    btn2.addEventListener("click", () => playOneSound(shortsoundEl, rollSoundEl));
    

    编辑

    这是link to a working codesandbox

    我认为它不起作用的原因是我之前打错了:

    soundToPlay.paused ? soundToPlay.play() : soundPlay.pause()

    应该是:

    soundToPlay.paused ? soundToPlay.play() : soundToPlay.pause()

    附言

    由于(新式)浏览器规范,必须先刷新页面,然后才能播放嵌入在页面中的声音。

    【讨论】:

    • 这很好用,非常感谢。我可以请知道为什么有一个声音变量,为什么他们是这样声明的?你不能用shortsound和rollsound代替新的命名吗?
    • 很高兴我的解决方案适合您,声音对象用于动态访问声音元素。基本上,它让您只将名称作为参数而不是整个元素发送给函数。你当然也可以这样做。
    • 我根据您的建议添加了另一种编写代码的方法。随意选择其中一个。
    • 对此我感激不尽。我更了解替代版本,但它并没有像预期的那样工作。
    • 您是否遇到任何错误?您可以在更改后使用更新信息编辑您的问题,我会尽力提供帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-12
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多