douqiao2471 2013-07-18 06:40
浏览 88

单击时播放某个音轨

All the songs have a .song class but it plays the first song on the playlist. It's basically the play button for the whole playlist. I've play around with this for awhile and I can't seem to get it right. It might be the simplest thing too. I have the song populate with php depending on the album. I want people to be able to click a certain song and that song plays.

example: http://mixtapemonkey.com/mixtape?m=637

Also if you know how to toggle between the play and stop button, that would be nice to throw in there too. Thanks!

<script>

jQuery(document).ready(function(){

    i=0;

    nowPlaying = document.getElementsByClassName('playsong');
    nowPlaying[i].load();

    $('.play').on('click', function() {
        nowPlaying[i].play();
        callMeta();
    });

    $('.song').on('click', function() {
        nowPlaying[i].play();
        callMeta();
    });

    $('.pause').on('click', function() {
        nowPlaying[i].pause();
        callMeta();
    });

    $('.next').on('click', function() {
        $.each($('audio.playsong'), function(){
            this.pause();
        });
        ++i;
        nowPlaying[i].load();
        nowPlaying[i].play();
        callMeta();

    })

    function callMeta(){
        var trackTitle = $(nowPlaying[i]).attr('data-songtitle');
        $('.songtitle').html(trackTitle);

        var trackArtist = $(nowPlaying[i]).attr('data-songartist');
        $('.songartist').html(trackArtist);

    }

})

  • 写回答

2条回答 默认 最新

  • duandi8852752 2013-07-18 06:48
    关注

    You have to target the audio element inside each .song, now you're always targeting the first one.
    To toggle, check if the audio is paused, and play() or pause() accordingly :

    $('.song').on('click', function() {
        var audio = $('.playsong', this).get(0);
    
        if (audio.paused) {
             audio.play();
        }else{
             audio.pause()
        }
        callMeta();
    });
    

    EDIT:

    with a few changes, I'm guessing something like this would work :

    jQuery(document).ready(function($){
    
        var audios = $('.playsong');
        var audio  = audios.get(0);
    
        audio.load();
    
        $('.play').on('click', function() {
            callMeta(audio);
        });
    
        $('.song').on('click', function() {
            audio = $('.playsong', this).get(0);
            callMeta(audio);
        });
    
        $('.pause').on('click', function() {
            audio.pause();
        });
    
        $('.next').on('click', function() {
            var i = audios.index(audio);
            audio = $(audios).get(i+1);
            callMeta(audio);
        });
    
        function callMeta(elem){
            audios.each(function(i,el) {
                if (!el.paused) {
                    el.pause();
                    el.currentTime = 0;
                }
            });
            elem.load();
            elem.play();
            $(elem).on('ended', function() {
                $('.next').trigger('click');
            });
            $('.songtitle').html($(elem).attr('data-songtitle'));
            $('.songartist').html( $(elem).attr('data-songartist') );
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制