简体   繁体   English

html5音频使用javascript设置音量无效

[英]html5 audio Setting volume with javascript not effective

I have this simple tiny .mp3 that has to be played on new message.我有这个简单的小 .mp3,必须在新消息上播放。 It plays well but i can't seam to control the volumelevel of it.它播放得很好,但我无法控制它的音量。 I've this Jfidle http://jsfiddle.net/Ls9ef0zo/14/ prepared with a visual audio element.我有这个 Jfidle http://jsfiddle.net/Ls9ef0zo/14/准备了一个视觉音频元素。 Ofcourse i don't want it to be visible in the real environment.当然,我不希望它在真实环境中可见。

 <input id="volumeslider" type="range" min="0" max="1" step="0.1" value="0.5"
  oninput="outputUpdate(value)"/>                                                       


  var newmsgsound = document.createElement('audio');
  newmsgsound.setAttribute('src',
  http://chat.transonly.nl/sounds/sound_1.mp3');
  newmsgsound.setAttribute('id', 'newmsgsound');
  newmsgsound.setAttribute('controls', 'controls');
  body.appendChild(newmsgsound);

  function outputUpdate(vol) {
  var audiolevel = document.getElementById('newmsgsound');
  audiolevel.volume=vol;
  }

edit Not sure where to update the question as partialy answered so i'll do it here?编辑不确定在哪里更新问题作为部分回答所以我会在这里做?

Thanks to the anwers provided the audio-element actually responds now to the slider-event however!..In my own environment (Not Fidle) The sound played by the audio-element is not affected by it.感谢提供音频元素现在实际上响应滑块事件的答案!..在我自己的环境中(非小提琴)音频元素播放的声音不受它的影响。 I can see it is responding.我可以看到它正在响应。 Even when i set it to 'Mute' on the audio-element itself the sound gets played out loud.即使我在音频元素本身上将它设置为“静音”,声音也会被大声播放。 Isn't that ought?这不是应该的吗?

edit2* I should be very ashamed now!!! edit2*我现在应该很惭愧!!! I was hearing the sound of a browser i'd hidden and was sending test messages to that one.我听到了我隐藏的浏览器的声音,并且正在向那个浏览器发送测试消息。 :-) It's ok now! :-) 现在好了!

Update:更新:

Your code is fine.你的代码没问题。 You need to define the outputUpdate() function before referring it in volumeslider. 在volumeslider 中引用它之前,您需要定义outputUpdate() 函数。 You simply need to tweak jsfiddle to have "no wrap in body".您只需要调整 jsfiddle 即可“没有包裹体”。

 <script>
     var newmsgsound = document.createElement('audio');
     newmsgsound.setAttribute('src', 'http://chat.transonly.nl/sounds/sound_1.mp3');
     newmsgsound.setAttribute('id', 'newmsgsound');
     newmsgsound.setAttribute('controls', 'controls');
     document.body.appendChild(newmsgsound);

     function outputUpdate(vol) {      
        var audiolevel = document.getElementById('newmsgsound');
        audiolevel.volume=vol;
    }   
    </script>

    <input id="volumeslider" type="range" min="0" max="1" step="0.1" oninput="outputUpdate(value)"/>    

Here's the plunkr这是plunkr

You function output update is not used now.现在不使用您的函数输出更新。

try something like this.尝试这样的事情。

 var newmsgsound = document.createElement('audio');
 newmsgsound.setAttribute('src', 'http://chat.transonly.nl/sounds/sound_1.mp3');
 newmsgsound.setAttribute('id', 'newmsgsound');
 newmsgsound.setAttribute('controls', 'controls');
 document.body.appendChild(newmsgsound);
 var audiolevel = document.getElementById('newmsgsound');
 var value = document.getElementById('volumeslider').value;
 audiolevel.volume=value;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM