Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Video mute/unmute with jQuery

Tags:

I'd like to make a mute/unmute button in jQuery. I've done some searching on Stackoverflow and this is what I managed to do so far:

$("video").prop('muted', true);  $("#mute-video").click( function (){     if( $("video").prop('muted', true) )     {         $("video").prop('muted', false);     }      else {     $("video").prop('muted', true);     }  }); 

but for some reason it's only able to unmute, not to mute back.

Any idea what's wrong with the code?

like image 962
oneday Avatar asked Feb 18 '14 11:02

oneday


People also ask

Can you unmute a muted video?

Continue editing video To unmute a clip, simply right click and de-select mute, or deseclect the mute track icon.

How do I mute a video in JavaScript?

How do I mute and unmute a video in JavaScript? var button = document. getElementById('mute'); button.


1 Answers

When you're doing if( $("video").prop('muted', true) ) you're both setting the property to true and then ask if it's true.

Changing the condition to if( $("video").prop('muted') ) solves the problem - Here's an example.

Also note this will work on all videos on a page so if you have more than one player it might get confusing.

like image 116
Ronny Avatar answered Sep 22 '22 13:09

Ronny