1

I need to catch an event using the Brightcove API. When the video finishes to play I execute a function. I followed the API guide on the website but it's the first time I'm using Brightcove so I'm bit confused. Here's my code:

    <script language="JavaScript" type="text/javascript" src="http://admin.brightcove.com/js/BrightcoveExperiences.js"></script>

         <object id="myExperience1537710931001" class="BrightcoveExperience">
            <param name="bgcolor" value="#FFFFFF" />
            <param name="width" value="764" />
            <param name="height" value="455" />
            <param name="playerID" value="1537479248001" />
            <param name="playerKey" value="AQ~~,AAABZfMS9tk~,qKtLVPwo3pzgDkN5hMeILjqzKpujZdaw" />
            <param name="isVid" value="true" />
            <param name="isUI" value="true" />
            <param name="dynamicStreaming" value="true" />
            <param name="includeAPI" value="true" />
            <param name="templateLoadHandler" value="myTemplateLoaded" />
            <param name="templateReadyHandler" value="onTemplateReady" />
            <param name="@videoPlayer" value="1537710931001" />
        </object>


                <script type="text/javascript">

                    var player;
                    var modVP;
                    var modExp;
                    var modCon;

            function myTemplateLoaded(experienceID) {
                player = brightcove.api.getExperience(experienceID);
                modVP = player.getModule(brightcove.api.modules.APIModules.VIDEO_PLAYER);
                modExp = player.getModule(brightcove.api.modules.APIModules.EXPERIENCE);
                modCon = player.getModule(brightcove.api.modules.APIModules.CONTENT);
                modExp.addEventListener(brightcove.api.events.ExperienceEvent.TEMPLATE_READY, onTemplateReady);
            }

            function onTemplateReady(evt) {
                modVP.addEventListener(brightcove.api.events.MediaEvent.BEGIN, onMediaEventFired);
                modVP.addEventListener(brightcove.api.events.MediaEvent.CHANGE, onMediaEventFired);
                modVP.addEventListener(brightcove.api.events.MediaEvent.COMPLETE, onMediaEventFired);
                modVP.addEventListener(brightcove.api.events.MediaEvent.ERROR, onMediaEventFired);
                modVP.addEventListener(brightcove.api.events.MediaEvent.PLAY, onMediaEventFired);
                modVP.addEventListener(brightcove.api.events.MediaEvent.PROGRESS, onMediaProgressFired);
                modVP.addEventListener(brightcove.api.events.MediaEvent.STOP, onMediaEventFired);
            }


            function onMediaEventFired(evt) {
                if (evt.type === brightcove.api.events.MediaEvent.STOP) {
                    alert('Hello!!!!');
                }
            }
</script>

Not sure what I'm doing wrong but the alert doesn't show when the video has finished to play. Any idea?

Thanks
Mauro

Mauro74
  • 4,686
  • 15
  • 58
  • 80

1 Answers1

1

The callbacks work if you swap the player out. This would indicate you need to enable the API for your specified player. This can be done by editing the player settings within Brightcove admin (http://support.brightcove.com/en/docs/editing-settings-players).

You may also need to specify your handlers by adding params to the flash object, E.G.

<param name="templateLoadHandler" value="myTemplateLoaded" />

You will also need to change your event handler to test the event object type not data like so:

    function onMediaEventFired(evt) {

        if (evt.type === brightcove.api.events.MediaEvent.COMPLETE) {
            alert('Hello!!!!');
        }
    }
i_like_robots
  • 2,760
  • 2
  • 19
  • 23
  • I did as you suggested enabling the API in the settings of the player but it didn't work. Think there's something wrong in the last function trying to catch the event. function onMediaEventFired(evt){...} – Mauro74 Mar 30 '12 at 14:00
  • I have updated my answer, you must evaluate the event type, only some events will have data. – i_like_robots Mar 30 '12 at 14:22