2014-09-30 111 views
0

我有一個簡單的多音軌音頻播放器。用戶可以選擇一個他們喜歡參考的軌道。玩家基本上在工作。但是,我只想要一次播放一首曲目,但這並不奏效。音頻播放器 - 一次只播放一首曲目,不起作用

這裏是我已經做了

HTML AUDIO PLAYER

<div class="audioplayer"> 
    <div class="md-col-4"> 
    <input type="radio" name="track" class="track1" id="track1" value="1" checked="checked"> 
    <label for="track1" class="track1 selected"> 
     <div class="audiotrack"> 
      <div class="info"> 
       <h5>In this world</h5> 
       <p>Gramatik</p> 
      </div> 
      <audio src="/media/gramatik.mp3" class="audio-player" preload="auto"></audio> 

      <div class="playpause" data-state="play"></div> 
      <div class="progressbar"></div> 
     </div> 
    </label> 
    </div> 

    <div class="md-col-4"> 
    <input type="radio" name="track" class="track2" id="track1" value="2" checked="checked"> 
    <label for="track2" class="track2"> 
     <div class="audiotrack"> 
      <div class="info"> 
       <h5>Stars</h5> 
       <p>Tom Hank</p> 
      </div> 
      <audio src="/media/looseyourself.mp3" class="audio-player" preload="auto"></audio> 

      <div class="playpause" data-state="play"></div> 
      <div class="progressbar"></div> 
     </div> 
    </label> 
    </div> 
</div> 

JS

$('.audioplayer label').on('click', function(e){ 
    e.preventDefault(); 
    var labelid = $(this).attr('for').toString(); 
    $('.selected').removeClass('selected'); 
    $('input:radio[id='+labelid+']').prop('checked', true); 
    console.log($('input:radio[id='+labelid+']').prop('checked', true)); 
    $(this).addClass('selected'); 

    var btn = $(this).find(".playpause"); 
    var audio = btn.siblings('audio'); 

    var progress= btn.siblings(".progressbar"); 
    if(!btn.hasClass("playing")){ 
     btn.addClass("playing"); 
     audio.get(0).play(); 
     var playertime = setInterval(function(){ 
     var c = audio.get(0).currentTime; 
     var d = audio.get(0).duration; 
     progress.css("width", (c/d)*100 +'px'); 
     }, 300); 
    } 
    else{ 
     btn.removeClass("playing"); 
     audio.get(0).pause(); 
     clearInterval(playertime); 
    } 
}); 

我是缺少在這裏? 感謝

+0

您要禁用玩其他軌道的能力,或者您想停止/暫停當前的音頻和遊戲等一個? – Neo 2014-09-30 09:04:50

+0

暫停所有開始一個 – Winchestro 2014-09-30 09:07:58

+0

如果我點擊任何音軌,然後我想先前的軌道停止,如果有的話,目前點擊一個。 – MysticCodes 2014-09-30 09:08:00

回答

2

你可以做這樣的事情

var audio = btn.siblings('audio'); 
//I wouldn't recommend but this is a way to do it. 
$('audio').each(function(){ 
    console.log('audio'); 
    this.pause(); 
});  
var progress= btn.siblings(".progressbar"); 
+0

至少它工作。謝謝neolivz4ever – MysticCodes 2014-09-30 11:44:59