public class MediaPlayer extends Object
Modifier and Type | Field and Description |
---|---|
static int |
PLAYSTATE_PAUSED
indicates MediaPlayer state is paused
|
static int |
PLAYSTATE_PLAYING
indicates MediaPlayer state is playing
|
static int |
PLAYSTATE_STOPPED
indicates MediaPlayer state is stopped
|
Constructor and Description |
---|
MediaPlayer()
Creates an MediaPlayer.
Call release() to release native resources after using the
MediaPlayer. |
Modifier and Type | Method and Description |
---|---|
int |
getCurrentPosition()
Gets the current playback position.
|
int |
getDuration()
Gets the total duration of the media source.
|
int |
getPlayState()
Returns play state.
|
boolean |
isPlaying()
Checks whether the MediaPlayer is playing.
|
void |
pause()
Pauses playback.
|
void |
prepare()
Prepares the player for playback, asynchronously.
When the media source is ready for playback, MediaPlayerListener.onPrepared() will be called. |
void |
release()
Releases the native MediaPlayer resources.
|
void |
reset()
Resets the MediaPlayer to its uninitialized state.
|
void |
seekTo(int ms)
Seeks to specified time position.
|
void |
setDataSource(String source)
Sets the data source.
|
void |
setMediaPlayerListener(MediaPlayerListener listener)
Register a callback to be invoked when the media source is ready, or the
seek operation is completed, or the end of a media source is reached.
|
void |
start()
Starts or resumes playback.
|
void |
stop()
Stops playback.
Need to call prepare before start again. |
public static final int PLAYSTATE_STOPPED
public static final int PLAYSTATE_PAUSED
public static final int PLAYSTATE_PLAYING
public MediaPlayer() throws IOException
release()
to release native resources after using the
MediaPlayer.IOException
- if failed to create playerpublic void setDataSource(String source) throws IOException
[local file] file:///sdcard/1.wav or /sdcard/1.wav [file in jar] res://1.wav [http URL] http://xx.com/1.wav
source
- the path of the file or URL of the dataIOException
public int getPlayState()
PLAYSTATE_STOPPED
,
PLAYSTATE_PAUSED
,
PLAYSTATE_PLAYING
public void prepare() throws IOException
MediaPlayerListener.onPrepared()
will be called.IOException
- if data source has not set or an I/O error occurssetMediaPlayerListener(org.joshvm.media.MediaPlayerListener)
public void start() throws IOException
MediaPlayerListener.onCompletion()
will be called.IOException
public void pause() throws IOException
IOException
public void stop() throws IOException
IOException
public void reset() throws IOException
IOException
public void release() throws IOException
IOException
public int getCurrentPosition()
public int getDuration()
public boolean isPlaying()
public void seekTo(int ms) throws IOException
MediaPlayerListener.onSeekComplete()
will be called.ms
- the offset in milliseconds from the start to seek toIOException
public void setMediaPlayerListener(MediaPlayerListener listener)
listener
- the callback that will be run.