de.enough.polish.multimedia
Class AudioPlayer

java.lang.Object
  extended by de.enough.polish.multimedia.AudioPlayer
All Implemented Interfaces:
javax.microedition.media.PlayerListener

public class AudioPlayer
extends java.lang.Object
implements javax.microedition.media.PlayerListener

Plays back audio files - at the moment this is only supported for MIDP 2.0 and devices that support the MMAPI

Copyright Enough Software 2006 - 2008

 history
        Nov 21, 2006 - rob creation
 

Author:
Robert Virkus, j2mepolish@enough.de

Field Summary
 
Fields inherited from interface javax.microedition.media.PlayerListener
CLOSED, DEVICE_AVAILABLE, DEVICE_UNAVAILABLE, DURATION_UPDATED, END_OF_MEDIA, ERROR, STARTED, STOPPED, VOLUME_CHANGED
 
Constructor Summary
AudioPlayer()
          Creates a new audio player with no default content type and no caching.
AudioPlayer(boolean doCachePlayer)
          Creates a new audio player with no default content type.
AudioPlayer(boolean doCachePlayer, java.lang.String contentType)
          Creates a new audio player with no listener
AudioPlayer(boolean doCachePlayer, java.lang.String contentType, javax.microedition.media.PlayerListener listener)
          Creates a new audio player
AudioPlayer(java.lang.String contentType)
          Creates a new audio player without caching and with no listener.
 
Method Summary
 void cleanUpPlayer()
          Closes and deallocates the player.
static java.lang.String getAudioType(java.lang.String type, java.lang.String protocol)
          Helper function for getting a supported media type.
 javax.microedition.media.Player getPlayer()
          Returns the original player.
 boolean isPlaying()
          Determines if the audio player is currently playing music
 void play()
          Plays back the last media again.
 void play(java.io.InputStream in)
          Plays the media taken from the specified input stream with the content type specified in the constructor.
 void play(java.io.InputStream in, java.lang.String type)
          Plays the media taken from the specified input stream.
 void play(java.lang.String url)
          Plays the media taken from the specified URL with the content type specified in the constructor.
 void play(java.lang.String url, java.lang.String type)
          Plays the media taken from the specified URL.
 void playerUpdate(javax.microedition.media.Player p, java.lang.String event, java.lang.Object data)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AudioPlayer

public AudioPlayer()
Creates a new audio player with no default content type and no caching.


AudioPlayer

public AudioPlayer(boolean doCachePlayer)
Creates a new audio player with no default content type.

Parameters:
doCachePlayer - caches the player even though the end of the media is reached

AudioPlayer

public AudioPlayer(java.lang.String contentType)
Creates a new audio player without caching and with no listener.

Parameters:
contentType - the type of the referenced media, this is being resolved to the phone's expected type automatically. You can, for example, use the type "audio/mp3" and this method resolves the type to "audio/mpeg3", if this is expected by the device.

AudioPlayer

public AudioPlayer(boolean doCachePlayer,
                   java.lang.String contentType)
Creates a new audio player with no listener

Parameters:
doCachePlayer - caches the player even though the end of the media is reached
contentType - the type of the referenced media, this is being resolved to the phone's expected type automatically. You can, for example, use the type "audio/mp3" and this method resolves the type to "audio/mpeg3", if this is expected by the device.

AudioPlayer

public AudioPlayer(boolean doCachePlayer,
                   java.lang.String contentType,
                   javax.microedition.media.PlayerListener listener)
Creates a new audio player

Parameters:
doCachePlayer - caches the player even though the end of the media is reached
contentType - the type of the referenced media, this is being resolved to the phone's expected type automatically. You can, for example, use the type "audio/mp3" and this method resolves the type to "audio/mpeg3", if this is expected by the device.
listener - an optional PlayerListener
Method Detail

play

public void play(java.lang.String url,
                 java.lang.String type)
          throws javax.microedition.media.MediaException,
                 java.io.IOException
Plays the media taken from the specified URL.

Parameters:
url - the URL of the media
type - the type of the referenced media, this is being resolved to the phone's expected type automatically. You can, for example, use the type "audio/mp3" and this method resolves the type to "audio/mpeg3", if this is expected by the device.
Throws:
javax.microedition.media.MediaException - when the media is not supported
java.io.IOException - when the URL cannot be resolved

play

public void play(java.io.InputStream in,
                 java.lang.String type)
          throws javax.microedition.media.MediaException,
                 java.io.IOException
Plays the media taken from the specified input stream.

Parameters:
in - the media input
type - the type of the referenced media, this is being resolved to the phone's expected type automatically. You can, for example, use the type "audio/mp3" and this method resolves the type to "audio/mpeg3", if this is expected by the device.
Throws:
javax.microedition.media.MediaException - when the media is not supported
java.io.IOException - when the input cannot be read

play

public void play(java.lang.String url)
          throws javax.microedition.media.MediaException,
                 java.io.IOException
Plays the media taken from the specified URL with the content type specified in the constructor.

Parameters:
url - the URL of the media
Throws:
javax.microedition.media.MediaException - when the media is not supported
java.io.IOException - when the URL cannot be resolved

play

public void play(java.io.InputStream in)
          throws javax.microedition.media.MediaException,
                 java.io.IOException
Plays the media taken from the specified input stream with the content type specified in the constructor.

Parameters:
in - the media input
Throws:
javax.microedition.media.MediaException - when the media is not supported
java.io.IOException - when the input cannot be read

play

public void play()
          throws javax.microedition.media.MediaException
Plays back the last media again. This can only be used when doCachePlayer is set to true in the constructor

Throws:
javax.microedition.media.MediaException - when the player cannot be started
See Also:
AudioPlayer(boolean), AudioPlayer(boolean, String), AudioPlayer(boolean, String, PlayerListener), AudioPlayer(String)

getPlayer

public javax.microedition.media.Player getPlayer()
Returns the original player.

Returns:
the original player, this can be null when no audio has been played back so far.

getAudioType

public static java.lang.String getAudioType(java.lang.String type,
                                            java.lang.String protocol)
Helper function for getting a supported media type.

Parameters:
type - the type like "audio/mp3"
protocol - the protocol, when null is given the content type will be returned for any protocol
Returns:
the type supported by the device, for example "audio/mpeg3" - null when the given type is not supported by the device.

isPlaying

public boolean isPlaying()
Determines if the audio player is currently playing music

Returns:
true when audio is played back

playerUpdate

public void playerUpdate(javax.microedition.media.Player p,
                         java.lang.String event,
                         java.lang.Object data)
Specified by:
playerUpdate in interface javax.microedition.media.PlayerListener

cleanUpPlayer

public void cleanUpPlayer()
Closes and deallocates the player.