Changer de page automatiquement après la lecture d’un média avec JWPlayer.


Si vous comprenez de quoi je parle, vous aimerez le script qui suit. Il permet à votre site web de se rediriger à la page de votre choix lorsque la lecture du média est terminée. Un bouton permet de le désactiver au besoin. (crédits pour le code: Philibert Pérusse)
<!-- Customization script section -->
<script type="text/javascript">
<!--
var g_nextUrl = "The page you want to load after the media is finished";
var g_curMedia = "The media you want to play";
//-->
</script>
<img src="http://www.cheapettes.com/flash/toggle_01.png" align="right" onclick="javascript:OnButtonPlayNextMediaClick(this)" />
<!-- JW Player object creation sections -->
<script type="text/javascript" src="./swfobject.js"></script>
<script type="text/javascript">
var so = new SWFObject('./player.swf', 'MyPlayer', '220', '20', '9');
so.addParam('allowscriptaccess', 'always');
so.addParam('allowfullscreen', 'false');
so.addParam('flashvars', '&file='+g_curMedia+'&autostart=true');
so.write('MediaPlaybackLocation');
</script>
<!-- Management of openning another page upon completion of playback -->
<script type="text/javascript">
<!--
var g_bPlayNextMedia = true;
var g_player = null;
var g_timer = null;
// Callback function to manage the toggle button that allows choosing
// to continue playback. When the image is clicked, the state is changed.
function OnButtonPlayNextMediaClick(img)
{
g_bPlayNextMedia = !g_bPlayNextMedia;
if (g_bPlayNextMedia) {
img.src = "http://www.cheapettes.com/flash/toggle_01.png";
}
else {
img.src = "http://www.cheapettes.com/flash/toggle_02.png";
}
};
function LoadNextMedia() {
GotoUrl(g_nextUrl);
}
// Utility function
function GotoUrl($url)
{
if (parent != null) {
parent.location = $url;
}
else {
window.location = $url;
}
};
// Called automatically when the JWPlayer is finished loaded properly, so we keep
// a handle to the player object.
function playerReady(obj)
{
g_player = document.getElementById("MyPlayer");
};
// Called periodically, we check the player state and act accordingly
function OnTimerElapsed()
{
if (g_player != null) {
if (g_player.getConfig().state == "COMPLETED") {
// Make sure to "reset" the player state so it wont report
// its state as COMPLETED anymore. Just IDLE. This makes sure that
// if the button is clicked after the track has completed it does not
// move to the other media right away.
// It also makes sure the "back" button in the browser works fine.
g_player.sendEvent("STOP");
g_player.sendEvent("SEEK", 0);
if (g_bPlayNextMedia == true) {
LoadNextMedia();
}
}
}
};
// Called automatically when the page loads to start periodic verification of
// the player state.
function Initialize()
{
g_timer = setInterval(OnTimerElapsed, 250);
};
window.onload = Initialize();
//-->
</script>