Get player status with multiple YouTube players on page

I have one HTML code with multiple YouTube players.

I want onStateChange to be notified when a user plays a video.

function onYouTubePlayerReady(playerid) {
    var player = document.getElementById("playerA");
    player.addEventListener("onStateChange", "callback");
}

Now the callback receives the newState parameter.

function callback(newState) {
    ...
}

This only works with one player. But with many players, how can I find out which player raised the StateChange event? The callback has only one parameter.

Is there any way to find out which player raised the event form callback, or should I set uo a different callback for each of them?

+3
source share
3 answers

Here is my solution based on other answers:

(1) Add a parameter idto the object and / or insert tags

(2) playerapiid URL- ( id)

<object id="video1" data="http://www.youtube.com/v/AAAAAAAAAAA&version=3&enablejsapi=1&playerapiid=video1" ...>
    <embed ...>
</object>
<object id="video2" data="http://www.youtube.com/v/BBBBBBBBBBB&version=3&enablejsapi=1&playerapiid=video2" ...>
    <embed ...>
</object>

(3) onYouTubePlayerReady. :

function onYouTubePlayerReady(playerApiId) {
  var player = document.getElementById(playerApiId);
  window["onStateChange" + playerApiId] = function(state) {
    console.log("#" + playerApiId + ": new state " + state);
  };
  player.addEventListener("onStateChange", "onStateChange" + playerApiId);
}

.

+3

. :

var videos = {};

//store the video player data in the hash, with the playerapiid as the key
function loadFeaturedVideo(youTubeVideoId) {
    videos["featuredytplayer"] = {
        "yt_id": youTubeVideoId,
        "wrapperSelector": "#featured_player_wrapper",
        "embed": "featuredYtPlayer"
    };
    ...
}

function onYouTubePlayerReady(playerId) {
    var currentVideo = videos[playerId];
    var ytPlayer = document.getElementById(currentVideo["embed"]);
    videos[playerId]["stateChangeListener"] = function(newState) {
        var container = $(currentVideo["wrapperSelector"]);

        ...
    }
    ytPlayer.addEventListener("onStateChange", "videos['" + playerId + "']['stateChangeListener']");
    ...
}

, . addEventListener(), - ActionScript/Javascript .

: http://groups.google.com/group/youtube-api-gdata/browse_thread/thread/e8a8c85b801b9e25

+2

The @alalonde solution no longer works, as Adobe imposes additional restrictions on characters that can be used for Flash event listeners. You can no longer use [] or (), which makes previous solutions to this problem obsolete. These characters generate the following javascript error:

Identifier

starts right after the numeric literal

Here is my solution (using period characters):

var players = {}; // global to keep track of all players on the page
function onYouTubePlayerReady (idPlayer) {
   var ytPlayer = document.getElementById (idPlayer);
   var idPlayerParams = 'yt' + Math.floor (Math.random()*100000); // create random varable name
   players [idPlayerParams] = {
        idPlayer: idPlayer,
        onStateChanged: function (state) {
            alert ('youtube player state changed to: ' + state + ', player id: ' + idPlayer);
        },
        onError: function (err) {
            alert ('youtube player error: ' + err + ', player id: ' + idPlayer);
        }
    };
    ytPlayer.addEventListener ('onStateChange', 'players.' + idPlayerParams + '.onStateChanged');
    ytPlayer.addEventListener ('onError', 'players.' + idPlayerParams + '.onError');
}
+1
source

Source: https://habr.com/ru/post/1786003/


All Articles