简体   繁体   English

音频控件的Javascript按钮切换

[英]Javascript Button Toggle For Audio Controls

Here is a JSFiddle: 这是一个JSFiddle:

http://jsfiddle.net/pLjM7/ http://jsfiddle.net/pLjM7/

HTML: HTML:

<audio id="yourAudio" preload='none'>
    <source src='http://dl.dropbox.com/u/1538714/article_resources/song.m4a' type='audio/mpeg' />
</audio>
<a href="#" id="audioControl">play!</a>

JS: JS:

var yourAudio = document.getElementById('yourAudio'),
    ctrl = document.getElementById('audioControl');

ctrl.onclick = function () {

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

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

    // Prevent Default Action
    return false;
};

Instead of using the words 'play!' 而不是使用“玩!”一词 and 'pause!' 和“暂停!” how can I toggle between custom images I've created for play and pause? 如何在为播放和暂停而创建的自定义图像之间切换?

In addition to this, is there a way to make the 'play' button return after the audio is done playing. 除此之外,还有一种方法可以使音频播放完毕后返回“播放”按钮。 As of now when the audio is done playing it stays on pause. 到目前为止,当音频播放完毕时,它会保持暂停状态。

var yourAudio = document.getElementById('yourAudio'),
    ctrl = document.getElementById('audioControl');

ctrl.onclick = function () {

    pause_html='<img src="https://cdn2.iconfinder.com/data/icons/media-and-navigation-buttons-square/512/Button_4-128.png">';

    play_html='<img src="http://codropspz.tympanus.netdna-cdn.com/codrops/wp-content/uploads/2010/01/play1-150x150.png">';

    // Update the Button
    var pause = ctrl.innerHTML === pause_html;
    ctrl.innerHTML = pause ? play_html : pause_html;

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

    // Prevent Default Action
    return false;
};



    document.getElementById('yourAudio').addEventListener('ended', function(){
  ctrl.innerHTML=play_html;

    });

Second part using ended event (really nice feature). 第二部分使用结束事件(确实不错的功能)。 Jsfiddle: http://jsfiddle.net/8LczkwLz/ Jsfiddle: http : //jsfiddle.net/8LczkwLz/

PS You will have to add image in HTML, too, of course, check fiddle. PS当然,您还必须添加HTML图像,请检查小提琴。 Hope all is clear. 希望一切都清楚。

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

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