Friday, June 4, 2010

VideoPlayer using J2ME

/*
* VideoPlayer.java
*
* Created on March 13, 2010, 6:26 PM
*/

import java.io.*;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
/**
*
* @author Administrator
* @version
*/
public class VideoPlayer extends MIDlet implements CommandListener,
PlayerListener {
// private PlayerPool pool;
private VolumeControl va;
protected int volume = 100;
private Display display;
private Ticker ticker;
private List itemList;
private Form form;
private Command stop, pause, start, Volume,Mute,Unmute;
private Hashtable items, itemsInfo;
private Gauge gauge;
private Player player;
private VideoControl vc;
public VideoPlayer() {
display = Display.getDisplay(this);
itemList = new List("Select an item to play", List.IMPLICIT);
stop = new Command("Stop", Command.STOP, 1);
pause = new Command("Pause", Command.ITEM, 1);
start = new Command("Start", Command.ITEM, 1);
Volume = new Command("Volume", Command.ITEM, 1);
Mute = new Command("Mute", Command.ITEM, 1);
Unmute = new Command("UnMute", Command.ITEM, 1);
form = new Form("Playing video");
form.addCommand(stop);
form.addCommand(pause);
form.addCommand(Volume);
form.addCommand(Mute);
form.setCommandListener(this);
items = new Hashtable();
itemsInfo = new Hashtable();
// You have to add the files in rsoure

items.put("SpringWaterFall...", "file://test-mpeg.mpg");
itemsInfo.put("SpringWaterFall...", "video/mpeg");

items.put("checkItOut...","file://hst_1.mpeg");
itemsInfo.put("checkItOut...","video/mpeg");

items.put("GordaCliff...", "file://GordaCliff.mpg");
itemsInfo.put("GordaCliff...", "video/mpeg");


items.put("centaur_1....","file://centaur_1.mpg");
itemsInfo.put("centaur_1....","video/mpeg");

}

public void startApp() {
for(Enumeration en = items.keys(); en.hasMoreElements();) {
itemList.append((String)en.nextElement(), null);
}
itemList.setCommandListener(this);
display.setCurrent(itemList);
}

public void pauseApp() {
try {
if(player != null) player.stop();
} catch(Exception e) {}
}

public void destroyApp(boolean unconditional) {
if(player != null) player.close();
}

public void commandAction(Command c, Displayable d){
if (c==Mute)
{
va = (VolumeControl)player.getControl( "VolumeControl" );
if(va != null)
{
va.setMute(true);
}
form.removeCommand(Mute);
form.addCommand(Unmute);
}else
{
if(c==Unmute)
va = (VolumeControl)player.getControl( "VolumeControl" );
if(va != null)
{
va.setMute(false);
}
form.removeCommand(Unmute);
form.addCommand(Mute);

}



if(c==Volume)
{
Gauge gauge=new Gauge("Volume",true,100,50);
form.append(gauge);
}
else
{
if(d instanceof List) {
List list = ((List)d);
String key = list.getString(list.getSelectedIndex());
try {
ticker =new Ticker(key);
playAudio((String)items.get(key), key);
} catch (Exception e) {
System.err.println("Unable to play: " + e);
e.printStackTrace();
}
} else if(d instanceof Form){
try {
if(c == stop){
player.close();
display.setCurrent(itemList);
form.removeCommand(start);
form.addCommand(pause);
} else if(c == pause){
player.stop();
form.removeCommand(pause);
form.addCommand(start);
} else if(c == start){
player.start();
form.removeCommand(start);
form.addCommand(pause);
}
} catch(Exception e) {
System.err.println(e);
}
}
}


}


private void playAudio(String locator, String key) throws Exception {
String file = locator.substring(locator.indexOf("file://") + 6,
locator.length());
player = Manager.createPlayer(getClass().getResourceAsStream(file),
(String)itemsInfo.get(key));
player.addPlayerListener(this);
player.setLoopCount(-1);
player.prefetch();
player.realize();
player.start();
form.setTicker(ticker);
}
public void keyPressed(int keyCode)
{
// int gameAction = getGameActionkey(keyCode);
if (keyCode ==1)
{
increaseVolume();
}
else if (keyCode == 6)
{
decreaseVolume();
}

}

public void playerUpdate(Player player, String event, Object eventData) {
if(event.equals(PlayerListener.STARTED) && new Long(0L).equals((Long)
eventData)) {
VideoControl vc = null;
if((vc = (VideoControl)player.getControl("VideoControl")) != null) {
Item videoDisp = (Item)vc.initDisplayMode(vc.USE_GUI_PRIMITIVE, null);
form.append(videoDisp);
//va=VolumeControl(
va = (VolumeControl)player.getControl( "VolumeControl" );
if(va != null)
{

va.setLevel(volume);
// va.setLevel(100);


}

//volumeControl.setLevel( curVolume );

// form.setTicker(ticker);
}
display.setCurrent(form);
} else if(event.equals(PlayerListener.CLOSED)) {
form.deleteAll();
}
}



private void increaseVolume()
{
volume += 10;
if (volume > 100)
{
volume = 100;
}
va.setLevel(volume);
System.out.println(volume);
}

private void decreaseVolume()
{
volume -= 10;
if (volume < 0)
{
volume = 0;
}
va.setLevel(volume);

}

}

No comments:

Post a Comment