public class PLMediaPlayer
extends java.lang.Object
This class will function the same as android.media.MediaPlayer in most cases. Please see Audio and Video for additional help using PLMediaPlayer.
One may need to declare a corresponding WAKE_LOCK permission
This class requires the Manifest.permission.INTERNET
permission
when used with network-based content.
Applications may want to register for informational and error events in order to be informed of some internal state update and possible runtime errors during playback or streaming. Registration for these events is done by properly setting the appropriate listeners.
setOnPreparedListener(OnPreparedListener)
,setOnVideoSizeChangedListener(OnVideoSizeChangedListener)
,setOnSeekCompleteListener(OnSeekCompleteListener)
,setOnCompletionListener(OnCompletionListener)
,setOnBufferingUpdateListener(OnBufferingUpdateListener)
,setOnInfoListener(OnInfoListener)
,setOnErrorListener(OnErrorListener)
PLMediaPlayer()
or PLMediaPlayer(AVOptions)
setDataSource(String)
or setDataSource(Context, Uri)
etc. setDisplay(SurfaceHolder)
or setSurface(Surface)
setOnPreparedListener(com.pili.pldroid.player.PLMediaPlayer.OnPreparedListener)
prepareAsync()
start()
/seekTo(long)
/pause()
/stop()
/etc. release()
Modifier and Type | Class and Description |
---|---|
static interface |
PLMediaPlayer.OnBufferingUpdateListener
Interface definition of a callback to be invoked indicating buffering
status of a media resource being streamed over the network.
|
static interface |
PLMediaPlayer.OnCompletionListener
Interface definition for a callback to be invoked when playback of
a media source has completed.
|
static interface |
PLMediaPlayer.OnErrorListener
Interface definition of a callback to be invoked when there
has been an error during an asynchronous operation (other errors
will throw exceptions at method call time).
|
static interface |
PLMediaPlayer.OnInfoListener
Interface definition of a callback to be invoked to communicate some
info and/or warning about the media or its playback.
|
static interface |
PLMediaPlayer.OnPreparedListener
Interface definition for a callback to be invoked when the media
source is ready for playback.
|
static interface |
PLMediaPlayer.OnSeekCompleteListener
Interface definition of a callback to be invoked indicating
the completion of a seek operation.
|
static interface |
PLMediaPlayer.OnVideoSizeChangedListener
Interface definition of a callback to be invoked when the
video size is first known or updated
|
Modifier and Type | Field and Description |
---|---|
static int |
ERROR_CODE_404_NOT_FOUND
404 resource not found
|
static int |
ERROR_CODE_CONNECTION_REFUSED
Connection refused
|
static int |
ERROR_CODE_CONNECTION_TIMEOUT
Connection timeout
|
static int |
ERROR_CODE_EMPTY_PLAYLIST
Playlist is empty
|
static int |
ERROR_CODE_INVALID_URI
Invalid URL.
|
static int |
ERROR_CODE_IO_ERROR
Network error
|
static int |
ERROR_CODE_STREAM_DISCONNECTED
Disconnected with the server
|
static int |
MEDIA_ERROR_UNKNOWN
Unspecified media player error.
|
static int |
MEDIA_INFO_AUDIO_RENDERING_START
The player just output the very first audio frame.
|
static int |
MEDIA_INFO_BUFFERING_BYTES_UPDATE
Buffered bytes are updated now.
|
static int |
MEDIA_INFO_BUFFERING_END
The player is resuming playback after filling buffers.
|
static int |
MEDIA_INFO_BUFFERING_START
The player is temporarily pausing playback internally in order to
buffer more data.
|
static int |
MEDIA_INFO_NOT_SEEKABLE
The media cannot be seek (e.g live stream)
|
static int |
MEDIA_INFO_UNKNOWN
Unspecified media player info.
|
static int |
MEDIA_INFO_VIDEO_RENDERING_START
The player just pushed the very first video frame for rendering.
|
static int |
MEDIA_INFO_VIDEO_ROTATION_CHANGED
The video rotation is changed
|
Constructor and Description |
---|
PLMediaPlayer()
Default constructor.
|
PLMediaPlayer(AVOptions options)
Constructor with
AVOptions . |
Modifier and Type | Method and Description |
---|---|
long |
getCurrentPosition()
Gets the current playback position.
|
java.lang.String |
getDataSource()
Gets data source.
|
long |
getDuration()
Gets the duration of the file or stream.
|
int |
getVideoHeight()
Returns the height of the video.
|
int |
getVideoWidth()
Returns the width of the video.
|
boolean |
isLooping()
Checks whether the PLMediaPlayer is looping or non-looping.
|
boolean |
isPlaying()
Checks whether the MediaPlayer is playing.
|
void |
pause()
Pauses playback.
|
void |
prepareAsync()
Prepares the player for playback, asynchronously.
|
void |
release()
Releases resources associated with this MediaPlayer object.
|
void |
reset()
Resets the PLMediaPlayer to its uninitialized state.
|
void |
seekTo(long msec)
Seeks to specified time position.
|
void |
setDataSource(android.content.Context context,
android.net.Uri uri)
Sets the data source as a content Uri.
|
void |
setDataSource(android.content.Context context,
android.net.Uri uri,
java.util.Map<java.lang.String,java.lang.String> headers)
Sets the data source as a content Uri.
|
void |
setDataSource(java.io.FileDescriptor fd)
Sets the data source (FileDescriptor) to use.
|
void |
setDataSource(java.lang.String path)
Sets the data source (file-path or http/rtsp/rtmp URL) to use.
|
void |
setDisplay(android.view.SurfaceHolder sh)
Sets the
SurfaceHolder to use for displaying the video
portion of the media. |
void |
setLooping(boolean looping)
Sets the player to be looping or non-looping.
|
void |
setOnBufferingUpdateListener(PLMediaPlayer.OnBufferingUpdateListener listener)
Register a callback to be invoked when the status of a network
stream's buffer has changed.
|
void |
setOnCompletionListener(PLMediaPlayer.OnCompletionListener listener)
Register a callback to be invoked when the end of a media source
has been reached during playback.
|
void |
setOnErrorListener(PLMediaPlayer.OnErrorListener listener)
Register a callback to be invoked when an error has happened
during an asynchronous operation.
|
void |
setOnInfoListener(PLMediaPlayer.OnInfoListener listener)
Register a callback to be invoked when an info/warning is available.
|
void |
setOnPreparedListener(PLMediaPlayer.OnPreparedListener listener)
Register a callback to be invoked when the media source is ready
for playback.
|
void |
setOnSeekCompleteListener(PLMediaPlayer.OnSeekCompleteListener listener)
Register a callback to be invoked when a seek operation has been
completed.
|
void |
setOnVideoSizeChangedListener(PLMediaPlayer.OnVideoSizeChangedListener listener)
Register a callback to be invoked when the video size is
known or updated.
|
void |
setScreenOnWhilePlaying(boolean screenOn)
Control whether we should use the attached SurfaceHolder to keep the
screen on while video playback is occurring.
|
void |
setSurface(android.view.Surface surface)
Sets the
Surface to be used as the sink for the video portion of
the media. |
void |
setVolume(float leftVolume,
float rightVolume)
Sets the volume on this player.
|
void |
setWakeMode(android.content.Context context,
int mode)
Set the low-level power management behavior for this MediaPlayer.
|
void |
start()
Starts or resumes playback.
|
void |
stop()
Stops playback.
|
public static final int MEDIA_INFO_UNKNOWN
public static final int MEDIA_INFO_VIDEO_RENDERING_START
public static final int MEDIA_INFO_BUFFERING_BYTES_UPDATE
public static final int MEDIA_INFO_BUFFERING_START
public static final int MEDIA_INFO_BUFFERING_END
public static final int MEDIA_INFO_NOT_SEEKABLE
public static final int MEDIA_INFO_VIDEO_ROTATION_CHANGED
public static final int MEDIA_INFO_AUDIO_RENDERING_START
public static final int MEDIA_ERROR_UNKNOWN
public static final int ERROR_CODE_INVALID_URI
public static final int ERROR_CODE_IO_ERROR
public static final int ERROR_CODE_STREAM_DISCONNECTED
public static final int ERROR_CODE_EMPTY_PLAYLIST
public static final int ERROR_CODE_404_NOT_FOUND
public static final int ERROR_CODE_CONNECTION_REFUSED
public static final int ERROR_CODE_CONNECTION_TIMEOUT
public PLMediaPlayer()
When done with the PLMediaPlayer, you should call release()
,
to free the resources.
public void release()
public void setDisplay(android.view.SurfaceHolder sh)
SurfaceHolder
to use for displaying the video
portion of the media.
Either a surface holder or surface must be set if a display or video sink
is needed. Not calling this method or setSurface(Surface)
when playing back a video will result in only the audio track being played.
A null surface holder or surface will result in only the audio track being
played.
sh
- the SurfaceHolder to use for video displaypublic void setSurface(android.view.Surface surface)
Surface
to be used as the sink for the video portion of
the media. This is similar to setDisplay(SurfaceHolder)
.
Setting a Surface will un-set any Surface or SurfaceHolder that was previously set.
A null surface will result in only the audio track being played.
If the Surface sends frames to a SurfaceTexture
, the timestamps
returned from SurfaceTexture.getTimestamp()
will have an
unspecified zero point. These timestamps cannot be directly compared
between different media sources, different instances of the same media
source, or multiple runs of the same program. The timestamp is normally
monotonically increasing and is unaffected by time-of-day adjustments,
but it is reset when the position is set.
surface
- The Surface
to be used for the video portion of the media.public void setWakeMode(android.content.Context context, int mode)
This function has the MediaPlayer access the low-level power manager
service to control the device's power usage while playing is occurring.
The parameter is a combination of PowerManager
wake flags.
Use of this method requires Manifest.permission.WAKE_LOCK
permission.
By default, no attempt is made to keep the device awake during playback.
context
- the Context to usemode
- the power/wake mode to setPowerManager
public void setScreenOnWhilePlaying(boolean screenOn)
setWakeMode(android.content.Context, int)
where possible, since it doesn't
require that the application have permission for low-level wake lock
access.screenOn
- Supply true to keep the screen on, false to allow it to turn off.public void setDataSource(android.content.Context context, android.net.Uri uri) throws java.io.IOException, java.lang.IllegalArgumentException, java.lang.SecurityException, java.lang.IllegalStateException
context
- the Context to use when resolving the Uriuri
- the Content URI of the data you want to playjava.io.IOException
- the io exceptionjava.lang.IllegalArgumentException
- the illegal argument exceptionjava.lang.SecurityException
- the security exceptionjava.lang.IllegalStateException
- if it is called in an invalid statepublic void setDataSource(android.content.Context context, android.net.Uri uri, java.util.Map<java.lang.String,java.lang.String> headers) throws java.io.IOException, java.lang.IllegalArgumentException, java.lang.SecurityException, java.lang.IllegalStateException
context
- the Context to use when resolving the Uriuri
- the Content URI of the data you want to playheaders
- the headers to be sent together with the request for the data Note that the cross domain redirection is allowed by default, but that can be changed with key/value pairs through the headers parameter with "android-allow-cross-domain-redirect" as the key and "0" or "1" as the value to disallow or allow cross domain redirection.java.io.IOException
- the io exceptionjava.lang.IllegalArgumentException
- the illegal argument exceptionjava.lang.SecurityException
- the security exceptionjava.lang.IllegalStateException
- if it is called in an invalid statepublic void setDataSource(java.lang.String path) throws java.io.IOException, java.lang.IllegalArgumentException, java.lang.SecurityException, java.lang.IllegalStateException
path
- the path of the file, or the http/rtsp/rtmp URL of the stream you want to playjava.io.IOException
- the io exceptionjava.lang.IllegalArgumentException
- the illegal argument exceptionjava.lang.SecurityException
- the security exceptionjava.lang.IllegalStateException
- if it is called in an invalid state When path
refers to a local file, the file may actually be opened by a process other than the calling application. This implies that the pathname should be an absolute path (as any other process runs with unspecified current working directory), and that the pathname should reference a world-readable file. As an alternative, the application could first open the file for reading, and then use the file descriptor form setDataSource(FileDescriptor)
.
public void setDataSource(java.io.FileDescriptor fd) throws java.io.IOException, java.lang.IllegalArgumentException, java.lang.IllegalStateException
fd
- the FileDescriptor for the file you want to playjava.io.IOException
- the io exceptionjava.lang.IllegalArgumentException
- the illegal argument exceptionjava.lang.IllegalStateException
- if it is called in an invalid statepublic java.lang.String getDataSource()
public void prepareAsync() throws java.lang.IllegalStateException
After setting the datasource and the display surface, you need to call prepareAsync().
This method starts preparing the media in the background and returns immediately.
When the media is done preparing, the onPrepared() method of the MediaPlayer.OnPreparedListener
is called, which is configured through setOnPreparedListener(OnPreparedListener)
java.lang.IllegalStateException
- if it is called in an invalid statepublic void setVolume(float leftVolume, float rightVolume)
AudioManager.setStreamVolume(int, int, int)
which sets the volume of ALL streams of
a particular type. Note that the passed volume values are raw scalars in range 0.0 to 1.0.
UI controls should be scaled logarithmically.leftVolume
- left volume scalarrightVolume
- right volume scalarpublic void start() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- if it is called in an invalid statepublic void pause() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- if the internal player engine has not been initialized.public void stop() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- if the internal player engine has not been initialized.public void seekTo(long msec) throws java.lang.IllegalStateException
msec
- the offset in milliseconds from the start to seek tojava.lang.IllegalStateException
- if the internal player engine has not been initializedpublic void reset()
public int getVideoWidth()
setOnVideoSizeChangedListener(OnVideoSizeChangedListener)
to provide a notification when the width is available.public int getVideoHeight()
setOnVideoSizeChangedListener(OnVideoSizeChangedListener)
to provide a notification when the height is available.public boolean isPlaying()
java.lang.IllegalStateException
- if the internal player engine has not been initialized or has been released.public long getCurrentPosition()
public long getDuration()
public void setLooping(boolean looping)
looping
- whether to loop or notpublic boolean isLooping()
public void setOnInfoListener(PLMediaPlayer.OnInfoListener listener)
listener
- the callback that will be runpublic void setOnPreparedListener(PLMediaPlayer.OnPreparedListener listener)
listener
- the callback that will be runpublic void setOnCompletionListener(PLMediaPlayer.OnCompletionListener listener)
listener
- the callback that will be runpublic void setOnBufferingUpdateListener(PLMediaPlayer.OnBufferingUpdateListener listener)
listener
- the callback that will be run.public void setOnSeekCompleteListener(PLMediaPlayer.OnSeekCompleteListener listener)
listener
- the callback that will be runpublic void setOnVideoSizeChangedListener(PLMediaPlayer.OnVideoSizeChangedListener listener)
listener
- the callback that will be runpublic void setOnErrorListener(PLMediaPlayer.OnErrorListener listener)
listener
- the callback that will be run