Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html5 audio player - jquery toggle click play/pause?

Tags:

html

jquery

audio

i wonder what i'm doing wrong?

    $('.player_audio').click(function() {     if ($('.player_audio').paused == false) {         $('.player_audio').pause();         alert('music paused');     } else {         $('.player_audio').play();         alert('music playing');     } }); 

i can't seem to start the audio track if i hit the "player_audio" tag.

<div class='thumb audio'><audio class='player_audio' src='$path/$value'></audio></div> 

any idea what i'm doing wrong or what i have to do to get it working?

like image 643
matt Avatar asked Jun 07 '10 08:06

matt


2 Answers

You can call native methods trough trigger in jQuery. Just do this:

$('.play').trigger("play"); 

And the same for pause: $('.play').trigger("pause");

EDIT: as F... pointed out in the comments, you can do something similar to access properties: $('.play').prop("paused");

like image 93
Thash Avatar answered Sep 22 '22 10:09

Thash


Well, I'm not 100% sure, but I don't think jQuery extends/parses those functions and attributes (.paused, .pause(), .play()).

try to access those over the DOM element, like:

$('.player_audio').click(function() {   if (this.paused == false) {       this.pause();       alert('music paused');   } else {       this.play();       alert('music playing');   } }); 
like image 38
jAndy Avatar answered Sep 22 '22 10:09

jAndy