How to change the volume of an iframe video by javascript?

6

I have tried the code below to solve.

var volumeIcon = 1;

$(".ico-volume").on('click', function(event) {

   if (volumeIcon == 1)
   {
      $(this).find("img").removeAttr('src');
      $(this).find("img").attr('src', 'img/ico/volume-out-ico.png');
      volumeIcon = 0;
      setVolume(volumeIcon);
   }
   else
   {
      $(this).find("img").removeAttr('src');
      $(this).find("img").attr('src', 'img/ico/volume-in-ico.png');
      volumeIcon = 1;
      setVolume(volumeIcon);
   }

});

essa primeira função é de um icone de volume que eu tenho aqui, e quando o usuário apertar ele troca a imagem, mas se atentem na função que ele chama por ultimo. Pois bem, aqui vai as outras funções:

function onYouTubePlayerReady(playerId)
{
    ytplayer = document.getElementById("video");  
}

function setVolume(volume)
{
    if (ytplayer)
        ytplayer.volume = volume;
}

Note: In the HTML of the player, I already put the parameter in enablejsapi=1 in <iframe> .

Could someone help me, please? : D

    
asked by anonymous 20.11.2015 / 13:35

1 answer

1

I've searched the YouTube API here and see what I found:

Disables the sound of the player.

player.mute():Void

Enable the sound of the player.

player.unMute():Void

Returns true if the player is mute. If not, return false.

player.isMuted():Boolean

Sets the volume. Accepts an integer between 0 and 100.

  

What you have to do in your code.

player.setVolume(volume:Number):Void

Returns the current volume of the player, an integer between 0 and 100. getVolume () will return the volume even if the player is mute.

player.getVolume():Number

Follow the YouTube API Link.

link

    
20.11.2015 / 14:19