简体   繁体   English

如何更改 HTML5 音频中的播放/按钮图像?

[英]How can I change play/button images in HTML5 audio?

I have the code of audio player.我有音频播放器的代码。 How can I change the picture of play/pause button when press?按下时如何更改播放/暂停按钮的图片?

<div class="onlineradio"><img src="images/Radio.jpg" alt="" />
<p><video id="yourAudio" width="0" height="0">
    <source src='http://178.219.160.126:8000/stream.mp3' type='audio/mpeg' preload="auto" />
</video> <a id="audioControl" href="#"><img src="images/radioscalebutton.png" alt="" /></a></p>
</div>
<script>// <![CDATA[
var yourAudio = document.getElementById('yourAudio'),
    ctrl = document.getElementById('audioControl');

ctrl.onclick = function () {

    // Update the Button
    var pause = ctrl.innerHTML === 'pause!';
    ctrl.innerHTML = pause ? 'pause' : 'play';

    // Update the Audio
    var method = pause ? 'pause' : 'play';
    yourAudio[method]();

    // Prevent Default Action
    return false;
};
// ]]></script>

I have implemented a final working solution with custom audio controls (play and pause) with toggling state button.我已经使用带有切换状态按钮的自定义音频控件(播放和暂停)实现了最终的工作解决方案。 If something is not clear for you, please ask.如果您有不清楚的地方,请询问。

Here is the working solution and the JSFiddle :这是工作解决方案和JSFiddle

 var yourAudio = document.getElementById('yourAudio'), ctrl = document.getElementById('audioControl'), playButton = document.getElementById('play'), pauseButton = document.getElementById('pause'); function toggleButton() { if (playButton.style.display === 'none') { playButton.style.display = 'block'; pauseButton.style.display = 'none'; } else { playButton.style.display = 'none'; pauseButton.style.display = 'block'; } } ctrl.onclick = function () { if (yourAudio.paused) { yourAudio.play(); } else { yourAudio.pause(); } toggleButton(); // Prevent Default Action return false; };
 #audioControl img { width: 50px; height: 50px; } #pause { display: none; }
 <div class="onlineradio"> <img src="images/Radio.jpg" alt="" /> <p> <audio id="yourAudio"> <source src='http://178.219.160.126:8000/stream.mp3' type='audio/mpeg' preload="auto" /> </audio> <a id="audioControl" href="#"> <img src="http://etc-mysitemyway.s3.amazonaws.com/icons/legacy-previews/icons/glossy-black-3d-buttons-icons-media/002110-glossy-black-3d-button-icon-media-a-media22-arrow-forward1.png" id="play"/> <img src="https://www.wisc-online.com/asset-repository/getfile?id=415&getType=view" id="pause"/> </a> </p> </div>

You can download the images of controls and use refer to them locally.您可以下载控件的图像并在本地使用它们。

Karlen Kishmiryan提出的解决方案仅在每页只有一个音频按钮时才有效。

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

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