9

Actually this the first time I'm gonna use jplayer plugin, so all I need is to play a sound clip (2 sec) when I click on a div, and I don't know how to use jplayer!

I also need to load the sound clip file in cache as well as the page is loading, so when I click that div the sound clip plays immediately without loading it when clicking

btw, is it possible to do that without using jplayer, jquery maybe?!

Derek Adair
  • 21,846
  • 31
  • 97
  • 134
Sam
  • 325
  • 2
  • 3
  • 9

2 Answers2

3

If you want to stick with jPlayer, try setting warningAlerts and errorAlerts to true.

$(id).jPlayer( { warningAlerts: true, errorAlerts: true } );

This may solve your problem.

But as long as you only need sound (no video), SoundManager 2 might be a better fit for you. Its really robust and provides extensive debugging output.

soundManager.url = './swf/'; // directory where SM2 .SWFs live

var mySound;

soundManager.onready(function() {

    // SM2 has loaded - now you can create and play sounds!

    mySound = soundManager.createSound({
      id: 'someSound',
      url: '/path/to/some.mp3'
    });
});

//....
//if(xhr-stuff)
    if(mySound)
        mySound.play();

Use soundmanager2.js for testing and soundmanager2-nodebug-jsmin.js for production. If you have further questions, don't hesistate to ask.

Maximilian Hils
  • 6,309
  • 3
  • 27
  • 46
2

-snip-

scratch that... there's an api

http://www.jplayer.org/latest/developer-guide/#jPlayer-play

Use

$("#jpId").jPlayer("play");
Joseph Marikle
  • 76,418
  • 17
  • 112
  • 129
  • This didn't work for me - I even tried adding $.jPlayer("play", 0); and it is still only playing one time. Perhaps I should have just created my own question - but it was similar enough I figured i'd just offera bounty on this one. – Derek Adair Sep 19 '11 at 18:24
  • The following code preceeds my jPlayer('play') api call. `$("#audio-player").jPlayer({ swfPath: './aud', solution: "html, flash", muted: false, errorAlerts: false, warningAlerts: false, ready: function(e){ $(this).jPlayer("setMedia", { mp3: "./aud/msg_rcvd.mp3", ogg: "./aud/msg_rcvd.ogg" }) } });` – Derek Adair Sep 19 '11 at 18:24
  • @Derek Adair You said you can only get it to play one time? Are you trying to get it to loop then? – Joseph Marikle Sep 19 '11 at 20:09
  • Nope, it's supposed to be triggered by via a custom event. (I figured a click event was easy enough to substitute for my "custom" event). Basically it looks like its TRYING to play again (it actually runs `$.jPlayer("play", 0)`) the audio appears to not be playing. The only potential lead I have is it could have something to do with playing my OGG/MP3 OR something to do with headers or whatever. – Derek Adair Sep 19 '11 at 22:02
  • That's very odd... I have never used the plugin before but I imagine it's on jplayer's end or the browser. (does jplayer just provide a front end to the HTML5 player?). – Joseph Marikle Sep 19 '11 at 22:05
  • If they do I don't wanna use it - I really just need a simple x-browser way to play a sound if the window is not focused and a condition is met in an XHR poll. – Derek Adair Sep 20 '11 at 00:17