PlayList
hello there;
I have created a program that can play a video file, but however i would like to extend it to play a list of video files consecutively ( in a loop).
please help me on that
thanks in advance
Here is the code:
import java.awt.*;
import java.awt.event.*;
import javax.media.*;
import javax.media.Player;
public class MyPlayer extends Frame implements ControllerListener
{
public static void main(String[] args)
{
new MyPlayer(args [0]);
}
private Player player;
private Panel panel;
public MyPlayer(String mediaFile)
{
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
if (player!=null) player.close();
System.exit(0);
}});
setTitle("Player");
panel=new Panel();
panel.setLayout(new BorderLayout());
add("Center", panel);
setVisible(true);
try
{
System.out.println("\n\n THE " + mediaFile);
MediaLocator ml=new MediaLocator("file:///"+mediaFile);
player=Manager.createPlayer(ml);
player.addControllerListener(this);
player.start();
}
catch(Exception x) { x.printStackTrace(); }
}
public void controllerUpdate(ControllerEvent ce)
{
Component visual, control;
int videoWidth=320, videoHeight=200, controlHeight=30;
if (ce instanceof RealizeCompleteEvent)
{
if ((visual=player.getVisualComponent())!=null)
{
Dimension size=visual.getPreferredSize();
videoWidth=size.width;
videoHeight=size.height;
panel.add("Center", visual);
}
if ((control=player.getControlPanelComponent())!=null)
{
controlHeight=control.getPreferredSize().height;
panel.add("South", control);
}
setSize(videoWidth+10, videoHeight+controlHeight+30);
validate();
}
}
}
[1974 byte] By [
helloboya] at [2007-11-26 21:55:08]

