Code Search for Developers
 
 
  

MediaPlayer.java from GridBlocks at Krugle


Show MediaPlayer.java syntax highlighted

/*
 * Copyright (c) 2005
 * Helsinki Institute of Physics
 * see LICENSE file for details
 *
 * MediaPlayer.java
 * Created on Nov 5, 2003
 */

package fi.hip.gb.client;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.control.VolumeControl;

import fi.hip.gb.midlet.core.LiteResult;

/**
 * Plays audio files.
 * 
 * @author Juho Karppinen
 */
public class MediaPlayer extends GenericResultForm implements PlayerListener, ItemStateListener {
	/** player object */
	private Player player;
	/** playing status */
	private boolean playing = false;
	/** mime type used while playing WAV files */
	private String type = "audio/x-wav";

	/** position of the playback */	
	private Gauge gauge = new Gauge("Position: 0.0s / 0.0s", false, 100, 0);
	/** volumen of the playback */
	private Gauge volume = new Gauge("Volume: 50%", true, 10, 5);
	
	/** command for starting the playback */
	private Command playCommand = new Command("Play", Command.ITEM, 1);
	/** command for stopping the playback */
	private Command stopCommand = new Command("Stop", Command.ITEM, 1);
	
	private Timer progressTimer;
	
	public MediaPlayer(LiteResult resultAudio) {
		super("MediaPlayer: " + resultAudio.getName(), resultAudio);
/*
		try {
			InputStream is = new ByteArrayInputStream(_result.readBytes());
			_player = Manager.createPlayer(is, type);
			_player.addPlayerListener(this);
			_player.realize();
		} catch (IOException ioe) {
			discardPlayer();
			_midlet.showAlert("IOException",
				ioe.getMessage(), AlertType.ERROR, null);
		} catch (MediaException me) {
			discardPlayer();
			_midlet.showAlert("MediaException",
				me.getMessage(), AlertType.ERROR, null);
		} catch (SecurityException se) {
			discardPlayer();
			_midlet.showAlert("SecurityException",se.getMessage(),
				AlertType.ERROR, null);
		}
*/
		addCommand(playCommand);
		
		//_elapsed.setText("0.0s / 0.0s");
		
		if(this.result.getRecordID() == -1) {
			addCommand(saveCommand);
		}
		
		//append(_elapsed);
		append(gauge);
		append(volume);
		
		if(player != null) {
		}

		setCommandListener(this);
		setItemStateListener(this);
	}

	public void commandAction(Command c, Displayable disp) {
	    super.commandAction(c, disp);
		if ((c == playCommand)) {
			try {
				InputStream is = new ByteArrayInputStream(this.result.readBytes());
				player = Manager.createPlayer(is, type);
				player.addPlayerListener(this);
								
				player.realize();

				VolumeControl vol = (VolumeControl) player.getControl("VolumeControl");
				vol.setLevel(volume.getValue()*10);

				//if(_player.getDuration() != Player.TIME_UNKNOWN) {
					int ms = (int)(player.getDuration()/1000l);
					System.out.println("length " + ms);
					gauge.setMaxValue(ms);
				//}
				
				player.start();
				playingStarted();

			} catch (IOException ioe) {
				discardPlayer();
				MIDui.showException("Cannot play media file", ioe);
			} catch (MediaException me) {
				discardPlayer();
				MIDui.showException("Cannot play media file", me);
			} catch (SecurityException se) {
				discardPlayer();
				MIDui.showException("Cannot play media file", se);
			}
		} else if (c == stopCommand) {
			stop();
		}
	}

	public void itemStateChanged(Item item) {
		if(player != null && item == volume) {
			System.out.println("Volume " + volume.getValue() * 10 + "%");
			VolumeControl vol = (VolumeControl) player.getControl("VolumeControl");
			volume.setLabel("Volume: " + volume.getValue() * 10 + "%");
		}
	}
	
	private void stop() {
		if (player != null) player.close();
	}

	/**
	 * Called in case of exception to make sure invalid 
	 * players are closed
	 */
	private void discardPlayer() {
		if (player != null) {
			player.close();
			player = null;
		}
	}

	public void playerUpdate(final Player p, final String event, final Object eventData) {
		System.out.println(event +  eventData);
		
		// queue a call to updateEvent in the user interface event queue
		MIDui.getDisplay().callSerially(new Runnable() {
			public void run() {
				MediaPlayer.this.updateEvent(p, event, eventData);
			}
		});
	}

	private void updateEvent(Player p, String event, Object eventData) {
		if (event == END_OF_MEDIA) {
			p.close();
		} else if (event == CLOSED) {
			player = null;
			playingStopped();
		}
	}

	private void playingStarted() {
		removeCommand(playCommand);
		addCommand(stopCommand);
		playing = true;
		
		StatusUpdate updater = new StatusUpdate();
		progressTimer = new Timer();
		progressTimer.scheduleAtFixedRate(updater, 100, 100);
	}

	private void playingStopped() {
		removeCommand(stopCommand);
		addCommand(playCommand);
		playing = false;
		gauge.setValue(0);
	}
	
	//	a class to increment the value of a Gauge everytime it is run
	class StatusUpdate extends TimerTask {
		public void run() {
			int position = (int)(player.getMediaTime()/100000l);
			gauge.setValue(position);
			int posSec = position / 10;
			int maximum = (int)(player.getDuration()/100000l);
			int maxSec = maximum / 10;
			gauge.setLabel("Position: " + posSec + "."
				+ (position-posSec*10) + "s / " 
				+ maxSec + "." + + (maximum-maxSec*10) + "s");
			/*
			_elapsed.setText(posSec + "." + (position-posSec*10) + "s / " 
					+ maxSec + "." + + (maximum-maxSec*10) + "s");
			*/
			if(! playing) {
				System.out.println("Stopping playing");
				cancel();
			}
		}
	}
}



See more files for this project here

GridBlocks

GridBlocks builds a grid application framework via easy-to-use building blocks in distributed environment. The framework offers components for Grid security, distributed storage, computing, and Portlet web interfaces.

Project homepage: http://sourceforge.net/projects/gridblocks
Programming language(s): Java,JSP,XML
License: other

  BluetoothBrowser.java
  Broadcasts.java
  CameraCanvas.java
  Configs.java
  DispatchForm.java
  GPSForm.java
  GenericImageCanvas.java
  GenericResultForm.java
  GraphicsCanvas.java
  InfoForm.java
  InputUI.java
  JobsForm.java
  Logging.java
  MIDui.java
  MediaPlayer.java
  PictureCanvas.java
  ProgressForm.java
  Results.java
  StatusForm.java
  TextForm.java
  Thumbnails.java