Go
Not registered? Sign up!

How to get state of player

Google Translate
8 posts | return to the JavaScript Interaction forum | get the rss feed for this thread

Nov. 07, 2009christy

Hello,
I want state of player.Is file IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
etc.I am using flash player and javascript.Please resolve my problem.
Thanx.

Nov. 07, 2009hobbs

 
See: http://developer.longtailvideo.com/trac/wiki/FlashEvents#Modelevents

You have two choices:

1) setup a Model Listener - the current STATE will be available as soon as it changes,

2) use player.getConfig()['state'] to get the current state when you need it.

You can add the Model Listener STATE like this:
    <script type="text/javascript">
      var player        =  null;
      var playlist      =  null;
      var currentState  =  null;


      function playerReady(obj)
      {
        player = gid(obj.id);
        addListeners();
      };


      function addListeners()
      {
        playlist = player.getPlaylist();

        if((playlist !== null) && (playlist.length > 0))
        {
          player.addModelListener('STATE', 'stateMonitor');
        }
        else
        {
          setTimeout("addListeners();", 100);
        }
      };


      function stateMonitor(obj)
      {
        currentState = obj.newstate;
        
      //...do something here based upon the state of the player

      //...only for testing
        alert('State: ' + obj.newstate);
      };


      function gid(name)
      {
        return document.getElementById(name);
      };
    </script>

Nov. 09, 2009christy

Hi hobbs,
I used the function given by you,but its not working as it is not getting playerReady obj. I included swfobject.js. what should I include more to get playerReady obj.
Thnx.

Nov. 09, 2009hobbs

 
How do you know that the player isn't calling playerReady()?

In your Flash embedding code, you have to use a player id in three places, the name attribute, the id attribute, and the id flashvar.

Post your full page code or a link to your Test Page so someone can help you further.

Nov. 09, 2009christy

hi hobbs,
Thanks for reply

Here is my code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>JW API Example 4-3-2-0 - JW FLV Media Player</title>

<script type="text/javascript" src="swfobject.js"></script>
<script type="text/javascript">

var currentState = "NONE";
var previousState = "NONE";

var player = null;
function playerReady(thePlayer) {
alert("id"+player.id);
player = document.getElementById(thePlayer.id);
addListeners();
}


function addListeners() {
if (player) {
player.addModelListener("STATE", "stateListener");
} else {
setTimeout("addListeners()",100);
}
}


function stateListener(obj) { //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
currentState = obj.newstate;
previousState = obj.oldstate;

alert(currentState);

var tmp = document.getElementById("stat");
if (tmp) {
tmp.innerHTML = "current state: " + currentState +
"<br>previous state: " + previousState;
}

if ((currentState == "COMPLETED")&&(previousState == "PLAYING")) {
document.location.href="http://www.longtailvideo.com/players/jw-flv-player/";
}
}


function createPlayer() {
var flashvars = {
file:"Ad_Samsung_AC_Z2.swf",
autostart:"true"
}

var params = {
allowfullscreen:"true",
allowscriptaccess:"always"
}

var attributes = {
id:"player1",
name:"player1"
}

swfobject.embedSWF("player.swf", "placeholder1", "320", "196", "9.0.115", false, flashvars, params, attributes);
}
</script>
</head>
<body onload="createPlayer();">

<a href="http://www.longtailvideo.com/players/jw-flv-player/">JW FLV Media Player</a><br>
Checking the ModelListener "State" and redirecting after playing.<br>
(Please click the "Back" button to return to this page.)<br>

<div id="placeholder1">
<a href="http://www.adobe.com/go/getflashplayer">Get flash</a> to see this player
</div>
<br>

<div id="stat"></div>

</body>
</html>

I have used the player id and name just once.
Any other places where we have to add id?

Nov. 09, 2009christy

hello ,
please help me to resolve my problem.

Nov. 09, 2009christy

Hello hobbs

I made some changes in the code which you have given.From which I am getting only buffering and playing state.But not getting completed state.How i will get that state.

Nov. 09, 2009hobbs

 
Sorry, it won't work with a SWF file. Convert Ad_Samsung_AC_Z2.swf to FLV or MP4.

Add a reaction

You can also return to the category or try this search for related threads.


 

Search the Forums

Go

Support

Support Here are some helpful links to learn more about the JW Player™:

Monetize Your Video

Monetize Your Video Earn money with ads from LongTail's AdSolution. Watch our demos and sign up now!

Why Buy a License?

Why Buy a License? If you don’t buy a commercial license, you cannot use a JW Player™ on (i) a site that has ads; (ii) a corporate site; or a (iii) CMS. Our licenses are very inexpensive, so what are you waiting for? Buy a license today.