Player - checking for if it's playing

Hi I'd like to know how you check if a RealizedPlayer is stopped or playing someting.//Thanks in advance
[119 byte] By [Consideratea] at [2007-11-27 6:45:34]
# 1
Post some code
tjacobs01a at 2007-7-12 18:17:31 > top of Java-index,Security,Cryptography...
# 2

The code needs to listen to the ControllerEvents*,

with particular attention to sub-classes like

StartEvent and EndOfMediaEvent.

*

http://java.sun.com/products/java-media/jmf/2.1.1/apidocs/javax/media/ControllerEvent.html

Check these code samples that deal with CE's.

http://www.google.com/search?q=controllerevent+site%3Ajava.sun.com+filetype%3Ajava

AndrewThompson64a at 2007-7-12 18:17:31 > top of Java-index,Security,Cryptography...
# 3

import javax.swing.*;

import java.awt.*;

import javax.media.*;

import java.awt.event.*;

import java.net.*;

import java.io.*;

public class myMusic

{

static Player helloJMFPlayer = null;

int Counter = 0;

public void getFile(File f)

{

try

{

stop();

// method using URL

URL url = f.toURL();

helloJMFPlayer = Manager.createRealizedPlayer( url);

Counter ++;

}

catch( Exception e)

{

System.out.println(" Unable to create the audioPlayer :" + e );

}

helloJMFPlayer.start();

}

public static void stop()

{

if(helloJMFPlayer != null)

{

helloJMFPlayer.stop();

helloJMFPlayer.close();

}

}

public static void main( String args[])

{

myMusic music = new myMusic();

}

}

Consideratea at 2007-7-12 18:17:31 > top of Java-index,Security,Cryptography...
# 4

I do not know what that code was supposed to achieve,

but try running this code and looking at the ouptut.

This code was originally posted by someone trying

to get a player to show all files in a directory, and they

needed the code to detect when each video ended.

This version is their code, with a few minor tweaks.

For it to work, you will either need to give it a

String indicating the name of a directory containing

videos, as a command line argument, or change

the default directory that is hard coded (see the

main() ).

import java.awt.BorderLayout;

import java.awt.Component;

import java.awt.Container;

import java.awt.event.ActionListener;

import java.io.File;

import javax.media.ConfigureCompleteEvent;

import javax.media.ControllerEvent;

import javax.media.ControllerListener;

import javax.media.EndOfMediaEvent;

import javax.media.Manager;

import javax.media.MediaLocator;

import javax.media.Processor;

import javax.media.RealizeCompleteEvent;

import javax.media.Time;

import javax.media.protocol.DataSource;

import javax.swing.JFrame;

import javax.swing.JPanel;

/**

* @author Stefan

* @author Andrew Thompson

*/

