views:

525

answers:

3

The question is closely related to my past question here, but it is not the same.

Problem: to add a Play/Pause button in Chromeless Youtube here.

Attack: I added the below code like here (search "SERVERFAULT" in the code) or like below:

  var playingtimes = 0;

  function playPauseVideo(playingtimes) {
    if (playingtimes % 2 == 0){
      playVideo();
    }else{
      pauseVideo();
    }
    playingtimes += 1;
  }

Then, I added near the end:

<a href="javascript:void(0);" onclick="playPauseVideo(playingtimes);">Play/Pause</a>

Error: the button works like Play-button, because playingtimes is EVEN ie 0 all the time. Errror is in the playingtimes-variable.

Question: How can I add a Play/Pause Button to the Chromeless Youtube?

A: 

Can you add this line into your code. click the Play/Pause button a bunch of times and edit your question with the results.

alert("Value of 'playingtimes' global is " + playingtimes);

Place here:

  var playingtimes = 0;

      function playPauseVideo(playingtimes) {
        if (playingtimes % 2 == 0){
          playVideo();
        }else{
          pauseVideo();
        }
        playingtimes += 1;
        alert("Value of 'playingtimes' global is " + playingtimes);
      }
Izzy
A: 

Remove playingtimes from function definition and from function call, then it will be used global variable playingtimes instead of local one with the same name which you defined inside the function. So your code will be:

var playingtimes = 0;
function playPauseVideo() {

and link to call it:

<a href="javascript:void(0);" onclick="playPauseVideo();">Play/Pause</a>
Alex Pavlov
+2  A: 

You can get the state of the player directly by using ytplayer.getPlayerState(), so you can make a simple function like this to do it:

function playPause() {
    if (ytplayer.getPlayerState() != 1) {
        // player is not playing, so tell it to play
        playVideo();
    } else {
        pauseVideo();
    }
}

The cool thing about a setup like this, it should also work if the video is ENDED or CUED.

Geoff