jquery - 如果歌曲属性不匹配,则将 currentTime 重置为 0 Jquery

标签 jquery audio

我正在开发一个前后音乐播放器,如果 .attr('song'); 将在相同的 currentTime 播放声音。是一样的,但是如果 .attr('song');更改它应该将 currentTime 重置为 0。它适用于同时播放声音,但如果 attr 不同则不会重置。如果用户单击“之前”或“之后”按钮并且歌曲 .attr('song') == rock 它不应该将 currentTime 重置为 0,但如果他们单击按钮和 .attr('song');是不同的,它应该将 currentTime 设置为 0

JSFiddle:

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;
 }

最佳答案

我要做的是添加一个全局变量“oldsong”window.oldsong = nowplay.attr('song'); , 之前 全局变量 nowplay 更改为 $(this) ,然后在播放新歌曲时检查它是否匹配,

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

这是附加的工作代码和js fiddle :

JSFiddle

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

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>

关于jquery - 如果歌曲属性不匹配,则将 currentTime 重置为 0 Jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41646719/

相关文章:

javascript - 仅在提交后禁用提交按钮

android - 在媒体播放器中重复播放音频

android - 无法与Android应用共享音频文件

audio - 涡轮帕斯卡的声音。它们是如何工作的?

matlab - 使用 Matlab 从 .wav 文件中去除脉冲噪声

javascript - 在 javascript 中使用 promises - 只访问服务器一次

jquery - 是否可以在 Facebook 应用程序中使用视差?

javascript - .this 元素中的 jQuery closest() 不起作用

Jquery 图片库图像在悬停时缩放

javascript - 如何为链接添加声音