[Player-cvs] kit/jsr135 myplay.java,NONE,1.1

[Player-cvs] kit/jsr135 myplay.java,NONE,1.1

cdunn at helixcommunity.org cdunn at helixcommunity.org
Thu Dec 15 18:21:41 PST 2005


Update of /cvsroot/player/kit/jsr135
In directory cvs:/tmp/cvs-serv3798

Added Files:
	myplay.java 
Log Message:


--- NEW FILE: myplay.java ---

import java.util.*;
import java.lang.*;
import java.lang.Object.*;
import java.io.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;

class play {

    public static void main(String[] args) {
	    if (args.length < 1) {
		    System.out.println("Using default url..");
		    new MyPlayer().playUrl("rtsp://clientqa.dev.prognet.com/alice.rm");
		    //new MyPlayer().playUrl("c:\\src\\music\\laika.mp3");
		    //new MyPlayer().playUrl("rtsp://clientqa.dev.prognet.com/mp3/mario.mp3");
		    //new MyPlayer().playUrl("rtsp://clientqa.dev.prognet.com/mp3/crystalmethod.mp3");
		    return;
	    }
	    if (args.length < 1 || args[0].compareTo("-h") == 0) {
		    System.out.println("Usage: play [-volume] [<url>[<url>...]]");
		    return;
	    }
	    else if (args.length > 0 && args[0].compareTo("-volume") == 0) {
		    if (args.length > 1) {
		    	new MyPlayer().playUrlVolume(args[1]);
		    } else {
		    	new MyPlayer().playUrlVolume("c:\\src\\music\\laika.mp3");
		    }
		    return;
	    }
	    else if (args.length > 0 && args[0].compareTo("-video") == 0) {
		    if (args.length > 1) {
		    	new MyPlayer().playUrlVideo(args[1]);
		    } else {
		    	new MyPlayer().playUrlVideo("c:\\src\\music\\buena1.rm");
		    }
		    return;
	    }

	    MyPlayer mp = new MyPlayer();
	    for (int i=0; i<args.length; i++) {
		    mp.playUrl(args[i]);
	    }
    }

}

class MyPlayer implements javax.microedition.media.PlayerListener {

