MediaPlayer:API:Callbacks
Getting Started
The media player has many callbacks that can be defined in JavaScript to provide more granular control over the media player and how it interacts with certain actions.
Callbacks
Callbacks are simply global JavaScript functions that the media player tries to call. If they don't exist, then the media player skips over them. It is important to always return a value or the media player will assume it was unable to successfully complete the callback.
This callback is useful when the media player wants to redirect itself to a new URL and you need better control over how to perform this redirect rather than the default implementation offered by Flash.
Parameters
- url
- this is the URL to which the media player wants to redirect.
Example Usage
<script type="text/javascript"> metrix_navigateToURL = function(url) { document.location = url; return true; } </script>
metrix_isFullscreenDisabled
This callback is useful for when you want to control access to fullscreen mode. Sometimes this is desired during chat sessions, because fullscreen mode does not allow input from the keyboard.
Parameters
This function has no parameters.
Example Usage
<script type="text/javascript"> metrix_isFullscreenDisabled = function() { return false; } </script>
metrix_onStateChange
This callback notifies the viewer on the current state and previous state of the media player. This allows you to have control and detect errors more easily with playback.
Parameters
- state
- this is the current state of the media player.
- lastState
- this is the previous state of the media player.
Example Usage
<script type="text/javascript"> metrix_onStateChange = function(state, lastState) { if (state == "connectionError") { alert("A connection error has occurred."); } return true; } </script>
metrix_onDisplayChange
This callback notifies the viewer that a display change is taking place.
Parameters
- display
- this is the new display mode. Allowable values are "normal" and "fullscreen".
Example Usage
<script type="text/javascript"> metrix_onDisplayChange = function(display) { if (display == "fullscreen") { alert("Enjoy the show!"); } return true; } </script>
metrix_filterComObject
This callback is extremely important for doing a custom live event implementation. All of the live events go through this function. Returning true will prevent the event from occurring.
Parameters
- obj
- this is an object with several properties. Take a look at the example below to see the implementation details.
Example Usage
<script type="text/javascript"> metrix_filterComObject = function(obj) { var id = obj.id; var msgtype = parseInt(obj.type); var user = obj.user; var msg = obj.msg; var host = obj.host; switch(msgtype) { case MCONST_CHAT: { } break; case MCONST_ENTER: { } break; case MCONST_LEAVE: { } break; case MCONST_GIFT: { } break; case MCONST_ENTER_VOYEUR: { } break; case MCONST_ACCESS_PUBLIC: { } break; case MCONST_ACCESS_PRIVATE_EXCLUSIVE: { } break; case MCONST_ACCESS_PRIVATE_GROUP: { } break; case MCONST_MODE_CHANGE: { } break; case MCONST_SHUTDOWN: { } break; } return false; }; </script>
metrix_updateRoster
This callback allows you to build your own chat roster of who is viewing the live event.
Parameters
- user
- this is the username of the viewer being updated.
- host
- this is whether or not the viewer is the host.
- add
- this is a boolean determining if the viewer is entering or leaving the event.
Example Usage
<script type="text/javascript"> var roster = {}; metrix_updateRoster = function(user, host, add) { if (add) { roster[user] = true; } else { delete roster[user]; } return true; }
metrix_onPlayheadUpdate
This callback allows you to dynamically adjust your playhead cursor as your media plays.
Parameters
- obj
- this is an object.
- curTime
- this is the current time in seconds of the media playing.
- totalTime
- this is the total time in seconds of the media playing.
- playerId
- this is the HTML DOM id of the media player.
Example Usage
<script type="text/javascript"> metrix_onPlayheadUpdate = function(obj) { var complete = Math.floor(100 * obj.curTime / obj.totalTime); alert("This player is " + complete + "% complete."); return true; }
metrix_onLoad
This callback allows you know when the media player is ready for interaction.
Parameters
- obj
- this is an object.
- playerId
- this is the HTML DOM id of the media player.
Example Usage
<script type="text/javascript"> metrix_onLoad = function(obj) { alert("This player is ready."); return true; }
metrix_onComplete
This callback allows you know when the media player is done with playback for the current media.
Parameters
- obj
- this is an object.
- mode
- the mode param has the following values: pre and post. The pre value is before the next transition begins and the post value is after the next transition begins.
- playState
- this is actual state of the playback. Possible values are pre, play, or post. The pre/post roll functionality uses pre and post values.
- playerId
- this is the HTML DOM id of the media player.
Example Usage
<script type="text/javascript"> metrix_onComplete = function(obj) { alert("This player is done playing."); return true; }
metrix_onNoCamerasDetected
This callback allows you know when the media player is unable to detect any cameras.
Parameters
- obj
- this is an object.
- playerId
- this is the HTML DOM id of the media player.
Return Values
- false
- this stops the default execution for handling this callback
- true
- this continues with the default execution for handling this callback
Example Usage
<script type="text/javascript"> metrix_onNoCamerasDetected = function(obj) { alert("Sorry... it looks like you don't have a webcam."); return true; }
metrix_onBroadcastError
This callback allows you know when the media player is unable to successfully broadcast.
Parameters
- obj
- this is an object.
- playerId
- this is the HTML DOM id of the media player.
Return Values
- false
- this stops the default execution for handling this callback
- true
- this continues with the default execution for handling this callback
Example Usage
<script type="text/javascript">
metrix_onBroadcastError = function(obj) { alert("Sorry... it looks like there was a problem broadcasting."); return true; }
metrix_onBroadcastDenied
This callback allows you know when the media player is unable to get permission from the user to use the camera.
Parameters
- obj
- this is an object.
- playerId
- this is the HTML DOM id of the media player.
Return Values
- false
- this stops the default execution for handling this callback
- true
- this continues with the default execution for handling this callback
Example Usage
<script type="text/javascript">
metrix_onBroadcastError = function(obj) { alert("Sorry... it looks like you did not grant permission to use the camera."); return true; }
metrix_onBroadcastInit
This callback allows you know when the media player is about to prompt for camera settings, which leads into starting the broadcast. By returning false, you have the opportunity to create your own camera settings dialog in HTML. This can be used along with metrix_getBroadcastSettings and metrix_initBroadcast.
Parameters
- obj
- this is an object.
- playerId
- this is the HTML DOM id of the media player.
Return Values
- false
- this stops the default execution for handling this callback
- true
- this continues with the default execution for handling this callback
Example Usage
<script type="text/javascript">
metrix_onBroadcastError = function(obj) { alert("Sorry... it looks like you did not grant permission to use the camera."); return true; }