Methods Summary |
---|
public void | addPlayerListener(PlayerListener playerListener)Add a player listener for this player.
chkClosed(false);
if (playerListener != null)
listeners.addElement(playerListener);
|
protected void | chkClosed(boolean unrealized)Check to see if the Player is closed. If the
unrealized boolean flag is true, check also to
see if the Player is UNREALIZED.
if (state == CLOSED || (unrealized && state == UNREALIZED)) {
throw new IllegalStateException("The Player is " +
(state == CLOSED ? "closed" : "unrealized"));
}
|
public synchronized void | close()Close the Player and release its resources.
When the method returns, the Player is in the
CLOSED state and can no longer be used.
A CLOSED event will be delivered to the registered
PlayerListener s.
If close is called on a closed Player
the request is ignored.
if (state == CLOSED)
return;
deallocate();
doClose();
state = CLOSED;
try {
if (stream != null)
stream.close();
} catch (IOException e) { }
sendEvent(PlayerListener.CLOSED, null);
mplayers.remove(new Integer(pID));
|
public synchronized void | deallocate()Release the scarce or exclusive
resources like the audio device acquired by the Player .
When deallocate returns, the Player
is in the UNREALIZED or REALIZED state.
If the Player is blocked at
the realize call while realizing, calling
deallocate unblocks the realize call and
returns the Player to the UNREALIZED state.
Otherwise, calling deallocate returns the
Player to the REALIZED state.
If deallocate is called when the Player
is in the UNREALIZED or REALIZED
state, the request is ignored.
If the Player is STARTED
when deallocate is called, deallocate
will implicitly call stop on the Player .
chkClosed(false);
loopAfterEOM = false;
if (state < PREFETCHED)
return;
if (state == STARTED)
stop();
doDeallocate();
state = REALIZED;
|
protected abstract void | doClose()the worker method to close the player
|
protected abstract void | doDeallocate()the worker method to deallocate the player
|
protected abstract Control | doGetControl(java.lang.String type)The worker method to actually obtain the control.
|
protected abstract long | doGetDuration()The actual worker method to retrieve the duration.
|
protected abstract long | doGetMediaTime()The actual worker method to gets this player's
current media time in microseconds.
|
synchronized void | doLoop()the worker method to deliver EOM event
// If a loop count is set, we'll loop back to the beginning.
if ((loopCount > 1) || (loopCount == -1)) {
try {
if (setMediaTime(0) == 0) {
if (loopCount > 1)
loopCount--;
start();
} else
loopCount = 1;
} catch (MediaException ex) {
loopCount = 1;
}
} else if (loopCountSet > 1)
loopCount = loopCountSet;
loopAfterEOM = false;
|
protected abstract void | doPrefetch()the worker method to prefetch the player
|
protected abstract void | doRealize()The worker method to realize the player.
|
protected abstract int | doSetLevel(int vol)The worker method to actually obtain the control.
|
protected abstract long | doSetMediaTime(long now)The worker method to actually set player's media time.
|
protected abstract boolean | doStart()The worker method to actually start the player
|
protected abstract void | doStop()the worker method to stop the player
|
public static com.sun.mmedia.BasicPlayer | get(int pid)Obtain a BasicPlayer instance based on the global id.
return (BasicPlayer)(mplayers.get(new Integer(pid)));
|
public Control | getControl(java.lang.String type)Gets the Control that supports the specified
class or interface. The full class
or interface name should be specified.
Null is returned if the Control
is not supported.
chkClosed(true);
// Prepend the package name if the type given does not
// have the package prefix.
if (type.indexOf('.") < 0)
return doGetControl("javax.microedition.media.control." + type);
return doGetControl(type);
|
public Control[] | getControls()Obtain the collection of Control s
from this player.
chkClosed(true);
return new Control[] { this };
|
public long | getDuration()Get the duration of the media.
The value returned is the media's duration
when played at the default rate.
If the duration cannot be determined (for example, the
Player is presenting live
media) getDuration returns TIME_UNKNOWN .
chkClosed(false);
return doGetDuration();
|
public int | getLevel()Get the current volume set for this
player.
return level;
|
public long | getMediaTime()Gets this Player 's current media time.
If the media time cannot be determined,
getMediaTime returns TIME_UNKNOWN .
chkClosed(false);
return doGetMediaTime();
|
public int | getState()Gets the current state of this Player .
The possible states are: UNREALIZED,
REALIZED, PREFETCHED, STARTED, CLOSED.
/**
* A race condition can occur between
* the return of this method and the execution of
* a state changing method.
*/
return state;
|
protected long | getStrmLoc()Get the current position of the source stream
return location;
|
public boolean | isMuted()Check if this player is muted.
return mute;
|
private void | openConnection()establish the connection with the source.
try {
HttpConnection httpCon = (HttpConnection)Connector.open(locator);
int rescode = httpCon.getResponseCode();
// both 4XX and 5XX are error codes
if (rescode >= 400) {
httpCon.close();
throw new IOException("bad url");
} else {
stream = httpCon.openInputStream();
String ctype = httpCon.getType();
boolean supportedCT = false;
if (locator.endsWith(".wav")) {
supportedCT = true;
} else if (ctype != null &&
ctype.toLowerCase().equals("audio/x-wav")) {
supportedCT = true;
}
httpCon.close();
if (!supportedCT) {
stream.close();
stream = null;
throw new MediaException("unsupported media type");
}
}
} catch (IOException ioex) {
throw ioex;
} catch (MediaException mex) {
throw mex;
} catch (Exception ex) {
new IOException(ex.getMessage() + " failed to connect");
}
location = 0;
|
public synchronized void | prefetch()Acquires the scarce and exclusive resources
and processes as much data as necessary
to reduce the start latency.
When prefetch completes successfully,
the Player is in
the PREFETCHED state.
If prefetch is called when the Player
is in the UNREALIZED state,
it will implicitly call realize .
If prefetch is called when the Player
is already in the PREFETCHED state, the Player
may still process data necessary to reduce the start
latency. This is to guarantee that start latency can
be maintained at a minimum.
If prefetch is called when the Player
is in the STARTED state,
the request will be ignored.
If the Player cannot obtain all
of the resources it needs, it throws a MediaException .
When that happens, the Player will not be able to
start. However, prefetch may be called again when
the needed resource is later released perhaps by another
Player or application.
chkClosed(false);
if (state >= PREFETCHED)
return;
if (state < REALIZED)
realize();
doPrefetch();
state = PREFETCHED;
|
protected int | readStrm(byte[] buffer, int offset, int length)Read a data buffer from source stream.
int len = stream.read(buffer, offset, length);
if (len > 0)
location += len;
return len;
|
public synchronized void | realize()Constructs portions of the Player without
acquiring the scarce and exclusive resources.
This may include examining media data and may
take some time to complete.
When realize completes successfully,
the Player is in the
REALIZED state.
If realize is called when the Player is in
the REALIZED, PREFETCHTED or STARTED state,
the request will be ignored.
chkClosed(false);
if (state >= REALIZED)
return;
doRealize();
state = REALIZED;
|
public void | removePlayerListener(PlayerListener playerListener)Remove a player listener for this player.
chkClosed(false);
listeners.removeElement(playerListener);
|
private void | reopenStrm()Re-open the source stream.
try {
stream.reset();
return;
} catch (IOException ex) {
if (locator == null)
throw ex;
}
try {
stream.close();
stream = null;
} catch (IOException e) {}
openConnection();
|
protected long | seekStrm(long where)In source stream, seek to a particular position
if (stream == null)
return location;
long skipped, oldLocation = location;
if (where < oldLocation) { // seek backward
reopenStrm();
location = 0;
skipped = stream.skip(where);
} else {
skipped = stream.skip((where - oldLocation));
}
if (skipped > 0)
location += skipped;
return location;
|
public void | sendEvent(java.lang.String evt, java.lang.Object evtData)Deliver the events to the player listeners.
// There's always one listener for EOM -- itself.
if (listeners.size() == 0 && evt != PlayerListener.END_OF_MEDIA)
return;
// Deliver the event to the listeners.
synchronized (evtLock) {
if (evtQ == null)
evtQ = new EvtQ(this);
evtQ.sendEvent(evt, evtData);
}
|
public int | setLevel(int ll)Set the volume using a linear point scale 0 to 100.
int newl;
if (ll < 0) {
ll = 0;
} else if (ll > 100) {
ll = 100;
}
if (!mute) {
newl = doSetLevel(ll);
if (newl != level) {
level = newl;
sendEvent(PlayerListener.VOLUME_CHANGED, this);
}
}
return level;
|
public void | setLocator(java.lang.String locator, boolean con)Set the locator of this player.
this.locator = locator;
if (con)
openConnection();
|
public void | setLoopCount(int count)Set the number of times the Player will loop
and play the content.
By default, the loop count is one. That is, once started,
the Player will start playing from the current
media time to the end of media once.
If the loop count is set to N where N is bigger than one,
starting the Player will start playing the
content from the current media time to the end of media.
It will then loop back to the beginning of the content
(media time zero) and play till the end of the media.
The number of times it will loop to the beginning and
play to the end of media will be N-1.
Setting the loop count to 0 is invalid. An
IllegalArgumentException will be thrown.
Setting the loop count to -1 will loop and play the content
indefinitely.
If the Player is stopped before the preset loop
count is reached either because stop is called,
calling start again will
resume the looping playback from where it was stopped until it
fully reaches the preset loop count.
An END_OF_MEDIA event will be posted
every time the Player reaches the end of media.
If the Player loops back to the beginning and
starts playing again because it has not completed the loop
count, a STARTED event will be posted.
chkClosed(false);
if (state == STARTED)
throw new IllegalStateException("setLoopCount");
if (count == 0 || count < -1)
throw new IllegalArgumentException("setLoopCount");
loopCountSet = count;
loopCount = count;
|
public synchronized long | setMediaTime(long now)Sets the Player 's media time.
For some media types, setting the media time may not be very
accurate. The returned value will indicate the
actual media time set.
Setting the media time to negative values will effectively
set the media time to zero. Setting the media time to
beyond the duration of the media will set the time to
the end of media.
There are some media types that cannot support the setting
of media time. Calling setMediaTime will throw
a MediaException in those cases.
chkClosed(true);
if (now < 0)
now = 0;
long theDur = getDuration();
if ((theDur != TIME_UNKNOWN) && (now > theDur))
now = theDur;
long rtn = doSetMediaTime(now);
EOM = false;
return rtn;
|
public void | setMute(boolean mute)set player mute.
if (mute && !this.mute) {
doSetLevel(0);
this.mute = true;
sendEvent(PlayerListener.VOLUME_CHANGED, this);
} else if (!mute && this.mute) {
this.level = doSetLevel(level);
this.mute = false;
sendEvent(PlayerListener.VOLUME_CHANGED, this);
}
|
public void | setStrm(java.io.InputStream stream)Set the input stream of this player.
this.stream = stream;
|
protected long | skipStrm(int numBytes)This is a skip fully method
long skipped = stream.skip(numBytes);
if (skipped > 0)
location += skipped;
if (skipped < numBytes)
throw new IOException("skipped over eom");
return (skipped);
|
public synchronized void | start()Starts the Player as soon as possible.
If the Player was previously stopped
by calling stop ,
it will resume playback
from where it was previously stopped. If the
Player has reached the end of media,
calling start will automatically
start the playback from the start of the media.
When start returns successfully,
the Player must have been started and
a STARTED event will
be delivered to the registered PlayerListener s.
However, the Player is not guaranteed to be in
the STARTED state. The Player may have
already stopped (in the PREFETCHED state) because
the media has 0 or a very short duration.
If start is called when the Player
is in the UNREALIZED or REALIZED state,
it will implicitly call prefetch .
If start is called when the Player
is in the STARTED state,
the request will be ignored.
chkClosed(false);
if (state >= STARTED)
return;
if (state < REALIZED)
realize();
if (state < PREFETCHED)
prefetch();
// If it's at the EOM, it will automatically
// loop back to the beginning.
if (EOM)
setMediaTime(0);
if (!doStart())
throw new MediaException("start");
state = STARTED;
sendEvent(PlayerListener.STARTED, new Long(getMediaTime()));
|
public synchronized void | stop()Stops the Player . It will pause the playback at
the current media time.
When stop returns, the Player is in the
PREFETCHED state.
A STOPPED event will be delivered to the registered
PlayerListener s.
If stop is called on
a stopped Player , the request is ignored.
chkClosed(false);
loopAfterEOM = false;
if (state < STARTED)
return;
doStop();
state = PREFETCHED;
sendEvent(PlayerListener.STOPPED, new Long(getMediaTime()));
|