Search code examples
jqueryjplayer

How to prevent event of jPlayer


I want to prevent some events of jPlayer. My business condition says that when user listen audio for 2 times then I have to stop playing audio, for this I am trying event.preventDefault() but it's not working, code as below:

$("#jquery_jplayer_1").bind($.jPlayer.event.play, function(event) { 
        if(cnt==2)
        {
        event.preventDefault();}
    });

But this code is not working, How can I prevent an event of jPlayer.


Solution

  • You can do it in a dirty way by using the plugin events...

    var cnt = 0;
    $('#player').jPlayer({
       /* options here ... */
       play: function(){
          if (cnt == 2) $(this).jPlayer('stop');
       },
       ended: function(){
          cnt++;
       }
    });
    

    You can't prevent the play event but you can trigger the stop event to deny it. The audio will only play 2 times. Note that the cnt increases only when the audio/video reached the end and not if the video gets paused, or stopped. You might want to remove the pause or stop controls.