    public void playUrlVideo(String url) {
	 try {
	    Player p = Manager.createPlayer("rtsp://clientqa.dev.prognet.com/alice.rm");
	    p.prefetch();

	    VideoControl video;

	    video = (VideoControl)p.getControl("VideoControl");
	    video.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, null);
	    video.setVisible(true);

	    p.start();

	    System.out.println("getDisplayX: "+ video.getDisplayX());
	    System.out.println("getDisplayY: "+ video.getDisplayY());
	    System.out.println("getSourceWidth: " + video.getSourceWidth());
	    System.out.println("getSourceHeight: " + video.getSourceHeight());
	    System.out.println("getDisplayWidth: " + video.getDisplayWidth());
	    System.out.println("getDisplayHeight: " + video.getDisplayHeight());

	    int x = 50;
	    int y = 50;
	    System.out.println("setDisplayLocation: " + x + "," + y);
	    video.setDisplayLocation(x, y);

	    System.out.println("Sleep for 2 before displaysize...");
	    Thread.currentThread().sleep(4000);
	    int width = video.getDisplayWidth() * 2;
	    int height = video.getDisplayHeight() * 2;
	    System.out.println("setDisplaySize: " + width + "," + height);
	    video.setDisplaySize(width, height);

	    System.out.println("getDisplayX: "+ video.getDisplayX());
	    System.out.println("getDisplayY: "+ video.getDisplayY());
	    System.out.println("getSourceWidth: " + video.getSourceWidth());
	    System.out.println("getSourceHeight: " + video.getSourceHeight());
	    System.out.println("getDisplayWidth: " + video.getDisplayWidth());
	    System.out.println("getDisplayHeight: " + video.getDisplayHeight());

	    /*
	    System.out.println("setDisplayFullScreen: True");
	    video.setDisplayFullScreen(true);
	    */

	    System.out.println("Sleep for 2 before start...");
	    Thread.currentThread().sleep(4000);
	    p.start();
	    Thread.currentThread().sleep(15000);

	    p.stop();
	    p.close();
	 } catch (MediaException pe) {
	 } catch (IOException ioe) {
	 } catch (InterruptedException ie) {
	 }
    }
    public void playUrlVolume(String url) {
	 try {
	    Player p = Manager.createPlayer(url);
	    p.realize();
	    p.prefetch();


	    VolumeControl volume;
	    GUIControl gc;
	    VideoControl vidc;

	    volume= (VolumeControl)p.getControl("VolumeControl");
	    gc= (GUIControl)p.getControl("GUIControl");
	    vidc = (VideoControl)p.getControl("VideoControl");

	    Control[] c = p.getControls();
	    System.out.println("Controls: " + c);
	    for (int i=0; i<c.length; i++) {
		    System.out.println("control: " + c[i]);
	    }

	    volume.setMute(false);
	    volume.setLevel(100); 
	    p.start();
	    System.out.println("Playing clip for 5s at volume = " + volume.getLevel() + " muted=" + volume.isMuted());
	    Thread.currentThread().sleep(5000);
	    volume.setLevel(50); 
	    System.out.println("Playing clip for 5s at volume = " + volume.getLevel() + " muted=" + volume.isMuted());
	    Thread.currentThread().sleep(5000);
	    volume.setLevel(0); 
	    System.out.println("Playing clip for 5s at volume = " + volume.getLevel() + " muted=" + volume.isMuted());
	    Thread.currentThread().sleep(5000);
	    volume.setLevel(100); 
	    System.out.println("Playing clip for 5s at volume = " + volume.getLevel() + " muted=" + volume.isMuted());
	    Thread.currentThread().sleep(5000);
	    volume.setMute(true);
	    System.out.println("Playing clip for 5s at volume = " + volume.getLevel() + " muted=" + volume.isMuted());
	    Thread.currentThread().sleep(5000);
	    volume.setMute(false);
	    System.out.println("Playing clip for 5s at volume = " + volume.getLevel() + " muted=" + volume.isMuted());
	    Thread.currentThread().sleep(5000);
	    p.stop();
	    p.close();
	 } catch (MediaException pe) {
	 } catch (IOException ioe) {
	 } catch (InterruptedException ie) {
	 }
    }
	    

    public void playUrl(String url)
    {
	Player p;
	String line;
	BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	VideoControl video=null;
	VolumeControl volume=null;
	GUIControl gc;
	boolean visible = true;
	boolean fullscreen = false;

	try {
	    p = Manager.createPlayer(url);

	    /*
	    MyThread2 t2 = new MyThread2(p);
	    t2.start();

	    System.out.println("Before start in thread1 " +Thread.currentThread());
	    p.start();
	    System.out.println("After start in thread1 " +Thread.currentThread());
	    */

	    //p.start();
	    //System.out.println("AFTER start, sleeping for 20s");
	    //Thread.currentThread().sleep(20000);
	    //p.stop();

	    //Player p2 = Manager.createPlayer(url);
	    //p2.start();

	    p.addPlayerListener(this);

	    printUsage();
	    System.out.print(">>> ");

	    while ((line = in.readLine()) != null)
	    {
		if (line.compareTo("realize") == 0 ||
		    line.compareTo("r") == 0) {
			    p.realize();
		}
		else if (line.compareTo("prefetch") == 0 ||
		    line.compareTo("f") == 0) {
			    p.prefetch();
		}
		else if (line.compareTo("start") == 0 ||
		    line.compareTo("play") == 0 ||
		    line.compareTo("s") == 0) {
			    p.start();
		}
		else if (line.compareTo("stop") == 0 ||
		        line.compareTo("pause") == 0 ||
		        line.compareTo("p") == 0) {
			    p.stop();
		}
		else if (line.compareTo("deallocate") == 0 ||
		    line.compareTo("d") == 0) {
			    p.deallocate();
		}
		else if (line.compareTo("close") == 0 ||
		        line.compareTo("c") == 0) {
			    p.close();
		}
		else if (line.compareTo("q") == 0 ||
		        line.compareTo("quit") == 0) {
			    System.out.println("Qutting command loop, calling close...");
			    p.close();
			    break;
		}
		else if (line.compareTo("state") == 0 ||
		        line.compareTo("getState") == 0) {
			    System.out.println("Calling getState ...");
			    printStateString(p.getState());
		}
		else if (line.compareTo("getMediaTime") == 0 ||
		        line.compareTo("t") == 0) {
			System.out.println("Current media time: "+ p.getMediaTime());
		}
		else if (line.compareTo("setMediaTime") == 0 ||
		        line.compareTo("seek") == 0) {
			System.out.print("Enter seek time: ");
	    		line = in.readLine();
			int t = Integer.parseInt(line);
			System.out.println("Seeking: "+ t);
			System.out.println("Current media time: "+ p.setMediaTime(t));
		}
		else if (line.compareTo("getDuration") == 0 ||
		        line.compareTo("dur") == 0) {
			    System.out.println("Duration: " + p.getDuration());
		}
		else if (line.compareTo("getVolume") == 0) {
	    		volume = (VolumeControl)p.getControl("VolumeControl");
	    		System.out.println("Volume = " + volume.getLevel() + " muted=" + volume.isMuted());
		}
		else if (line.compareTo("getVideo") == 0) {
			if (p.getState() < Player.PREFETCHED) {
			    printStateString(p.getState());
				System.out.println("WARNING: player must be in the prefetched state.");
			}
		    	video = (VideoControl)p.getControl("VideoControl");
		    	video.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, null);
		    	video.setVisible(true);
			System.out.println("getDisplayX: "+ video.getDisplayX());
			System.out.println("getDisplayY: "+ video.getDisplayY());
			System.out.println("getSourceWidth: " + video.getSourceWidth());
			System.out.println("getSourceHeight: " + video.getSourceHeight());
			System.out.println("getDisplayWidth: " + video.getDisplayWidth());
			System.out.println("getDisplayHeight: " + video.getDisplayHeight());
		}
		else if (line.compareTo("volume") == 0 ||
		        line.compareTo("vol") == 0) {
		    if (volume != null) {
	    		System.out.println("Volume = " + volume.getLevel() + " muted=" + volume.isMuted());
		    }
		}
		else if (line.compareTo("mute") == 0) {
		    if (volume != null) {
			volume.setMute(!volume.isMuted());
	    		System.out.println("Volume = " + volume.getLevel() + " muted=" + volume.isMuted());
		    }
		}
		else if (line.compareTo("setLevel") == 0) {
		    if (volume != null) {
			System.out.print("Enter volume level (0-100): ");
	    		line = in.readLine();
			int t = Integer.parseInt(line);
			volume.setLevel(t); 
	    		System.out.println("Volume = " + volume.getLevel() + " muted=" + volume.isMuted());
		    }
		}
		else if (line.compareTo("video") == 0) {
		    if (video != null) {
			System.out.println("getDisplayX: "+ video.getDisplayX());
			System.out.println("getDisplayY: "+ video.getDisplayY());
			System.out.println("getSourceWidth: " + video.getSourceWidth());
			System.out.println("getSourceHeight: " + video.getSourceHeight());
			System.out.println("getDisplayWidth: " + video.getDisplayWidth());
			System.out.println("getDisplayHeight: " + video.getDisplayHeight());
		    }
		}
		else if (line.compareTo("setVisible") == 0) {
		    if (video != null) {
			visible = !visible;
			System.out.println("Setting visiblity: " + visible);
			video.setVisible(visible);
		    }
		}
		else if (line.compareTo("setDisplayLocation") == 0) {
		    if (video != null) {
			System.out.print("Enter (x, y): ");
	    		line = in.readLine();
			int x = Integer.parseInt(line);
			int y = Integer.parseInt(line);
	    		System.out.println("setDisplayLocation: " + x + "," + y);
	    		video.setDisplayLocation(x, y);
		    }
		}
		else if (line.compareTo("setDisplaySize") == 0) {
		    if (video != null) {
			System.out.print("Enter (w, h): ");
			int w, h;
	    		line = in.readLine();
			if (line.compareTo("d") == 0) {
	    			w = video.getDisplayWidth() * 2;
				h = video.getDisplayHeight() * 2;
			} else {
			    w = Integer.parseInt(line);
			    h = Integer.parseInt(line);
			}
				
	    		System.out.println("setDisplaySize: " + w + "," + h);
	    		video.setDisplaySize(w, h);
		    }
		}
		else if (line.compareTo("setDisplayFullScreen") == 0) {
		    if (video != null) {
			fullscreen = !fullscreen;
	    		System.out.println("setDisplayFullScreen: " + fullscreen);
			video.setDisplayFullScreen(fullscreen);
		    }
		}
		else if (line.compareTo("") == 0) {
			if (p.getState() != Player.CLOSED) {
				System.out.println("Current media time: "+ p.getMediaTime());
			}
		}
		else {
			printUsage();
		}

		printStateString(p.getState());
		System.out.print(">>> ");
	    }


	} 
