2012-06-03 5 views
13

방금 ​​HTML5를 배우기 시작했으며 JQuery를 사용하는 HTML5 오디오 플레이어를 사용하고있었습니다. Playlist로 플레이어를 코딩했는데, 내 입력 범위 슬라이더가 작동하지 않는 Duration을 제외한 모든 것이 잘 작동합니다. 내가 디버깅 할 때 내 기간이 나를 NAN으로 보여주는 것을 발견했다. 노래를 초기화 한 후 재생 시간을 설정합니다. 내가 재생 목록의 노래를 클릭하면 다음 HTML5 오디오 플레이어 재생 시간 내림차순

내 JS 코드

jQuery(document).ready(function() { 

    container = $('.container'); 
    playList = $('#playlist'); 
    playListSong = $('#playlist li'); 
    cover = $('.cover'); 
    play = $('#play'); 
    pause = $('#pause'); 
    mute = $('#mute'); 
    muted = $('#muted'); 
    close = $('#close'); 

    song = new Audio('music/Whistle-Flo-Rida-Mp3-Download.mp3','music/Whistle-Flo-Rida-Mp3-Download.mp3'); 

    var canPlay = song.canPlayType('audio/mpeg;'); 
    if (canPlay) { 
     song.type= 'audio/mpeg'; 
     song.src= 'music/Whistle-Flo-Rida-Mp3-Download.mp3'; 
    } 

    playListSong.click(function(){ 

     $('#close').trigger('click'); 
     window["song"] = new Audio('music/'+$(this).html()+'.mp3','music/'+$(this).html()+'.mp3'); 

     $('#play').trigger('click'); 

    }); 

    play.live('click', function(e) { 

     e.preventDefault(); 
     song.play(); 
     //cover.attr("title", song.duration); 
     $(this).replaceWith('<a class="button gradient" id="pause" href="" title=""><span>k</span></a>'); 

     $('#close').fadeIn(300); 
     $('#seek').attr('max',song.duration); 
    }); 

    pause.live('click', function(e) { 
     e.preventDefault(); 
     song.pause(); 
     $(this).replaceWith('<a class="button gradient" id="play" href="" title=""><span>d</span></a>'); 

    }); 

    mute.live('click', function(e) { 
     e.preventDefault(); 
     song.volume = 0; 
     $(this).replaceWith('<a class="button gradient" id="muted" href="" title=""><span>o</span></a>'); 

    }); 

    muted.live('click', function(e) { 
     e.preventDefault(); 
     song.volume = 1; 
     $(this).replaceWith('<a class="button gradient" id="mute" href="" title=""><span>o</span></a>'); 

    }); 

    $('#close').click(function(e) { 
     e.preventDefault(); 
     container.removeClass('containerLarge'); 
     cover.removeClass('coverLarge'); 
     song.pause(); 
     song.currentTime = 0; 
     $('#pause').replaceWith('<a class="button gradient" id="play" href="" title=""><span>d</span></a>'); 
     $('#close').fadeOut(300); 
    }); 



    $("#seek").bind("change", function() { 
     song.currentTime = $(this).val(); 
     $("#seek").attr("max", song.duration); 
    }); 

    song.addEventListener('timeupdate',function(){ 
     curtime = parseInt(song.currentTime, 10); 
     $("#seek").attr("value", curtime); 
    }); 

}); 

이며, 기간은 항상 NAN하지만 난 다음 시간을 재생 버튼을 클릭하면 기본적으로 나는 하나의 노래를 설정하고, 직접 페이지로드에있다 잘 작동합니다. 재생 목록 노래에 대해서는 작동하지 않습니다.

답변

27

loadedmetadata 이벤트 리스너를 사용하면 메타 데이터가로드되는 즉시 오디오 지속 시간을 얻을 수 있습니다. 다음 코드와 같은 것을하십시오 :

+0

'durationchang' 이벤트를 대신 사용하면 변경 될 때마다 기간 표시가 업데이트됩니다. –

+0

대답은 나에게 좋은 힌트를 준다. – Calvin

+0

이 코드를 어디에 사용합니까? – Jaromjj