Search code examples
phpjplayer

Get the current time in jPlayer


I want to get the current time of a song, for which I am using

var currentTime = $('#jquery_jplayer_1').data("jPlayer").status.currentTime;

but I am getting and error Uncaught TypeError: Cannot read property 'status' of undefined in the console.

What i am trying to do is suppose a song is playing and when the user clicks on the playbutton of another song i want to get the time played of the previous song and then start playing the clicked song.

Here is my code:

var currentTime = $('#jquery_jplayer_1').data("jPlayer").status.currentTime;
alert(currentTime);

$("#jquery_jplayer_1").jPlayer("stop");

var id = "#jquery_jplayer_1";
var media = {
    mp3: audio_element
};
var options = {
    swfPath: "/appystream/assets/jplayer",
    supplied: "mp3",
    wmode: "window"
};
var currentTime = $('#jquery_jplayer_1').data("jPlayer").status.currentTime;
alert(currentTime);

// jPlayer android versions 2.3 fix.
var myFix = new jPlayerAndroidFix(id, media, options);
myFix.setMedia(media).play();
setTimeout(function () {
    myFix.setMedia(media).play();
}, 2000);

No matter where I put the code to get the current time, it responds with the error given above..

What am I doing wrong ? how else can I do it ?


Solution

  • I think you are doing things in the wrong way. I way I did the things is:

    $(document).ready(function(){
    
        $("#jp").jPlayer({
            ready: function () {
                $(this).jPlayer("setMedia", {
                    m4v: "http://www.jplayer.org/video/m4v/Big_Buck_Bunny_Trailer_480x270_h264aac.m4v",
                    ogv: "http://www.jplayer.org/video/ogv/Big_Buck_Bunny_Trailer_480x270.ogv",
                    poster: "http://www.jplayer.org/video/poster/Big_Buck_Bunny_Trailer_480x270.png"
                });
            },
            ended: function (event) {
                $(this).jPlayer("play");
            },
            swfPath: "js",
            supplied: "m4v, ogv"
        });
    
        $("#jp").bind($.jPlayer.event.timeupdate, function(event) { 
            var currentTime = Math.floor(event.jPlayer.status.currentTime)
    
            if (currentTime < 3){
                $("#slideshow").html("<img src='http://yoursite.com.com/wm/jplayer/slides/slide1.png'/>");  
            }
    
            if (currentTime >= 3 && currentTime < 12){
                $("#slideshow").html("<img src='http://yoursite.com.com/wm/jplayer/slides/slide2.png'/>");  
            }
    
            if (currentTime >= 12 && currentTime < 16){
                $("#slideshow").html("<img src='http://yoursite.com.com/wm/jplayer/slides/slide3.png'/>");  
            }
    
            if (currentTime >= 16 && currentTime < 22){
                $("#slideshow").html("<img src='http://yoursite.com.com/wm/jplayer/slides/slide4.png'/>");  
            }
    
            if (currentTime >= 22 && currentTime < 30){
                $("#slideshow").html("<img src='http://yoursite.com.com/wm/jplayer/slides/slide5.png'/>");  
            }
    
    
            if (currentTime >= 30){
                $("#slideshow").html("<img src='http://yoursite.com.com/wm/jplayer/slides/fin.png'/>"); 
            }
    
    
    
        });
    
    
    });