# 1
To make your app repeat the file add the following to your
public void controllerUpdate(ControllerEvent ce)
if (ce instanceof RealizeCompleteEvent)
{
....
}
else if(ce instanceof EndOfMediaEvent)
{
System.out.println("EndOfMediaEvent");
Player p = (Player)ce.getSourceController();
p.setMediaTime(new Time(0));
p.start();
}
# 2
Hello Stefan;
i think u didn't get right.
I'll try to make it simple and elaborate more on my problem.
let's say we have a list strings were inserted into JList. those strings represent the media files or the URL where media files are located.
the MediaLocator will read the first string from JList and passes the result to createPlaer() method. now the question, when the EndOfMediaEvent occurs, the next string in the should be passed to MediaLocator and then forward the ml to createPlayer() method to play the second media file, and so on.
Thanks in advance for being so supportive.
# 3
> Hello Stefan;
>
> i think u didn't get right.
> I'll try to make it simple and elaborate more on my
> problem.
>
> let's say we have a list strings were inserted into
> JList. those strings represent the media files or the
> URL where media files are located.
> the MediaLocator will read the first string from
> JList and passes the result to createPlaer() method.
> now the question, when the EndOfMediaEvent occurs,
> the next string in the should be passed to
> MediaLocator and then forward the ml to
> createPlayer() method to play the second media file,
> and so on.
>
> Thanks in advance for being so supportive.
Well, to be frank your first post was misleading. Anyways the solution to the question you asked.
1.) Have a index called currentsong
int currentsong
This index should hold the value of the index of the song url which is being played currently.
2.) After EndOfMediaEvent you have to add 1 to the currentsong fetch the next song url from the list.
Does that help?
null
# 4
Right, well since your piece of code didn't actually deal with a list, I wasn't sure what you were after
Anyhow at the EndOfMediaEvent that's when you get the next media URL & play it
Your code only shows the GUI being established by the instantiation of MyPlayer from main() with a string representing your media item
You need to modify your code so that you have methods/events which feed the core parts of your App with the media items from a list(Vector, Jlist , Array or whatever)
Take a look at the JAvadocs for MediaPlayer and setMediaLocator / setDataSource methods - they'll probably help
# 5
hello;
let's assume that I am using an array of strings as u can see in the code bellow. i tried to move the next file in the list, but however i failed and the current file is repeating itself.
here is code:
import java.awt.*;
import java.awt.event.*;
import javax.media.*;
import javax.media.Player;
public class MyPlayer extends Frame implements ControllerListener
{
private Player player;
private Panel panel;
private String [] urls = {"Alnil.mpeg","wob.mpeg", "love.mp3", "1.mp3"};
int currentsong = 0;
public static void main(String[] args)
{
new MyPlayer();
}
public MyPlayer()
{
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
if (player!=null) player.close();
System.exit(0);
}});
setTitle("Player");
panel=new Panel();
panel.setLayout(new BorderLayout());
add("Center", panel);
setVisible(true);
try
{
while(currentsong < 4)
{
System.out.println("\n\n THE " + urls[currentsong]);
MediaLocator ml=new MediaLocator("file:///"+urls[currentsong]);
player=Manager.createPlayer(ml);
player.addControllerListener(this);
player.start();
currentsong++;
}
}
catch(Exception x) { x.printStackTrace(); }
}
public void controllerUpdate(ControllerEvent ce)
{
Component visual, control;
int videoWidth=320, videoHeight=200, controlHeight=30;
if (ce instanceof RealizeCompleteEvent)
{
if ((visual=player.getVisualComponent())!=null)
{
Dimension size=visual.getPreferredSize();
videoWidth=size.width;
videoHeight=size.height;
panel.add("Center", visual);
}
if ((control=player.getControlPanelComponent())!=null)
{
controlHeight=control.getPreferredSize().height;
panel.add("South", control);
}
setSize(videoWidth+10, videoHeight+controlHeight+30);
validate();
}
else if(ce instanceof EndOfMediaEvent)
{
System.out.println("\nEndOfMediaEvent");
player = (Player)ce.getSourceController();
player.setMediaTime(new Time(0));
//player.start();
}
}
thanks
# 6
This is bad design. You have packed everything in the constructor.
import java.awt.*;
import java.awt.event.*;
import javax.media.*;
import javax.media.Player;
public class MyPlayer extends Frame implements ControllerListener
{
private Player player;
private Panel panel;
private String [] urls = {"Alnil.mpeg","wob.mpeg", "love.mp3", "1.mp3"};
int currentsong = 0;
public static void main(String[] args)
{
new MyPlayer();
}
public MyPlayer()
{
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
if (player!=null) player.close();
System.exit(0);
}});
setTitle("Player");
panel=new Panel();
panel.setLayout(new BorderLayout());
add("Center", panel);
setVisible(true);
try
{
System.out.println("\n\n THE " + urls[currentsong]);
MediaLocator ml=new MediaLocator("file:///"+urls[currentsong++]);
player=Manager.createRealizedPlayer(ml);
player.addControllerListener(this);
player.start();
}
catch(Exception x) { x.printStackTrace(); }
}
public void controllerUpdate(ControllerEvent ce)
{
Component visual, control;
int videoWidth=320, videoHeight=200, controlHeight=30;
if (ce instanceof RealizeCompleteEvent)
{
if ((visual=player.getVisualComponent())!=null)
{
Dimension size=visual.getPreferredSize();
videoWidth=size.width;
videoHeight=size.height;
panel.add("Center", visual);
}
if ((control=player.getControlPanelComponent())!=null)
{
controlHeight=control.getPreferredSize().height;
panel.add("South", control);
}
setSize(videoWidth+10, videoHeight+controlHeight+30);
validate();
}
else if(ce instanceof EndOfMediaEvent)
{
System.out.println("\nEndOfMediaEvent");
MediaLocator ml=new MediaLocator("file:///"+urls[currentsong++]);
player=Manager.createRealizedPlayer(ml);
player.start();
}
}
And your code lacks indentation too
Please post your code within [url="http://forum.java.sun.com/help.jspa?sec=formatting"]code tags [/url]
