Embedding Player with Javascript API - Callback?

Hi,

im trying to control an embedded stream player via javaScript. As basis I used the code from here:
https://github.com/justintv/Twitch-API/blob/master/player.md with minimal changes (needed to add “http://” to the source-pathes, somehow)
But it seems like the eventsCallback function “onPlayerEvent” is never getting called.
Already tried to replace “eventsCallback” with “playerCallback” ( Twitch Player JS API: Broken initCallback? ) but this won’t do the trick as well.
In consequence there won’t be an instance of the player to control the stream with player.playVideo(), player.mute() etc.

How to make the script invoke the defined callback function?
Is there another way to get an instance of the twitch player to use the functions player.playVideo(), player.mute()…?

thanks,

martin

The source code shown on the Twitch API is correct and functions properly.

“player” is a variable that is hooked to the object in the DOM. Unless player is in the scope you’re trying to use it, you won’t be able to access the variable. You can read more about variables in Javascript at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var

Thank you for your answer.
I think I didnt make clear enough which part isn’t working for me. Thats the code for now:

     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>

    <script>
        $(function () {
            window.onPlayerEvent = function (data) {
                console.log("LOG: on PlayerEvent!");
                data.forEach(function (event) {
                    console.log("LOG: foreach data");
                    if (event.event == "playerInit") {
                        console.log("LOG: playerInit!");
                        var player = $("#live_embed_player")[0];
                        player.playVideo();
                        player.mute();
                    }
                });
            }
            console.log("LOG: test");

            swfobject.embedSWF("http://www-cdn.jtvnw.net/swflibs/TwitchPlayer.swf", "twitch_embed_player", "640", "400", "11", null, {
                "eventsCallback": "onPlayerEvent",
                "embed": 1,
                "channel": "dreamleague",
                "auto_play": "true"
            }, {
                "allowScriptAccess": "always",
                "allowFullScreen": "true"
            });
        });
    </script>
</head>

<body>
    <div id="twitch_embed_player"></div>
</body>

</html>

it’s pretty much copy&pasted from the player.md documentation…
To show what I mean I added some console log calls.
When the file is opened in Firefox, the player loads, the stream starts and i see the control log output “LOG: test”. But thats it. No more console log outputs. (and the stream doesn’t get muted, too)
Thats why I came to the conclusion that the function “onPlayerEvent” never gets called. Maybe I missunderstand the code and it’s fine that the function isn’t called - then I would be happy about a little explanation about how the callback actually works.

I guess there is a slight error on the Twitch documentation:

live_embed_player should be twitch_embed_player

yeah I think you are right, it should be:

var player = $("#twitch_embed_player")[0];

but this still doesn’t change anything about the fact (at least in my case) that the script never jumps into the onPlayerEvent function/section so this line is actually never reached.

It works fine for me: http://nightdev.com/testing/embedcallback.html

1 Like

your solution worked for me, too.
-> I found my (very basic) mistake: Didn’t deploy my file on a webserver, so the script showed a different behavior.

Thank you for helping me find that mistake.

Was player.md just completely removed? Can someone explain what’s going on??

@syrah any news?

Yeap, apparently player.md disappeared from the face of the earth, and the old .swf that’s still up is broken now somehow ( loadStream(); doesn’t work anymore ) and it doesn’t look like someone’s going to fix it. I’m migrating all my current projects to the new html5 player (embed-video.md), as much of a pain in the ass that may be…

Yer I was lucky enough that I could just swap to the iframe code: