-1

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.

Amogh
  • 4,453
  • 11
  • 45
  • 106
  • You can't, unless you edit the source code of the plugin. – Spokey Mar 06 '15 at 08:25
  • @Spokey Ohh can you give me hint how to do it? and is it allowed? – Amogh Mar 06 '15 at 08:26
  • If you want to stop people playing the video, why bother even having it? – Rory McCrossan Mar 06 '15 at 08:28
  • @RoryMcCrossan hey why downvote!! It was just an example. and what if I want to stop playing on an business condition. I want to stop playing audio when user listen it for two times. – Amogh Mar 06 '15 at 08:30
  • I didn't downvote, but it would be best to put details like that in the question – Rory McCrossan Mar 06 '15 at 08:33
  • @RoryMcCrossan I apologize, question updated. – Amogh Mar 06 '15 at 08:37
  • You can't do it like this because the events are probably triggered internally (and preventDefault doesn't what what you think it does). You could for example trigger the stop event instead of preventDefault(). If you would create a [fiddle](http://jsfiddle.net) or use the stack snippets you might get help faster since people will be able to directly work with the code, that doesn't mean that you don't have to try it yourself first. – Spokey Mar 06 '15 at 08:40
  • @Spokey yes I tried but just in regular manner like Jquery. Thanks for your help. and everyone can't think in same manner. I tried I failed so I asked. – Amogh Mar 06 '15 at 08:44
  • Where is `cnt` coming from in this context? – TZHX Mar 06 '15 at 09:03
  • @TZHX I am incrementing on event `$.jPlayer.event.ended` – Amogh Mar 06 '15 at 09:06

1 Answers1

0

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.

Spokey
  • 10,974
  • 2
  • 28
  • 44