//	catch (Exception e) {
//		System.out.println("Caught io exception");
//	}//catch (InterruptedException ie) {
	//	System.out.println("Caught io exception");
	//}
	catch (MediaException me) {
		System.out.println("Media Exception " + me);
	}
	catch (IllegalStateException ie) {
		System.out.println("Illegal State Exception " + ie);
	}
	catch (IOException ioe) {
		System.out.println("IO Exception " + ioe);
	}
    }

    private void printStateString(long state) {
	    System.out.print("Player state ("+state+") = ");
	    switch ((int)state) {
		    case Player.UNREALIZED :
			    System.out.println("UNREALIZED"); break;
		    case Player.REALIZED :
			    System.out.println("REALIZED"); break;
		    case Player.PREFETCHED :
			    System.out.println("PREFETCHED"); break;
		    case Player.STARTED :
			    System.out.println("STARTED"); break;
		    case Player.CLOSED :
			    System.out.println("CLOSED"); break;
		    default:
			    System.out.println("unknown state"); break;
	    }
    }

    private static void printUsage() {
		System.out.println("Enter a command:");
		System.out.println("	realize, r		: open url");
		System.out.println("	prefetch, f		: prefetch");
		System.out.println("	start, play, s		: start/play");
		System.out.println("	stop, pause, p		: stop/pause");
		System.out.println("	deallocate, d		: deallocate");
		System.out.println("	close, c		: close the player");
		System.out.println("	getState, state		: get the player state");
		System.out.println("	getMediaTime, t		: get the current media time");
		System.out.println("	setMediaTime, seek	: set the media time (seek)");
		System.out.println("	getDuration, len	: set the media duration (clip len)");
		System.out.println("	getVolume		: Get the volume control");
		System.out.println("	volume, vol		: Print volume info");
		System.out.println("	setMute, mute		: Toggle mute");
		System.out.println("	setLevel		: Set the volume level");
		System.out.println("	getVideo		: Get the video control and initialize");
		System.out.println("	video			: Print video info");
		System.out.println("	setVisible		: Toggle video visiblity");
		System.out.println("	setDisplayLocation	: Set the video display location");
		System.out.println("	setDisplaySize		: Set the video display size, enter 'd' to double");
		System.out.println("	setDisplayFullScreen	: Toggle fullscreen mode");
		System.out.println("	quit, q			: close the player, quit cmd loop");
    }

    public synchronized void playerUpdate(Player player, java.lang.String event, java.lang.Object eventData) {

	if (event.equals(PlayerListener.STARTED)) {
		System.out.println("playerUpdate: "+event); 
	} else if (event.equals( PlayerListener.BUFFERING_STARTED)) {
//		System.out.println("playerUpdate: "+event); 
	} else if (event.equals( PlayerListener.BUFFERING_STOPPED)) {
		System.out.println("playerUpdate: "+event); 
	} else if (event.equals( PlayerListener.CLOSED)) {
		System.out.println("playerUpdate: "+event); 
	} else if (event.equals( PlayerListener.DEVICE_AVAILABLE)) {
		System.out.println("playerUpdate: "+event); 
	} else if (event.equals( PlayerListener.DEVICE_UNAVAILABLE)) {
		System.out.println("playerUpdate: "+event); 
	} else if (event.equals( PlayerListener.DURATION_UPDATED)) {
		//System.out.println("playerUpdate: "+event); 
	} else if (event.equals( PlayerListener.END_OF_MEDIA)) {
		System.out.println("playerUpdate: "+event); 
	} else if (event.equals( PlayerListener.ERROR)) {
		System.out.println("playerUpdate: "+event); 
	} else if (event.equals( PlayerListener.RECORD_ERROR)) {
		System.out.println("playerUpdate: "+event); 
	} else if (event.equals( PlayerListener.RECORD_STARTED)) {
		System.out.println("playerUpdate: "+event); 
	} else if (event.equals( PlayerListener.RECORD_STOPPED)) {
		System.out.println("playerUpdate: "+event); 
	} else if (event.equals( PlayerListener.SIZE_CHANGED)) {
		System.out.println("playerUpdate: "+event); 
	} else if (event.equals( PlayerListener.STARTED)) {
		System.out.println("playerUpdate: "+event); 
	} else if (event.equals( PlayerListener.STOPPED)) {
		System.out.println("playerUpdate: "+event); 
	} else if (event.equals( PlayerListener.STOPPED_AT_TIME)) {
		System.out.println("playerUpdate: "+event); 
	} else if (event.equals( PlayerListener.VOLUME_CHANGED)) {
		System.out.println("playerUpdate: "+event); 
	} else {
		System.out.println("ERROR: unknown event: "+event);
	}
    }

}

class MyThread2 implements Runnable {
  private Player p;
  private Thread t;

  public void start() {
		t = new Thread(this);
		t.setName("MyThread2");
		t.start();
  }
	public MyThread2(Player p) {
		this.p = p;
	}

	public void run() {
		try {
			System.out.println("Before deallocate in thread2 " +Thread.currentThread());
			p.deallocate();
			System.out.println("After deallocate in thread2 " +Thread.currentThread());
		} catch (Exception e) {
		}
	}
}






More information about the Player-cvs mailing list
 

Site Map   |   Terms of Use   |   Privacy Policy   |   Contact Us

Copyright © 1995-2007 RealNetworks, Inc. All rights reserved. RealNetworks and Helix are trademarks of RealNetworks.
All other trademarks or registered trademarks are the property of their respective holders.