public class PlayFiles extends JFrame implements ControllerListener{

private Container cont;

private JPanel jPPlayer = new JPanel();

private File [] files;

private int nF = 0;

public static void main(String[] args) throws Exception

{

if(args.length == 1){

new PlayFiles(args[0]);

} else {

// change this to a directory containing videos!

new PlayFiles("F:/anim/mov6");

}

}

public PlayFiles(String loc) throws Exception{

super("Stafah Multi-File Player");

setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

File dir = new File(loc);

if(dir.isDirectory()){

cont = getContentPane();

jPPlayer.setLayout(new BorderLayout());

cont.add(jPPlayer);

playFiles(dir);

pack();

setVisible(true);

}

}

private boolean isVideoFile(File f){

boolean bRetVal = false;

String fName = f.getName();

if(fName.endsWith("mpeg") ||

fName.endsWith("mov") ||

fName.endsWith("avi")){

bRetVal = true;

}

return bRetVal;

}

public void controllerUpdate(ControllerEvent e)

{

Processor p = (Processor)e.getSourceController();

if(e instanceof ConfigureCompleteEvent){

System.out.println("ConfigureCompleteEvent");

p.setContentDescriptor(null);

p.realize();

}

else if(e instanceof RealizeCompleteEvent){

System.out.println("RealizeCompleteEvent");

try{

Component c = p.getVisualComponent();

if(c != null){

jPPlayer.add(c);

pack();

}

}catch(Exception eX){

eX.printStackTrace();

}

p.start();

validate();

}

else if(e instanceof EndOfMediaEvent){

System.out.println("EndOfMediaEvent");

Component c = p.getVisualComponent();

jPPlayer.remove(c);

validate();

p.removeControllerListener(this);

playNextFile();

}

}

private void playNextFile(){

nF++;

if(nF >= files.length){

nF = 0;

}

File f = files[nF];

if(isVideoFile(f)){

try{

playFile(f);

}catch(Exception eX){

eX.printStackTrace();

}

}

}

private void playFile(File f) throws Exception{

System.out.println("file - '" + f.getPath()+ "'");

MediaLocator mL= new MediaLocator("file:" + f.getCanonicalPath());

DataSource dS = Manager.createDataSource(mL);

Processor proc = Manager.createProcessor(dS);

proc.addControllerListener(this);

proc.configure();

}

private void playFiles(File dir) throws Exception{

files = dir.listFiles();

nF = 0;

//fnd the 1st video file

for(int i = 0; i < files.length; i++){

File f = files[i];

if(isVideoFile(f)){

playFile(f);

break;

}

nF++;

}

}

}

AndrewThompson64a at 2007-7-12 18:17:31 > top of Java-index,Security,Cryptography...
# 5

in addition to the ControllerUpdate, the getState() method will tell you what state it is in. So you can something along these lines:

if (myPlayer.getState() == javax.media.Player.STARTED) { ... }

// or

// operation will enter this if statement if your player is passed or at the realized state.

if (myPlayer.getState() >= javax.media.Player.REALIZED) {..}

might not be safe, but just an idea.

kdajania at 2007-7-12 18:17:31 > top of Java-index,Security,Cryptography...
# 6

> in addition to the ControllerUpdate, the getState()

> method will tell you what state it is in. So you can

> something along these lines:

>

> >

> if (myPlayer.getState() ==

> javax.media.Player.STARTED) { ... }

>

> // or

>

> // operation will enter this if statement if your

> player is passed or at the realized state.

> if (myPlayer.getState() >=

> javax.media.Player.REALIZED) {..}

>

>

> might not be safe, but just an idea.

Quite good idea I must say, this doesn't work though.

The reason? There is no variable in Player called STARTED or REALIZED.

Is there anyhow to get the sourcecode to jmf? If so please tell me and I might get an answer from just looking at it.

Consideratea at 2007-7-12 18:17:31 > top of Java-index,Security,Cryptography...
# 7
You are right, it is not in player because it is inherited from the Controller. So check out the API and you will see it. If you are using an IDE like Eclipse, just start typing javax.media.Player. and you will see the fields come up. I know they exist because I just used this method.
kdajania at 2007-7-12 18:17:31 > top of Java-index,Security,Cryptography...
# 8

Well, I've looked into it some more. The getState() method returns an int. That int does not change value by using method Player.stop(). So how much I even try this getState() method won't help me out. I've tried with .close() method as well but still no results, I might have to use some other method than getState() because I belive even if the Player is stopped it's still realized and I can't get away from that.

Consideratea at 2007-7-12 18:17:31 > top of Java-index,Security,Cryptography...
# 9

getState () will return the current state of the player. I'm sure you've read how during the process of creating the player it moves from as realizing, realized, prefetching...etc.

If you stop the player, you can close it, deallocate it, and I believe that will do the trick you are looking for.

Try it out and see what happens, you don't have much to loose. Print out the states everytime you do an operation on the player and see where you are.

Message was edited by:

kdajani

kdajania at 2007-7-12 18:17:31 > top of Java-index,Security,Cryptography...