2017-01-14 5 views
0

私は同じcurrentTimeでサウンドを再生する前と後のミュージックプレーヤーで作業しています。.attr( 'song');同じですが、.attr( 'song');それはcurrentTimeを0にリセットする必要があります。これは同時にサウンドを再生するために働きますが、attrが異なる場合はリセットされません。ユーザーが "前"または "後"ボタンをクリックし、両方の曲.attr( 'song')== rockの場合、currentTimeを0にリセットしてはいけませんが、ボタンと.attr( 'song')をクリックすると、それは0song attrがcurrent_Timeを0にリセットしていない場合Jquery

JSFiddleにCURRENTTIMEを設定する必要が異なります。

https://jsfiddle.net/jeffd/z7uymp23/

HTML:

<div class="songtype">Rock <br> <div class="smallerthang"><strong>Steve Godsley</strong> by Boontango</div></div> 
<div class="beforebutton player" key="rockbefore.mp3" song="rock"><span class="symbol"><i class="material-icons playbuttonyo">play_circle_filled</i></span> Before</div> 
<div class="afterbutton player" key="rockafter.mp3" song="rock"><span class="symbol"><i class="material-icons playbuttonyo">play_circle_filled</i></span> After</div> 

<div class="songtype">Rap <br> <div class="smallerthang"><strong>Million or More</strong> by China Fox, Superhype Mic, Warren 'Thir13een' Young, Nicolas BONNEYRAT</div></div> 
<div class="beforebutton player" key="rapbefore.mp3" song="rap"><span class="symbol"><i class="material-icons playbuttonyo">play_circle_filled</i></span> Before</div> 
<div class="afterbutton player" key="rapafter.mp3" song="rap"><span class="symbol"><i class="material-icons playbuttonyo">play_circle_filled</i></span> After</div> 

jQueryの

window.nowplay=$(".player"); 
$(".player").on('click', function() { 

    var key = $(this).attr('key'); 
    var song = $(this).attr('song'); 
    window.nowplay = $(this); 
    window.nowplay2 = $(this); 
    EvalSound(this, key); 
    $(".player").not(this).removeClass("pause"); 
    $(".player").not(this).children('.symbol').html("<i class='material-icons playbuttonyo'>play_circle_filled</i>"); 
    nowplay.children('.symbol').html("<i class='material-icons playbuttonyo'>pause_circle_filled</i>"); 
    nowplay.addClass("pause"); 
}); 
var thissound = new Audio(); 
var currentKey; 

function EvalSound(el, key) { 
    thissound.addEventListener('ended', function() { 

nowplay.children('.symbol').html("<i class='material-icons playbuttonyo'>play_circle_filled</i>"); 
    }); 
    thissound.addEventListener('pause', function() { 

     nowplay.removeClass("pause"); 

      nowplay.children('.symbol').html("<i class='material-icons playbuttonyo'>play_circle_filled</i>"); 

    }); 
    thissound.addEventListener('loadstart', function() { 


    }); 
     thissound.addEventListener('playing', function() { 

     nowplay.children('.symbol').html("<i class='material-icons playbuttonyo'>pause_circle_filled</i>"); 

    }); 
    var timenow = thissound.currentTime; 
    if (currentKey !== key) thissound.src = key; 


    currentKey = key; 
    //currentSong = song; 

    thissound.currentTime = timenow; 
    //if (song !== currentSong) thissound.currentTime = 0; 

    if (thissound.paused) thissound.play(); 
    else thissound.pause(); 
    currentPlayer = thissound; 
} 
+0

代わりに(.ATTR 'の使用[' .DATA() '](https://api.jquery.com/jquery.data/))'と接頭辞 'key' 'song'属性は' data-' – zer00ne

答えて

0
私は何をしなければならなかったことは 前にグローバル変数nowplayは$(この)に変更し、それが新しい曲を再生するとき、それは後にマッチしたかどうかを確認され、 window.oldsong = nowplay.attr('song');」をoldsongのグローバル変数を追加しました

JSFiddle

https://jsfiddle.net/jeffd/z7uymp23/1/

:ここ
thissound.currentTime = timenow; 
     if (nowplay.attr('song') !== oldsong) thissound.currentTime = 0; 

は、添付動作するコードとJSフィドルです

のjQuery

window.nowplay=$(".player"); 
$(".player").on('click', function() { 
    var key = $(this).attr('key'); 
    var song = $(this).attr('song'); 
    window.oldsong = nowplay.attr('song'); 
    window.nowplay = $(this); 
    window.nowplay2 = $(this); 
    EvalSound(this, key); 
    $(".player").not(this).removeClass("pause"); 
    $(".player").not(this).children('.symbol').html("<i class='material-icons playbuttonyo'>play_circle_filled</i>"); 
    nowplay.children('.symbol').html("<i class='material-icons playbuttonyo'>pause_circle_filled</i>"); 
    nowplay.addClass("pause"); 
}); 
    var thissound = new Audio(); 
    var currentKey; 
function EvalSound(el, key) { 
    thissound.addEventListener('ended', function() { 

nowplay.children('.symbol').html("<i class='material-icons playbuttonyo'>play_circle_filled</i>"); 
    }); 
    thissound.addEventListener('pause', function() { 

     nowplay.removeClass("pause"); 

      nowplay.children('.symbol').html("<i class='material-icons playbuttonyo'>play_circle_filled</i>"); 

    }); 
    thissound.addEventListener('loadstart', function() { 


    }); 
     thissound.addEventListener('playing', function() { 

     nowplay.children('.symbol').html("<i class='material-icons playbuttonyo'>pause_circle_filled</i>"); 

    }); 
    var timenow = thissound.currentTime; 
    if (currentKey !== key) thissound.src = key; 


    currentKey = key; 
    //currentSong = song; 

    thissound.currentTime = timenow; 
    if (nowplay.attr('song') !== oldsong) thissound.currentTime = 0; 

    if (thissound.paused) thissound.play(); 
    else thissound.pause(); 
    currentPlayer = thissound; 
} 

</script> 
関連する問題