* @return true If the play operation was successful, false otherwise
* @throws MediaActionException
* If an error occurs performing this action
*/
public boolean doPlay() throws MediaActionException {
Player player = _handler.getPlayer();
if (player != null) {
if (player.getState() == Player.STARTED) {
// Already playing, nothing to do
return false;
}
}
if (player == null) {
final MediaPlayerDemoScreen screen = _handler.getScreen();
if (screen == null) {
return false; // MediaPlayerDemo has closed
}
final PlaylistEntry entry = screen.getCurrentPlaylistEntry();
if (entry == null) {
return false; // No entry to play
}
final String url = entry.getURL();
try {
player = Manager.createPlayer(url);
} catch (final Exception e) {
throw new MediaActionException("unable to load media from "
+ url + ": " + e);
}
_handler.setPlayer(player);
player.addPlayerListener(_handler);
try {
player.realize();
} catch (final Exception e) {
throw new MediaActionException("unable to fetch media: " + e);
}
// Cause playback to begin as soon as possible once start()
// is called on the Player.
final StreamingBufferControl sbc =
(StreamingBufferControl) player
.getControl("net.rim.device.api.media.control.StreamingBufferControl");
sbc.setBufferTime(0);
Control control = player.getControl("VolumeControl");
if (control instanceof VolumeControl) {
final VolumeControl volumeControl = (VolumeControl) control;
_handler.setVolumeController(volumeControl);
// Unmute the application on the assumption that if the user
// asks to
// play a track they actually want to hear it.
try {
doMute(false);
} catch (final Exception e) {
_handler.setVolumeController(null);
control = null;
}
// Set the volume to match the last-selected level
try {
_handler.changeVolume(_handler.getVolume());
} catch (final Exception e) {
_handler.setVolumeController(null);
control = null;
}
}
}
if (player != null) {
try {
player.start();
} catch (final Exception e) {
throw new MediaActionException("unable start player: " + e);
}
}
return true;