MediaPlayer
open class MediaPlayer : AudioRouting, VolumeAutomation
kotlin.Any | |
↳ | android.media.MediaPlayer |
MediaPlayer class can be used to control playback of audio/video files and streams.
MediaPlayer is not thread-safe. Creation of and all access to player instances should be on the same thread. If registering callbacks, the thread must have a Looper.
Topics covered here are:
State Diagram
Playback control of audio/video files and streams is managed as a state machine. The following diagram shows the life cycle and the states of a MediaPlayer object driven by the supported playback control operations. The ovals represent the states a MediaPlayer object may reside in. The arcs represent the playback control operations that drive the object state transition. There are two types of arcs. The arcs with a single arrow head represent synchronous method calls, while those with a double arrow head represent asynchronous method calls.
From this state diagram, one can see that a MediaPlayer object has the following states:
- When a MediaPlayer object is just created using
new
or afterreset()
is called, it is in the Idle state; and afterrelease()
is called, it is in the End state. Between these two states is the life cycle of the MediaPlayer object.- There is a subtle but important difference between a newly constructed MediaPlayer object and the MediaPlayer object after
reset()
is called. It is a programming error to invoke methods such asgetCurrentPosition()
,getDuration()
,getVideoHeight()
,getVideoWidth()
,setAudioAttributes(android.media.AudioAttributes)
,setLooping(boolean)
,setVolume(float,float)
,pause()
,start()
,stop()
,seekTo(long,int)
,prepare()
orprepareAsync()
in the Idle state for both cases. If any of these methods is called right after a MediaPlayer object is constructed, the user supplied callback method OnErrorListener.onError() won't be called by the internal player engine and the object state remains unchanged; but if these methods are called right afterreset()
, the user supplied callback method OnErrorListener.onError() will be invoked by the internal player engine and the object will be transfered to the Error state. - You must keep a reference to a MediaPlayer instance to prevent it from being garbage collected. If a MediaPlayer instance is garbage collected,
release
will be called, causing any ongoing playback to stop. - You must call
release()
once you have finished using an instance to release acquired resources, such as memory and codecs. Once you have calledrelease
, you must no longer interact with the released instance. - MediaPlayer objects created using
new
is in the Idle state, while those created with one of the overloaded convenientcreate
methods are NOT in the Idle state. In fact, the objects are in the Prepared state if the creation usingcreate
method is successful.
- There is a subtle but important difference between a newly constructed MediaPlayer object and the MediaPlayer object after
- In general, some playback control operation may fail due to various reasons, such as unsupported audio/video format, poorly interleaved audio/video, resolution too high, streaming timeout, and the like. Thus, error reporting and recovery is an important concern under these circumstances. Sometimes, due to programming errors, invoking a playback control operation in an invalid state may also occur. Under all these error conditions, the internal player engine invokes a user supplied OnErrorListener.onError() method if an OnErrorListener has been registered beforehand via
setOnErrorListener(android.media.MediaPlayer.OnErrorListener)
.- It is important to note that once an error occurs, the MediaPlayer object enters the Error state (except as noted above), even if an error listener has not been registered by the application.
- In order to reuse a MediaPlayer object that is in the Error state and recover from the error,
reset()
can be called to restore the object to its Idle state. - It is good programming practice to have your application register a OnErrorListener to look out for error notifications from the internal player engine.
- IllegalStateException is thrown to prevent programming errors such as calling
prepare()
,prepareAsync()
, or one of the overloadedsetDataSource
methods in an invalid state.
- Calling
setDataSource(java.io.FileDescriptor)
, orsetDataSource(java.lang.String)
, orsetDataSource(android.content.Context,android.net.Uri)
, orsetDataSource(java.io.FileDescriptor,long,long)
, orsetDataSource(android.media.MediaDataSource)
transfers a MediaPlayer object in the Idle state to the Initialized state.- An IllegalStateException is thrown if setDataSource() is called in any other state.
- It is good programming practice to always look out for
IllegalArgumentException
andIOException
that may be thrown from the overloadedsetDataSource
methods.
- A MediaPlayer object must first enter the Prepared state before playback can be started.
- There are two ways (synchronous vs. asynchronous) that the Prepared state can be reached: either a call to
prepare()
(synchronous) which transfers the object to the Prepared state once the method call returns, or a call toprepareAsync()
(asynchronous) which first transfers the object to the Preparing state after the call returns (which occurs almost right away) while the internal player engine continues working on the rest of preparation work until the preparation work completes. When the preparation completes or whenprepare()
call returns, the internal player engine then calls a user supplied callback method, onPrepared() of the OnPreparedListener interface, if an OnPreparedListener is registered beforehand viasetOnPreparedListener(android.media.MediaPlayer.OnPreparedListener)
. - It is important to note that the Preparing state is a transient state, and the behavior of calling any method with side effect while a MediaPlayer object is in the Preparing state is undefined.
- An IllegalStateException is thrown if
prepare()
orprepareAsync()
is called in any other state. - While in the Prepared state, properties such as audio/sound volume, screenOnWhilePlaying, looping can be adjusted by invoking the corresponding set methods.
- There are two ways (synchronous vs. asynchronous) that the Prepared state can be reached: either a call to
- To start the playback,
start()
must be called. Afterstart()
returns successfully, the MediaPlayer object is in the Started state.isPlaying()
can be called to test whether the MediaPlayer object is in the Started state.- While in the Started state, the internal player engine calls a user supplied OnBufferingUpdateListener.onBufferingUpdate() callback method if a OnBufferingUpdateListener has been registered beforehand via
setOnBufferingUpdateListener(android.media.MediaPlayer.OnBufferingUpdateListener)
. This callback allows applications to keep track of the buffering status while streaming audio/video. - Calling
start()
has no effect on a MediaPlayer object that is already in the Started state.
- While in the Started state, the internal player engine calls a user supplied OnBufferingUpdateListener.onBufferingUpdate() callback method if a OnBufferingUpdateListener has been registered beforehand via
- Playback can be paused and stopped, and the current playback position can be adjusted. Playback can be paused via
pause()
. When the call topause()
returns, the MediaPlayer object enters the Paused state. Note that the transition from the Started state to the Paused state and vice versa happens asynchronously in the player engine. It may take some time before the state is updated in calls toisPlaying()
, and it can be a number of seconds in the case of streamed content.- Calling
start()
to resume playback for a paused MediaPlayer object, and the resumed playback position is the same as where it was paused. When the call tostart()
returns, the paused MediaPlayer object goes back to the Started state. - Calling
pause()
has no effect on a MediaPlayer object that is already in the Paused state.
- Calling
- Calling
stop()
stops playback and causes a MediaPlayer in the Started, Paused, Prepared or PlaybackCompleted state to enter the Stopped state.- Once in the Stopped state, playback cannot be started until
prepare()
orprepareAsync()
are called to set the MediaPlayer object to the Prepared state again. - Calling
stop()
has no effect on a MediaPlayer object that is already in the Stopped state.
- Once in the Stopped state, playback cannot be started until
- The playback position can be adjusted with a call to
seekTo(long,int)
.- Although the asynchronuous
seekTo(long,int)
call returns right away, the actual seek operation may take a while to finish, especially for audio/video being streamed. When the actual seek operation completes, the internal player engine calls a user supplied OnSeekComplete.onSeekComplete() if an OnSeekCompleteListener has been registered beforehand viasetOnSeekCompleteListener(android.media.MediaPlayer.OnSeekCompleteListener)
. - Please note that
seekTo(long,int)
can also be called in the other states, such as Prepared, Paused and PlaybackCompleted state. WhenseekTo(long,int)
is called in those states, one video frame will be displayed if the stream has video and the requested position is valid. - Furthermore, the actual current playback position can be retrieved with a call to
getCurrentPosition()
, which is helpful for applications such as a Music player that need to keep track of the playback progress.
- Although the asynchronuous
- When the playback reaches the end of stream, the playback completes.
- If the looping mode was being set to true with
setLooping(boolean)
, the MediaPlayer object shall remain in the Started state. - If the looping mode was set to false , the player engine calls a user supplied callback method, OnCompletion.onCompletion(), if a OnCompletionListener is registered beforehand via
setOnCompletionListener(android.media.MediaPlayer.OnCompletionListener)
. The invoke of the callback signals that the object is now in the PlaybackCompleted state. - While in the PlaybackCompleted state, calling
start()
can restart the playback from the beginning of the audio/video source.
Valid and invalid states
Method Name Valid States Invalid States Comments attachAuxEffect {Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted} {Idle, Error} This method must be called after setDataSource. Calling it does not change the object state. getAudioSessionId any {} This method can be called in any state and calling it does not change the object state. getCurrentPosition {Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted} {Error} Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state. getDuration {Prepared, Started, Paused, Stopped, PlaybackCompleted} {Idle, Initialized, Error} Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state. getVideoHeight {Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted} {Error} Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state. getVideoWidth {Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted} {Error} Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state. isPlaying {Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted} {Error} Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state. pause {Started, Paused, PlaybackCompleted} {Idle, Initialized, Prepared, Stopped, Error} Successful invoke of this method in a valid state transfers the object to the Paused state. Calling this method in an invalid state transfers the object to the Error state. prepare {Initialized, Stopped} {Idle, Prepared, Started, Paused, PlaybackCompleted, Error} Successful invoke of this method in a valid state transfers the object to the Prepared state. Calling this method in an invalid state throws an IllegalStateException. prepareAsync {Initialized, Stopped} {Idle, Prepared, Started, Paused, PlaybackCompleted, Error} Successful invoke of this method in a valid state transfers the object to the Preparing state. Calling this method in an invalid state throws an IllegalStateException. release any {} After release()
, you must not interact with the object.reset {Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted, Error} {} After reset()
, the object is like being just created.seekTo {Prepared, Started, Paused, PlaybackCompleted} {Idle, Initialized, Stopped, Error} Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state. setAudioAttributes {Idle, Initialized, Stopped, Prepared, Started, Paused, PlaybackCompleted} {Error} Successful invoke of this method does not change the state. In order for the target audio attributes type to become effective, this method must be called before prepare() or prepareAsync(). setAudioSessionId {Idle} {Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted, Error} This method must be called in idle state as the audio session ID must be known before calling setDataSource. Calling it does not change the object state. setAudioStreamType (deprecated) {Idle, Initialized, Stopped, Prepared, Started, Paused, PlaybackCompleted} {Error} Successful invoke of this method does not change the state. In order for the target audio stream type to become effective, this method must be called before prepare() or prepareAsync(). setAuxEffectSendLevel any {} Calling this method does not change the object state. setDataSource {Idle} {Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted, Error} Successful invoke of this method in a valid state transfers the object to the Initialized state. Calling this method in an invalid state throws an IllegalStateException. setDisplay any {} This method can be called in any state and calling it does not change the object state. setSurface any {} This method can be called in any state and calling it does not change the object state. setVideoScalingMode {Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted} {Idle, Error} Successful invoke of this method does not change the state. setLooping {Idle, Initialized, Stopped, Prepared, Started, Paused, PlaybackCompleted} {Error} Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state. isLooping any {} This method can be called in any state and calling it does not change the object state. setOnBufferingUpdateListener any {} This method can be called in any state and calling it does not change the object state. setOnCompletionListener any {} This method can be called in any state and calling it does not change the object state. setOnErrorListener any {} This method can be called in any state and calling it does not change the object state. setOnPreparedListener any {} This method can be called in any state and calling it does not change the object state. setOnSeekCompleteListener any {} This method can be called in any state and calling it does not change the object state. setPlaybackParams {Initialized, Prepared, Started, Paused, PlaybackCompleted, Error} {Idle, Stopped} This method will change state in some cases, depending on when it's called. setScreenOnWhilePlaying any {} This method can be called in any state and calling it does not change the object state. setVolume {Idle, Initialized, Stopped, Prepared, Started, Paused, PlaybackCompleted} {Error} Successful invoke of this method does not change the state. setWakeMode any {} This method can be called in any state and calling it does not change the object state. start {Prepared, Started, Paused, PlaybackCompleted} {Idle, Initialized, Stopped, Error} Successful invoke of this method in a valid state transfers the object to the Started state. Calling this method in an invalid state transfers the object to the Error state. stop {Prepared, Started, Stopped, Paused, PlaybackCompleted} {Idle, Initialized, Error} Successful invoke of this method in a valid state transfers the object to the Stopped state. Calling this method in an invalid state transfers the object to the Error state. getTrackInfo {Prepared, Started, Stopped, Paused, PlaybackCompleted} {Idle, Initialized, Error} Successful invoke of this method does not change the state. addTimedTextSource {Prepared, Started, Stopped, Paused, PlaybackCompleted} {Idle, Initialized, Error} Successful invoke of this method does not change the state. selectTrack {Prepared, Started, Stopped, Paused, PlaybackCompleted} {Idle, Initialized, Error} Successful invoke of this method does not change the state. deselectTrack {Prepared, Started, Stopped, Paused, PlaybackCompleted} {Idle, Initialized, Error} Successful invoke of this method does not change the state. Permissions
One may need to declare a corresponding WAKE_LOCK permission
<uses-permission>
element.This class requires the
android.Manifest.permission#INTERNET
permission when used with network-based content.Callbacks
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 (via calls to
setOnPreparedListener
,setOnVideoSizeChangedListener
,setOnSeekCompleteListener
,setOnCompletionListener
,setOnBufferingUpdateListener
,setOnInfoListener
,setOnErrorListener
, etc). In order to receive the respective callback associated with these listeners, applications are required to create MediaPlayer objects on a thread with its own Looper running (main UI thread by default has a Looper running). - If the looping mode was being set to true with
Summary
Nested classes | |
---|---|
Encapsulates the DRM properties of the source. |
|
Thrown when a DRM method is called before preparing a DRM scheme through prepareDrm(). |
|
abstract |
Interface definition of a callback to be invoked indicating buffering status of a media resource being streamed over the network. |
abstract |
Interface definition for a callback to be invoked when playback of a media source has completed. |
abstract |
Interface definition of a callback to be invoked when the app can do DRM configuration (get/set properties) before the session is opened. |
abstract |
Interface definition of a callback to be invoked when the DRM info becomes available |
abstract |
Interface definition of a callback to notify the app when the DRM is ready for key request/response |
abstract |
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). |
abstract |
Interface definition of a callback to be invoked to communicate some info and/or warning about the media or its playback. |
abstract |
Interface definition of a callback to be invoked when discontinuity in the normal progression of the media time is detected. |
abstract |
Interface definition for a callback to be invoked when the media source is ready for playback. |
abstract |
Interface definition of a callback to be invoked indicating the completion of a seek operation. |
abstract |
Interface definition of a callback to be invoked when a player subtitle track has new subtitle data available. |
abstract |
Interface definition of a callback to be invoked when a track has timed metadata available. |
abstract |
Interface definition of a callback to be invoked when a timed text is available for display. |
abstract |
Interface definition of a callback to be invoked when the video size is first known or updated |
Thrown when the device requires DRM provisioning but the provisioning attempt has failed due to a network error (Internet reachability, timeout, etc.). |
|
Thrown when the device requires DRM provisioning but the provisioning attempt has failed due to the provisioning server denying the request. |
|
open |
Class for MediaPlayer to return each audio/video/subtitle track's metadata. |
Constants | |
---|---|
static Int |
File or network related operation errors. |
static Int |
Bitstream is not conforming to the related coding standard or file spec. |
static Int |
The video is streamed and its container is not valid for progressive playback i. |
static Int |
Media server died. |
static Int |
Some operation takes too long to complete, usually more than 3-5 seconds. |
static Int |
Unspecified media player error. |
static Int |
Bitstream is conforming to the related coding standard or file spec, but the media framework does not support the feature. |
static Int |
Informs that audio is not playing. |
static Int |
Bad interleaving means that a media has been improperly interleaved or not interleaved at all, e. |
static Int |
MediaPlayer is resuming playback after filling buffers. |
static Int |
MediaPlayer is temporarily pausing playback internally in order to buffer more data. |
static Int |
A new set of metadata is available. |
static Int |
The media cannot be seeked (e.g live stream) |
static Int |
The player was started because it was used as the next player for another player, which just completed playback. |
static Int |
Reading the subtitle track takes too long. |
static Int |
Unspecified media player info. |
static Int |
Subtitle track was not supported by the media framework. |
static Int |
Informs that video is not playing. |
static Int |
The player just pushed the very first video frame for rendering. |
static Int |
The video is too complex for the decoder: it can't decode frames fast enough. |
static String |
MIME type for SubRip (SRT) container. |
static Int |
The DRM preparation has failed . |
static Int |
The device required DRM provisioning but couldn't reach the provisioning server. |
static Int |
The device required DRM provisioning but the provisioning server denied the request. |
static Int |
The status codes for |
static Int |
This mode is used with |
static Int |
This mode is used with |
static Int |
This mode is used with |
static Int |
This mode is used with |
static Int |
Specifies a video scaling mode. |
static Int |
Specifies a video scaling mode. |
Public constructors | |
---|---|
Default constructor. |
|
MediaPlayer(context: Context) Default constructor with context. |
Public methods | |
---|---|
open Unit |
addOnRoutingChangedListener(listener: AudioRouting.OnRoutingChangedListener!, handler: Handler!) Adds an |
open Unit |
addTimedTextSource(context: Context!, uri: Uri!, mimeType: String!) Adds an external timed text source file (Uri). |
open Unit |
addTimedTextSource(fd: FileDescriptor!, mimeType: String!) Adds an external timed text source file (FileDescriptor). |
open Unit |
addTimedTextSource(fd: FileDescriptor!, offset: Long, length: Long, mime: String!) Adds an external timed text file (FileDescriptor). |
open Unit |
addTimedTextSource(path: String!, mimeType: String!) Adds an external timed text source file. |
open Unit |
attachAuxEffect(effectId: Int) Attaches an auxiliary effect to the player. |
open Unit |
Clears the listener previously set with |
open Unit |
Clears the listener previously set with |
open static MediaPlayer! |
Convenience method to create a MediaPlayer for a given Uri. |
open static MediaPlayer! |
create(context: Context!, uri: Uri!, holder: SurfaceHolder!) Convenience method to create a MediaPlayer for a given Uri. |
open static MediaPlayer! |
create(context: Context!, uri: Uri!, holder: SurfaceHolder!, audioAttributes: AudioAttributes!, audioSessionId: Int) Same factory method as |
open static MediaPlayer! |
Convenience method to create a MediaPlayer for a given resource id. |
open static MediaPlayer! |
create(context: Context!, resid: Int, audioAttributes: AudioAttributes!, audioSessionId: Int) Same factory method as |
open VolumeShaper |
createVolumeShaper(configuration: VolumeShaper.Configuration) Returns a |
open Unit |
deselectTrack(index: Int) Deselect a track. |
open Int |
Returns the audio session ID. |
open Int |
Gets the current playback position. |
open MediaPlayer.DrmInfo! |
Retrieves the DRM Info associated with the current source |
open String |
getDrmPropertyString(propertyName: String) Read a DRM engine plugin String property value, given the property name string. |
open Int |
Gets the duration of the file. |
open MediaDrm.KeyRequest |
getKeyRequest(keySetId: ByteArray?, initData: ByteArray?, mimeType: String?, keyType: Int, optionalParameters: MutableMap<String!, String!>?) A key request/response exchange occurs between the app and a license server to obtain or release keys used to decrypt encrypted content. |
open PersistableBundle! |
Return Metrics data about the current player. |
open PlaybackParams |
Gets the playback params, containing the current playback rate. |
open AudioDeviceInfo! |
Returns the selected output specified by |
open AudioDeviceInfo! |
Returns an |
open Int |
getSelectedTrack(trackType: Int) Returns the index of the audio, video, or subtitle track currently selected for playback, The return value is an index into the array returned by |
open SyncParams |
Gets the A/V sync mode. |
open MediaTimestamp? |
Get current playback position as a |
open Array<MediaPlayer.TrackInfo!>! |
Returns an array of track information. |
open Int |
Returns the height of the video. |
open Int |
Returns the width of the video. |
open Boolean |
Checks whether the MediaPlayer is looping or non-looping. |
open Boolean |
Checks whether the MediaPlayer is playing. |
open Unit |
pause() Pauses playback. |
open Unit |
prepare() Prepares the player for playback, synchronously. |
open Unit |
Prepares the player for playback, asynchronously. |
open Unit |
prepareDrm(uuid: UUID) Prepares the DRM for the current source |
open ByteArray! |
provideKeyResponse(keySetId: ByteArray?, response: ByteArray) A key response is received from the license server by the app, then it is provided to the DRM engine plugin using provideKeyResponse. |
open Unit |
release() Releases resources associated with this MediaPlayer object. |
open Unit |
Releases the DRM session |
open Unit |
Removes an |
open Unit |
reset() Resets the MediaPlayer to its uninitialized state. |
open Unit |
restoreKeys(keySetId: ByteArray) Restore persisted offline keys into a new session. |
open Unit |
Seeks to specified time position. |
open Unit |
Moves the media to specified time position by considering the given mode. |
open Unit |
selectTrack(index: Int) Selects a track. |
open Unit |
setAudioAttributes(attributes: AudioAttributes!) Sets the audio attributes for this MediaPlayer. |
open Unit |
setAudioSessionId(sessionId: Int) Sets the audio session ID. |
open Unit |
setAudioStreamType(streamtype: Int) Sets the audio stream type for this MediaPlayer. |
open Unit |
setAuxEffectSendLevel(level: Float) Sets the send level of the player to the attached auxiliary effect. |
open Unit |
setDataSource(context: Context, uri: Uri) Sets the data source as a content Uri. |
open Unit |
setDataSource(context: Context, uri: Uri, headers: MutableMap<String!, String!>?) Sets the data source as a content Uri. |
open Unit |
setDataSource(context: Context, uri: Uri, headers: MutableMap<String!, String!>?, cookies: MutableList<HttpCookie!>?) Sets the data source as a content Uri. |
open Unit |
Sets the data source (AssetFileDescriptor) to use. |
open Unit |
setDataSource(dataSource: MediaDataSource!) Sets the data source (MediaDataSource) to use. |
open Unit |
setDataSource(fd: FileDescriptor!) Sets the data source (FileDescriptor) to use. |
open Unit |
setDataSource(fd: FileDescriptor!, offset: Long, length: Long) Sets the data source (FileDescriptor) to use. |
open Unit |
setDataSource(path: String!) Sets the data source (file-path or http/rtsp URL) to use. |
open Unit |
setDisplay(sh: SurfaceHolder!) Sets the |
open Unit |
setDrmPropertyString(propertyName: String, value: String) Set a DRM engine plugin String property value. |
open Unit |
setLooping(looping: Boolean) Sets the player to be looping or non-looping. |
open Unit |
setNextMediaPlayer(next: MediaPlayer!) Set the MediaPlayer to start when this MediaPlayer finishes playback (i.e. reaches the end of the stream). |
open Unit |
Register a callback to be invoked when the status of a network stream's buffer has changed. |
open Unit |
Register a callback to be invoked when the end of a media source has been reached during playback. |
open Unit |
setOnDrmConfigHelper(listener: MediaPlayer.OnDrmConfigHelper!) Register a callback to be invoked for configuration of the DRM object before the session is created. |
open Unit |
setOnDrmInfoListener(listener: MediaPlayer.OnDrmInfoListener!) Register a callback to be invoked when the DRM info is known. |
open Unit |
setOnDrmInfoListener(listener: MediaPlayer.OnDrmInfoListener!, handler: Handler!) Register a callback to be invoked when the DRM info is known. |
open Unit |
Register a callback to be invoked when the DRM object is prepared. |
open Unit |
setOnDrmPreparedListener(listener: MediaPlayer.OnDrmPreparedListener!, handler: Handler!) Register a callback to be invoked when the DRM object is prepared. |
open Unit |
setOnErrorListener(listener: MediaPlayer.OnErrorListener!) Register a callback to be invoked when an error has happened during an asynchronous operation. |
open Unit |
setOnInfoListener(listener: MediaPlayer.OnInfoListener!) Register a callback to be invoked when an info/warning is available. |
open Unit |
Sets the listener to be invoked when a media time discontinuity is encountered. |
open Unit |
setOnMediaTimeDiscontinuityListener(listener: MediaPlayer.OnMediaTimeDiscontinuityListener, handler: Handler) Sets the listener to be invoked when a media time discontinuity is encountered. |
open Unit |
setOnPreparedListener(listener: MediaPlayer.OnPreparedListener!) Register a callback to be invoked when the media source is ready for playback. |
open Unit |
Register a callback to be invoked when a seek operation has been completed. |
open Unit |
Sets the listener to be invoked when a subtitle track has new data available. |
open Unit |
setOnSubtitleDataListener(listener: MediaPlayer.OnSubtitleDataListener, handler: Handler) Sets the listener to be invoked when a subtitle track has new data available. |
open Unit |
Register a callback to be invoked when a selected track has timed metadata available. |
open Unit |
Register a callback to be invoked when a timed text is available for display. |
open Unit |
Register a callback to be invoked when the video size is known or updated. |
open Unit |
setPlaybackParams(params: PlaybackParams) Sets playback rate using |
open Boolean |
setPreferredDevice(deviceInfo: AudioDeviceInfo!) Specifies an audio device (via an |
open Unit |
setScreenOnWhilePlaying(screenOn: Boolean) Control whether we should use the attached SurfaceHolder to keep the screen on while video playback is occurring. |
open Unit |
setSurface(surface: Surface!) Sets the |
open Unit |
setSyncParams(params: SyncParams) Sets A/V sync mode. |
open Unit |
setVideoScalingMode(mode: Int) Sets video scaling mode. |
open Unit |
Sets the volume on this player. |
open Unit |
setWakeMode(context: Context!, mode: Int) Set the low-level power management behavior for this MediaPlayer. |
open Unit |
start() Starts or resumes playback. |
open Unit |
stop() Stops playback after playback has been started or paused. |
Protected methods | |
---|---|
open Unit |
finalize() |
Constants
MEDIA_ERROR_IO
static val MEDIA_ERROR_IO: Int
File or network related operation errors.
Value: -1004
MEDIA_ERROR_MALFORMED
static val MEDIA_ERROR_MALFORMED: Int
Bitstream is not conforming to the related coding standard or file spec.
Value: -1007
MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK
static val MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK: Int
The video is streamed and its container is not valid for progressive playback i.e the video's index (e.g moov atom) is not at the start of the file.
Value: 200
MEDIA_ERROR_SERVER_DIED
static val MEDIA_ERROR_SERVER_DIED: Int
Media server died. In this case, the application must release the MediaPlayer object and instantiate a new one.
Value: 100
MEDIA_ERROR_TIMED_OUT
static val MEDIA_ERROR_TIMED_OUT: Int
Some operation takes too long to complete, usually more than 3-5 seconds.
Value: -110
MEDIA_ERROR_UNKNOWN
static val MEDIA_ERROR_UNKNOWN: Int
Unspecified media player error.
Value: 1
MEDIA_ERROR_UNSUPPORTED
static val MEDIA_ERROR_UNSUPPORTED: Int
Bitstream is conforming to the related coding standard or file spec, but the media framework does not support the feature.
Value: -1010
MEDIA_INFO_AUDIO_NOT_PLAYING
static val MEDIA_INFO_AUDIO_NOT_PLAYING: Int
Informs that audio is not playing. Note that playback of the video is not interrupted.
Value: 804
MEDIA_INFO_BAD_INTERLEAVING
static val MEDIA_INFO_BAD_INTERLEAVING: Int
Bad interleaving means that a media has been improperly interleaved or not interleaved at all, e.g has all the video samples first then all the audio ones. Video is playing but a lot of disk seeks may be happening.
Value: 800
MEDIA_INFO_BUFFERING_END
static val MEDIA_INFO_BUFFERING_END: Int
MediaPlayer is resuming playback after filling buffers.
Value: 702
MEDIA_INFO_BUFFERING_START
static val MEDIA_INFO_BUFFERING_START: Int
MediaPlayer is temporarily pausing playback internally in order to buffer more data.
Value: 701
MEDIA_INFO_METADATA_UPDATE
static val MEDIA_INFO_METADATA_UPDATE: Int
A new set of metadata is available.
Value: 802
MEDIA_INFO_NOT_SEEKABLE
static val MEDIA_INFO_NOT_SEEKABLE: Int
The media cannot be seeked (e.g live stream)
Value: 801
MEDIA_INFO_STARTED_AS_NEXT
static val MEDIA_INFO_STARTED_AS_NEXT: Int
The player was started because it was used as the next player for another player, which just completed playback.
Value: 2
MEDIA_INFO_SUBTITLE_TIMED_OUT
static val MEDIA_INFO_SUBTITLE_TIMED_OUT: Int
Reading the subtitle track takes too long.
Value: 902
MEDIA_INFO_UNKNOWN
static val MEDIA_INFO_UNKNOWN: Int
Unspecified media player info.
Value: 1
MEDIA_INFO_UNSUPPORTED_SUBTITLE
static val MEDIA_INFO_UNSUPPORTED_SUBTITLE: Int
Subtitle track was not supported by the media framework.
Value: 901
MEDIA_INFO_VIDEO_NOT_PLAYING
static val MEDIA_INFO_VIDEO_NOT_PLAYING: Int
Informs that video is not playing. Note that playback of the audio is not interrupted.
Value: 805
MEDIA_INFO_VIDEO_RENDERING_START
static val MEDIA_INFO_VIDEO_RENDERING_START: Int
The player just pushed the very first video frame for rendering.
Value: 3
MEDIA_INFO_VIDEO_TRACK_LAGGING
static val MEDIA_INFO_VIDEO_TRACK_LAGGING: Int
The video is too complex for the decoder: it can't decode frames fast enough. Possibly only the audio plays fine at this stage.
Value: 700
MEDIA_MIMETYPE_TEXT_SUBRIP
static valMEDIA_MIMETYPE_TEXT_SUBRIP: String
Deprecated: use MediaFormat.MIMETYPE_TEXT_SUBRIP
MIME type for SubRip (SRT) container. Used in addTimedTextSource APIs.
Value: "application/x-subrip"
PREPARE_DRM_STATUS_PREPARATION_ERROR
static val PREPARE_DRM_STATUS_PREPARATION_ERROR: Int
The DRM preparation has failed .
Value: 3
PREPARE_DRM_STATUS_PROVISIONING_NETWORK_ERROR
static val PREPARE_DRM_STATUS_PROVISIONING_NETWORK_ERROR: Int
The device required DRM provisioning but couldn't reach the provisioning server.
Value: 1
PREPARE_DRM_STATUS_PROVISIONING_SERVER_ERROR
static val PREPARE_DRM_STATUS_PROVISIONING_SERVER_ERROR: Int
The device required DRM provisioning but the provisioning server denied the request.
Value: 2
PREPARE_DRM_STATUS_SUCCESS
static val PREPARE_DRM_STATUS_SUCCESS: Int
The status codes for OnDrmPreparedListener.onDrmPrepared
listener.
DRM preparation has succeeded.
Value: 0
SEEK_CLOSEST
static val SEEK_CLOSEST: Int
This mode is used with seekTo(long,int)
to move media position to a frame (not necessarily a key frame) associated with a data source that is located closest to or at the given time.
Value: 3
See Also
SEEK_CLOSEST_SYNC
static val SEEK_CLOSEST_SYNC: Int
This mode is used with seekTo(long,int)
to move media position to a sync (or key) frame associated with a data source that is located closest to (in time) or at the given time.
Value: 2
See Also
SEEK_NEXT_SYNC
static val SEEK_NEXT_SYNC: Int
This mode is used with seekTo(long,int)
to move media position to a sync (or key) frame associated with a data source that is located right after or at the given time.
Value: 1
See Also
SEEK_PREVIOUS_SYNC
static val SEEK_PREVIOUS_SYNC: Int
This mode is used with seekTo(long,int)
to move media position to a sync (or key) frame associated with a data source that is located right before or at the given time.
Value: 0
See Also
VIDEO_SCALING_MODE_SCALE_TO_FIT
static val VIDEO_SCALING_MODE_SCALE_TO_FIT: Int
Specifies a video scaling mode. The content is stretched to the surface rendering area. When the surface has the same aspect ratio as the content, the aspect ratio of the content is maintained; otherwise, the aspect ratio of the content is not maintained when video is being rendered. Unlike VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING
, there is no content cropping with this video scaling mode.
Value: 1
VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING
static val VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING: Int
Specifies a video scaling mode. The content is scaled, maintaining its aspect ratio. The whole surface area is always used. When the aspect ratio of the content is the same as the surface, no content is cropped; otherwise, content is cropped to fit the surface.
Value: 2
Public constructors
MediaPlayer
MediaPlayer()
Default constructor.
Consider using one of the create() methods for synchronously instantiating a MediaPlayer from a Uri or resource.
You must call release()
when you are finished using the instantiated instance. Doing so frees any resources you have previously acquired.
MediaPlayer
MediaPlayer(context: Context)
Default constructor with context.
Consider using one of the create() methods for synchronously instantiating a MediaPlayer from a Uri or resource.
Parameters | |
---|---|
context |
Context: non-null context. This context will be used to pull information, such as android.content.AttributionSource and device specific session ids, which will be associated with the MediaPlayer . However, the context itself will not be retained by the MediaPlayer. |
Public methods
addOnRoutingChangedListener
open fun addOnRoutingChangedListener(
listener: AudioRouting.OnRoutingChangedListener!,
handler: Handler!
): Unit
Adds an AudioRouting.OnRoutingChangedListener
to receive notifications of routing changes on this MediaPlayer.
Parameters | |
---|---|
listener |
AudioRouting.OnRoutingChangedListener!: The AudioRouting.OnRoutingChangedListener interface to receive notifications of rerouting events. |
handler |
Handler!: Specifies the Handler object for the thread on which to execute the callback. If null , the handler on the main looper will be used. |
addTimedTextSource
open fun addTimedTextSource(
context: Context!,
uri: Uri!,
mimeType: String!
): Unit
Adds an external timed text source file (Uri). Currently supported format is SubRip with the file extension .srt, case insensitive. Note that a single external timed text source may contain multiple tracks in it. One can find the total number of available tracks using getTrackInfo()
to see what additional tracks become available after this method call.
Parameters | |
---|---|
context |
Context!: the Context to use when resolving the Uri |
uri |
Uri!: the Content URI of the data you want to play |
mimeType |
String!: The mime type of the file. Must be one of the mime types listed above. |
Exceptions | |
---|---|
java.io.IOException |
if the file cannot be accessed or is corrupted. |
java.lang.IllegalArgumentException |
if the mimeType is not supported. |
java.lang.IllegalStateException |
if called in an invalid state. |
addTimedTextSource
open fun addTimedTextSource(
fd: FileDescriptor!,
mimeType: String!
): Unit
Adds an external timed text source file (FileDescriptor). It is the caller's responsibility to close the file descriptor. It is safe to do so as soon as this call returns. Currently supported format is SubRip. Note that a single external timed text source may contain multiple tracks in it. One can find the total number of available tracks using getTrackInfo()
to see what additional tracks become available after this method call.
Parameters | |
---|---|
fd |
FileDescriptor!: the FileDescriptor for the file you want to play |
mimeType |
String!: The mime type of the file. Must be one of the mime types listed above. |
Exceptions | |
---|---|
java.lang.IllegalArgumentException |
if the mimeType is not supported. |
java.lang.IllegalStateException |
if called in an invalid state. |
addTimedTextSource
open fun addTimedTextSource(
fd: FileDescriptor!,
offset: Long,
length: Long,
mime: String!
): Unit
Adds an external timed text file (FileDescriptor). It is the caller's responsibility to close the file descriptor. It is safe to do so as soon as this call returns. Currently supported format is SubRip. Note that a single external timed text source may contain multiple tracks in it. One can find the total number of available tracks using getTrackInfo()
to see what additional tracks become available after this method call.
Parameters | |
---|---|
fd |
FileDescriptor!: the FileDescriptor for the file you want to play |
offset |
Long: the offset into the file where the data to be played starts, in bytes |
length |
Long: the length in bytes of the data to be played |
mime |
String!: The mime type of the file. Must be one of the mime types listed above. |
Exceptions | |
---|---|
java.lang.IllegalArgumentException |
if the mimeType is not supported. |
java.lang.IllegalStateException |
if called in an invalid state. |
addTimedTextSource
open fun addTimedTextSource(
path: String!,
mimeType: String!
): Unit
Adds an external timed text source file. Currently supported format is SubRip with the file extension .srt, case insensitive. Note that a single external timed text source may contain multiple tracks in it. One can find the total number of available tracks using getTrackInfo()
to see what additional tracks become available after this method call.
Parameters | |
---|---|
path |
String!: The file path of external timed text source file. |
mimeType |
String!: The mime type of the file. Must be one of the mime types listed above. |
Exceptions | |
---|---|
java.io.IOException |
if the file cannot be accessed or is corrupted. |
java.lang.IllegalArgumentException |
if the mimeType is not supported. |
java.lang.IllegalStateException |
if called in an invalid state. |
attachAuxEffect
open fun attachAuxEffect(effectId: Int): Unit
Attaches an auxiliary effect to the player. A typical auxiliary effect is a reverberation effect which can be applied on any sound source that directs a certain amount of its energy to this effect. This amount is defined by setAuxEffectSendLevel(). See setAuxEffectSendLevel(float)
.
After creating an auxiliary effect (e.g. android.media.audiofx.EnvironmentalReverb
), retrieve its ID with android.media.audiofx.AudioEffect#getId()
and use it when calling this method to attach the player to the effect.
To detach the effect from the player, call this method with a null effect id.
This method must be called after one of the overloaded setDataSource
methods.
Parameters | |
---|---|
effectId |
Int: system wide unique id of the effect to attach |
clearOnMediaTimeDiscontinuityListener
open fun clearOnMediaTimeDiscontinuityListener(): Unit
Clears the listener previously set with setOnMediaTimeDiscontinuityListener(android.media.MediaPlayer.OnMediaTimeDiscontinuityListener)
or setOnMediaTimeDiscontinuityListener(android.media.MediaPlayer.OnMediaTimeDiscontinuityListener,android.os.Handler)
clearOnSubtitleDataListener
open fun clearOnSubtitleDataListener(): Unit
Clears the listener previously set with setOnSubtitleDataListener(android.media.MediaPlayer.OnSubtitleDataListener)
or setOnSubtitleDataListener(android.media.MediaPlayer.OnSubtitleDataListener,android.os.Handler)
.
create
open static fun create(
context: Context!,
uri: Uri!
): MediaPlayer!
Convenience method to create a MediaPlayer for a given Uri. On success, prepare()
will already have been called and must not be called again.
You must call release()
when you are finished using the created instance. Doing so frees any resources you have previously acquired.
Note that since prepare()
is called automatically in this method, you cannot change the audio session ID (see setAudioSessionId(int)
) or audio attributes (see setAudioAttributes(android.media.AudioAttributes)
of the new MediaPlayer.
Parameters | |
---|---|
context |
Context!: the Context to use |
uri |
Uri!: the Uri from which to get the datasource |
Return | |
---|---|
MediaPlayer! |
a MediaPlayer object, or null if creation failed |
create
open static fun create(
context: Context!,
uri: Uri!,
holder: SurfaceHolder!
): MediaPlayer!
Convenience method to create a MediaPlayer for a given Uri. On success, prepare()
will already have been called and must not be called again.
You must call release()
when you are finished using the created instance. Doing so frees any resources you have previously acquired.
Note that since prepare()
is called automatically in this method, you cannot change the audio session ID (see setAudioSessionId(int)
) or audio attributes (see setAudioAttributes(android.media.AudioAttributes)
of the new MediaPlayer.
Parameters | |
---|---|
context |
Context!: the Context to use |
uri |
Uri!: the Uri from which to get the datasource |
holder |
SurfaceHolder!: the SurfaceHolder to use for displaying the video |
Return | |
---|---|
MediaPlayer! |
a MediaPlayer object, or null if creation failed |
create
open static fun create(
context: Context!,
uri: Uri!,
holder: SurfaceHolder!,
audioAttributes: AudioAttributes!,
audioSessionId: Int
): MediaPlayer!
Same factory method as create(android.content.Context,android.net.Uri,android.view.SurfaceHolder)
but that lets you specify the audio attributes and session ID to be used by the new MediaPlayer instance.
Parameters | |
---|---|
context |
Context!: the Context to use |
uri |
Uri!: the Uri from which to get the datasource |
holder |
SurfaceHolder!: the SurfaceHolder to use for displaying the video, may be null. |
audioAttributes |
AudioAttributes!: the AudioAttributes to be used by the media player. |
audioSessionId |
Int: the audio session ID to be used by the media player, see AudioManager.generateAudioSessionId() to obtain a new session. |
Return | |
---|---|
MediaPlayer! |
a MediaPlayer object, or null if creation failed |
create
open static fun create(
context: Context!,
resid: Int
): MediaPlayer!
Convenience method to create a MediaPlayer for a given resource id. On success, prepare()
will already have been called and must not be called again.
You must call release()
when you are finished using the created instance. Doing so frees any resources you have previously acquired.
Note that since prepare()
is called automatically in this method, you cannot change the audio session ID (see setAudioSessionId(int)
) or audio attributes (see setAudioAttributes(android.media.AudioAttributes)
of the new MediaPlayer.
Parameters | |
---|---|
context |
Context!: the Context to use |
resid |
Int: the raw resource id (R.raw.<something>) for the resource to use as the datasource |
Return | |
---|---|
MediaPlayer! |
a MediaPlayer object, or null if creation failed |
create
open static fun create(
context: Context!,
resid: Int,
audioAttributes: AudioAttributes!,
audioSessionId: Int
): MediaPlayer!
Same factory method as create(android.content.Context,int)
but that lets you specify the audio attributes and session ID to be used by the new MediaPlayer instance.
Parameters | |
---|---|
context |
Context!: the Context to use |
resid |
Int: the raw resource id (R.raw.<something>) for the resource to use as the datasource |
audioAttributes |
AudioAttributes!: the AudioAttributes to be used by the media player. |
audioSessionId |
Int: the audio session ID to be used by the media player, see AudioManager.generateAudioSessionId() to obtain a new session. |
Return | |
---|---|
MediaPlayer! |
a MediaPlayer object, or null if creation failed |
createVolumeShaper
open fun createVolumeShaper(configuration: VolumeShaper.Configuration): VolumeShaper
Returns a VolumeShaper
object that can be used modify the volume envelope of the player or track.
Parameters | |
---|---|
configuration |
VolumeShaper.Configuration: This value cannot be null . |
Return | |
---|---|
VolumeShaper |
This value cannot be null . |
Exceptions | |
---|---|
java.lang.IllegalArgumentException |
if the configuration is not allowed by the player. |
java.lang.IllegalStateException |
if too many VolumeShaper s are requested or the state of the player does not permit its creation (e.g. player is released). |
deselectTrack
open fun deselectTrack(index: Int): Unit
Deselect a track.
Currently, the track must be a timed text track and no audio or video tracks can be deselected. If the timed text track identified by index has not been selected before, it throws an exception.
Parameters | |
---|---|
index |
Int: the index of the track to be deselected. The valid range of the index is 0..total number of tracks - 1. The total number of tracks as well as the type of each individual track can be found by calling getTrackInfo() method. |
Exceptions | |
---|---|
java.lang.IllegalStateException |
if called in an invalid state. |
getAudioSessionId
open fun getAudioSessionId(): Int
Returns the audio session ID.
Return | |
---|---|
Int |
the audio session ID. {@see #setAudioSessionId(int)} Note that the audio session ID is 0 only if a problem occured when the MediaPlayer was contructed. |
getCurrentPosition
open fun getCurrentPosition(): Int
Gets the current playback position.
Return | |
---|---|
Int |
the current position in milliseconds |
getDrmInfo
open fun getDrmInfo(): MediaPlayer.DrmInfo!
Retrieves the DRM Info associated with the current source
Exceptions | |
---|---|
java.lang.IllegalStateException |
if called before prepare() |
getDrmPropertyString
open fun getDrmPropertyString(propertyName: String): String
Read a DRM engine plugin String property value, given the property name string.
Parameters | |
---|---|
propertyName |
String: the property name Standard fields names are: MediaDrm.PROPERTY_VENDOR , MediaDrm.PROPERTY_VERSION , MediaDrm.PROPERTY_DESCRIPTION , MediaDrm.PROPERTY_ALGORITHMS This value cannot be null . Value is android.media.MediaDrm#PROPERTY_VENDOR , android.media.MediaDrm#PROPERTY_VERSION , android.media.MediaDrm#PROPERTY_DESCRIPTION , or android.media.MediaDrm#PROPERTY_ALGORITHMS |
Return | |
---|---|
String |
This value cannot be null . |
getDuration
open fun getDuration(): Int
Gets the duration of the file.
Return | |
---|---|
Int |
the duration in milliseconds, if no duration is available (for example, if streaming live content), -1 is returned. |
getKeyRequest
open fun getKeyRequest(
keySetId: ByteArray?,
initData: ByteArray?,
mimeType: String?,
keyType: Int,
optionalParameters: MutableMap<String!, String!>?
): MediaDrm.KeyRequest
A key request/response exchange occurs between the app and a license server to obtain or release keys used to decrypt encrypted content.
getKeyRequest() is used to obtain an opaque key request byte array that is delivered to the license server. The opaque key request byte array is returned in KeyRequest.data. The recommended URL to deliver the key request to is returned in KeyRequest.defaultUrl.
After the app has received the key request response from the server, it should deliver to the response to the DRM engine plugin using the method provideKeyResponse
.
Parameters | |
---|---|
keySetId |
ByteArray?: is the key-set identifier of the offline keys being released when keyType is MediaDrm.KEY_TYPE_RELEASE . It should be set to null for other key requests, when keyType is MediaDrm.KEY_TYPE_STREAMING or MediaDrm.KEY_TYPE_OFFLINE . |
initData |
ByteArray?: is the container-specific initialization data when the keyType is MediaDrm.KEY_TYPE_STREAMING or MediaDrm.KEY_TYPE_OFFLINE . Its meaning is interpreted based on the mime type provided in the mimeType parameter. It could contain, for example, the content ID, key ID or other data obtained from the content metadata that is required in generating the key request. When the keyType is MediaDrm.KEY_TYPE_RELEASE , it should be set to null. |
mimeType |
String?: identifies the mime type of the content This value may be null . |
keyType |
Int: specifies the type of the request. The request may be to acquire keys for streaming, MediaDrm.KEY_TYPE_STREAMING , or for offline content MediaDrm.KEY_TYPE_OFFLINE , or to release previously acquired keys (MediaDrm.KEY_TYPE_RELEASE ), which are identified by a keySetId. Value is android.media.MediaDrm#KEY_TYPE_STREAMING , android.media.MediaDrm#KEY_TYPE_OFFLINE , or android.media.MediaDrm#KEY_TYPE_RELEASE |
optionalParameters |
MutableMap<String!, String!>?: are included in the key request message to allow a client application to provide additional message parameters to the server. This may be null if no additional parameters are to be sent. |
Return | |
---|---|
MediaDrm.KeyRequest |
This value cannot be null . |
Exceptions | |
---|---|
android.media.MediaPlayer.NoDrmSchemeException |
if there is no active DRM session |
getMetrics
open fun getMetrics(): PersistableBundle!
Return Metrics data about the current player.
Return | |
---|---|
PersistableBundle! |
a PersistableBundle containing the set of attributes and values available for the media being handled by this instance of MediaPlayer The attributes are descibed in MetricsConstants . Additional vendor-specific fields may also be present in the return value. |
getPlaybackParams
open fun getPlaybackParams(): PlaybackParams
Gets the playback params, containing the current playback rate.
Return | |
---|---|
PlaybackParams |
the playback params. This value cannot be null . |
Exceptions | |
---|---|
java.lang.IllegalStateException |
if the internal player engine has not been initialized. |
getPreferredDevice
open fun getPreferredDevice(): AudioDeviceInfo!
Returns the selected output specified by setPreferredDevice
. Note that this is not guaranteed to correspond to the actual device being used for playback.
getRoutedDevice
open fun getRoutedDevice(): AudioDeviceInfo!
Returns an AudioDeviceInfo
identifying the current routing of this MediaPlayer Note: The query is only valid if the MediaPlayer is currently playing. If the player is not playing, the returned device can be null or correspond to previously selected device when the player was last active. Audio may play on multiple devices simultaneously (e.g. an alarm playing on headphones and speaker on a phone), so prefer using #getRoutedDevices.
getSelectedTrack
open fun getSelectedTrack(trackType: Int): Int
Returns the index of the audio, video, or subtitle track currently selected for playback, The return value is an index into the array returned by getTrackInfo()
, and can be used in calls to selectTrack(int)
or deselectTrack(int)
.
Parameters | |
---|---|
trackType |
Int: should be one of TrackInfo.MEDIA_TRACK_TYPE_VIDEO , TrackInfo.MEDIA_TRACK_TYPE_AUDIO , or TrackInfo.MEDIA_TRACK_TYPE_SUBTITLE |
Return | |
---|---|
Int |
index of the audio, video, or subtitle track currently selected for playback; a negative integer is returned when there is no selected track for trackType or when trackType is not one of audio, video, or subtitle. |
Exceptions | |
---|---|
java.lang.IllegalStateException |
if called after release() |
getSyncParams
open fun getSyncParams(): SyncParams
Gets the A/V sync mode.
Return | |
---|---|
SyncParams |
the A/V sync params This value cannot be null . |
Exceptions | |
---|---|
java.lang.IllegalStateException |
if the internal player engine has not been initialized. |
getTimestamp
open fun getTimestamp(): MediaTimestamp?
Get current playback position as a MediaTimestamp
.
The MediaTimestamp represents how the media time correlates to the system time in a linear fashion using an anchor and a clock rate. During regular playback, the media time moves fairly constantly (though the anchor frame may be rebased to a current system time, the linear correlation stays steady). Therefore, this method does not need to be called often.
To help users get current playback position, this method always anchors the timestamp to the current system time
, so MediaTimestamp.getAnchorMediaTimeUs
can be used as current playback position.
Return | |
---|---|
MediaTimestamp? |
a MediaTimestamp object if a timestamp is available, or null if no timestamp is available, e.g. because the media player has not been initialized. |
See Also
getTrackInfo
open fun getTrackInfo(): Array<MediaPlayer.TrackInfo!>!
Returns an array of track information.
Return | |
---|---|
Array<MediaPlayer.TrackInfo!>! |
Array of track info. The total number of tracks is the array length. Must be called again if an external timed text source has been added after any of the addTimedTextSource methods are called. |
Exceptions | |
---|---|
java.lang.IllegalStateException |
if it is called in an invalid state. |
getVideoHeight
open fun getVideoHeight(): Int
Returns the height of the video.
Return | |
---|---|
Int |
the height of the video, or 0 if there is no video, no display surface was set, or the height has not been determined yet. The OnVideoSizeChangedListener can be registered via setOnVideoSizeChangedListener(android.media.MediaPlayer.OnVideoSizeChangedListener) to provide a notification when the height is available. |
getVideoWidth
open fun getVideoWidth(): Int
Returns the width of the video.
Return | |
---|---|
Int |
the width of the video, or 0 if there is no video, no display surface was set, or the width has not been determined yet. The OnVideoSizeChangedListener can be registered via setOnVideoSizeChangedListener(android.media.MediaPlayer.OnVideoSizeChangedListener) to provide a notification when the width is available. |
isLooping
open fun isLooping(): Boolean
Checks whether the MediaPlayer is looping or non-looping.
Return | |
---|---|
Boolean |
true if the MediaPlayer is currently looping, false otherwise |
isPlaying
open fun isPlaying(): Boolean
Checks whether the MediaPlayer is playing.
Return | |
---|---|
Boolean |
true if currently playing, false otherwise |
Exceptions | |
---|---|
java.lang.IllegalStateException |
if the internal player engine has not been initialized or has been released. |
pause
open fun pause(): Unit
Pauses playback. Call start() to resume.
Exceptions | |
---|---|
java.lang.IllegalStateException |
if the internal player engine has not been initialized. |
prepare
open fun prepare(): Unit
Prepares the player for playback, synchronously. After setting the datasource and the display surface, you need to either call prepare() or prepareAsync(). For files, it is OK to call prepare(), which blocks until MediaPlayer is ready for playback.
Exceptions | |
---|---|
java.lang.IllegalStateException |
if it is called in an invalid state |
prepareAsync
open fun prepareAsync(): Unit
Prepares the player for playback, asynchronously. After setting the datasource and the display surface, you need to either call prepare() or prepareAsync(). For streams, you should call prepareAsync(), which returns immediately, rather than blocking until enough data has been buffered.
Exceptions | |
---|---|
java.lang.IllegalStateException |
if it is called in an invalid state |
prepareDrm
open fun prepareDrm(uuid: UUID): Unit
Prepares the DRM for the current source
If OnDrmConfigHelper
is registered, it will be called during preparation to allow configuration of the DRM properties before opening the DRM session. Note that the callback is called synchronously in the thread that called prepareDrm
. It should be used only for a series of getDrmPropertyString
and setDrmPropertyString
calls and refrain from any lengthy operation.
If the device has not been provisioned before, this call also provisions the device which involves accessing the provisioning server and can take a variable time to complete depending on the network connectivity. If OnDrmPreparedListener
is registered, prepareDrm() runs in non-blocking mode by launching the provisioning in the background and returning. The listener will be called when provisioning and preparation has finished. If a OnDrmPreparedListener
is not registered, prepareDrm() waits till provisioning and preparation has finished, i.e., runs in blocking mode.
If OnDrmPreparedListener
is registered, it is called to indicate the DRM session being ready. The application should not make any assumption about its call sequence (e.g., before or after prepareDrm returns), or the thread context that will execute the listener (unless the listener is registered with a handler thread).
Parameters | |
---|---|
uuid |
UUID: The UUID of the crypto scheme. If not known beforehand, it can be retrieved from the source through getDrmInfo or registering a onDrmInfoListener . This value cannot be null . |
Exceptions | |
---|---|
java.lang.IllegalStateException |
if called before prepare(), or the DRM was prepared already |
android.media.UnsupportedSchemeException |
if the crypto scheme is not supported |
android.media.ResourceBusyException |
if required DRM resources are in use |
android.media.MediaPlayer.ProvisioningNetworkErrorException |
if provisioning is required but failed due to a network error |
android.media.MediaPlayer.ProvisioningServerErrorException |
if provisioning is required but failed due to the request denied by the provisioning server |
provideKeyResponse
open fun provideKeyResponse(
keySetId: ByteArray?,
response: ByteArray
): ByteArray!
A key response is received from the license server by the app, then it is provided to the DRM engine plugin using provideKeyResponse. When the response is for an offline key request, a key-set identifier is returned that can be used to later restore the keys to a new session with the method {@ link # restoreKeys}. * When the response is for a streaming or release request, null is returned. * * @param keySetId When the response is for a release request, keySetId identifies * the saved key associated with the release request (i.e., the same keySetId * passed to the earlier {@ link # getKeyRequest} call. It MUST be null when the * response is for either streaming or offline key requests. * * @param response the byte array response from the server * * @throws NoDrmSchemeException if there is no active DRM session * @throws DeniedByServerException if the response indicates that the * server rejected the request * @param keySetId This value may be {@code null}. * @param response This value cannot be {@code null}. * @apiSince 26
release
open fun release(): Unit
Releases resources associated with this MediaPlayer object.
You must call this method once the instance is no longer required.
releaseDrm
open fun releaseDrm(): Unit
Releases the DRM session
The player has to have an active DRM session and be in stopped, or prepared state before this call is made. A reset()
call will release the DRM session implicitly.
Exceptions | |
---|---|
android.media.MediaPlayer.NoDrmSchemeException |
if there is no active DRM session to release |
removeOnRoutingChangedListener
open fun removeOnRoutingChangedListener(listener: AudioRouting.OnRoutingChangedListener!): Unit
Removes an AudioRouting.OnRoutingChangedListener
which has been previously added to receive rerouting notifications.
Parameters | |
---|---|
listener |
AudioRouting.OnRoutingChangedListener!: The previously added AudioRouting.OnRoutingChangedListener interface to remove. |
reset
open fun reset(): Unit
Resets the MediaPlayer to its uninitialized state. After calling this method, you will have to initialize it again by setting the data source and calling prepare().
restoreKeys
open fun restoreKeys(keySetId: ByteArray): Unit
Restore persisted offline keys into a new session. keySetId identifies the keys to load, obtained from a prior call to provideKeyResponse
.
Parameters | |
---|---|
keySetId |
ByteArray: identifies the saved key set to restore This value cannot be null . |
seekTo
open fun seekTo(msec: Int): Unit
Seeks to specified time position. Same as seekTo(long,int)
with mode = SEEK_PREVIOUS_SYNC
.
Parameters | |
---|---|
msec |
Int: the offset in milliseconds from the start to seek to |
Exceptions | |
---|---|
java.lang.IllegalStateException |
if the internal player engine has not been initialized |
seekTo
open fun seekTo(
msec: Long,
mode: Int
): Unit
Moves the media to specified time position by considering the given mode.
When seekTo is finished, the user will be notified via OnSeekComplete supplied by the user. There is at most one active seekTo processed at any time. If there is a to-be-completed seekTo, new seekTo requests will be queued in such a way that only the last request is kept. When current seekTo is completed, the queued request will be processed if that request is different from just-finished seekTo operation, i.e., the requested position or mode is different.
Parameters | |
---|---|
msec |
Long: the offset in milliseconds from the start to seek to. When seeking to the given time position, there is no guarantee that the data source has a frame located at the position. When this happens, a frame nearby will be rendered. If msec is negative, time position zero will be used. If msec is larger than duration, duration will be used. |
mode |
Int: the mode indicating where exactly to seek to. Use SEEK_PREVIOUS_SYNC if one wants to seek to a sync frame that has a timestamp earlier than or the same as msec. Use SEEK_NEXT_SYNC if one wants to seek to a sync frame that has a timestamp later than or the same as msec. Use SEEK_CLOSEST_SYNC if one wants to seek to a sync frame that has a timestamp closest to or the same as msec. Use SEEK_CLOSEST if one wants to seek to a frame that may or may not be a sync frame but is closest to or the same as msec. SEEK_CLOSEST often has larger performance overhead compared to the other options if there is no sync frame located at msec. Value is android.media.MediaPlayer#SEEK_PREVIOUS_SYNC , android.media.MediaPlayer#SEEK_NEXT_SYNC , android.media.MediaPlayer#SEEK_CLOSEST_SYNC , or android.media.MediaPlayer#SEEK_CLOSEST |
Exceptions | |
---|---|
java.lang.IllegalStateException |
if the internal player engine has not been initialized |
java.lang.IllegalArgumentException |
if the mode is invalid. |
selectTrack
open fun selectTrack(index: Int): Unit
Selects a track.
If a MediaPlayer is in invalid state, it throws an IllegalStateException exception. If a MediaPlayer is in Started state, the selected track is presented immediately. If a MediaPlayer is not in Started state, it just marks the track to be played.
In any valid state, if it is called multiple times on the same type of track (ie. Video, Audio, Timed Text), the most recent one will be chosen.
The first audio and video tracks are selected by default if available, even though this method is not called. However, no timed text track will be selected until this function is called.
Currently, only timed text, subtitle or audio tracks can be selected via this method. In addition, the support for selecting an audio track at runtime is pretty limited in that an audio track can only be selected in the Prepared state.
Parameters | |
---|---|
index |
Int: the index of the track to be selected. The valid range of the index is 0..total number of track - 1. The total number of tracks as well as the type of each individual track can be found by calling getTrackInfo() method. |
Exceptions | |
---|---|
java.lang.IllegalStateException |
if called in an invalid state. |
setAudioAttributes
open fun setAudioAttributes(attributes: AudioAttributes!): Unit
Sets the audio attributes for this MediaPlayer. See AudioAttributes
for how to build and configure an instance of this class. You must call this method before prepare()
or prepareAsync()
in order for the audio attributes to become effective thereafter.
Parameters | |
---|---|
attributes |
AudioAttributes!: a non-null set of audio attributes |
setAudioSessionId
open fun setAudioSessionId(sessionId: Int): Unit
Sets the audio session ID.
Parameters | |
---|---|
sessionId |
Int: the audio session ID. The audio session ID is a system wide unique identifier for the audio stream played by this MediaPlayer instance. The primary use of the audio session ID is to associate audio effects to a particular instance of MediaPlayer: if an audio session ID is provided when creating an audio effect, this effect will be applied only to the audio content of media players within the same audio session and not to the output mix. When created, a MediaPlayer instance automatically generates its own audio session ID. However, it is possible to force this player to be part of an already existing audio session by calling this method. This method must be called before one of the overloaded setDataSource methods. Note that session id set using this method will override device-specific audio session id, if the MediaPlayer was instantiated using device-specific Context - see MediaPlayer.MediaPlayer(Context) . |
Exceptions | |
---|---|
java.lang.IllegalStateException |
if it is called in an invalid state |
setAudioStreamType
open funsetAudioStreamType(streamtype: Int): Unit
Deprecated: use setAudioAttributes(android.media.AudioAttributes)
Sets the audio stream type for this MediaPlayer. See AudioManager
for a list of stream types. Must call this method before prepare() or prepareAsync() in order for the target stream type to become effective thereafter.
Parameters | |
---|---|
streamtype |
Int: the audio stream type |
See Also
setAuxEffectSendLevel
open fun setAuxEffectSendLevel(level: Float): Unit
Sets the send level of the player to the attached auxiliary effect. See attachAuxEffect(int)
. The level value range is 0 to 1.0.
By default the send level is 0, so even if an effect is attached to the player this method must be called for the effect to be applied.
Note that the passed level value is a raw scalar. UI controls should be scaled logarithmically: the gain applied by audio framework ranges from -72dB to 0dB, so an appropriate conversion from linear UI input x to level is: x == 0 -> level = 0 0 < x <= R -> level = 10^(72*(x-R)/20/R)
Parameters | |
---|---|
level |
Float: send level scalar |
setDataSource
open fun setDataSource(
context: Context,
uri: Uri
): Unit
Sets the data source as a content Uri.
Parameters | |
---|---|
context |
Context: the Context to use when resolving the Uri This value cannot be null . |
uri |
Uri: the Content URI of the data you want to play This value cannot be null . |
Exceptions | |
---|---|
java.lang.IllegalStateException |
if it is called in an invalid state |
setDataSource
open fun setDataSource(
context: Context,
uri: Uri,
headers: MutableMap<String!, String!>?
): Unit
Sets the data source as a content Uri.
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.
Parameters | |
---|---|
context |
Context: the Context to use when resolving the Uri This value cannot be null . |
uri |
Uri: the Content URI of the data you want to play This value cannot be null . |
headers |
MutableMap<String!, String!>?: the headers to be sent together with the request for the data This value may be null . |
Exceptions | |
---|---|
java.lang.IllegalStateException |
if it is called in an invalid state |
setDataSource
open fun setDataSource(
context: Context,
uri: Uri,
headers: MutableMap<String!, String!>?,
cookies: MutableList<HttpCookie!>?
): Unit
Sets the data source as a content Uri. To provide cookies for the subsequent HTTP requests, you can install your own default cookie handler and use other variants of setDataSource APIs instead. Alternatively, you can use this API to pass the cookies as a list of HttpCookie. If the app has not installed a CookieHandler already, this API creates a CookieManager and populates its CookieStore with the provided cookies. If the app has installed its own handler already, this API requires the handler to be of CookieManager type such that the API can update the manager's CookieStore.
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.
Parameters | |
---|---|
context |
Context: the Context to use when resolving the Uri This value cannot be null . |
uri |
Uri: the Content URI of the data you want to play This value cannot be null . |
headers |
MutableMap<String!, String!>?: the headers to be sent together with the request for the data The headers must not include cookies. Instead, use the cookies param. This value may be null . |
cookies |
MutableList<HttpCookie!>?: the cookies to be sent together with the request This value may be null . |
Exceptions | |
---|---|
java.lang.IllegalArgumentException |
if cookies are provided and the installed handler is not a CookieManager |
java.lang.IllegalStateException |
if it is called in an invalid state |
java.lang.NullPointerException |
if context or uri is null |
java.io.IOException |
if uri has a file scheme and an I/O error occurs |
setDataSource
open fun setDataSource(afd: AssetFileDescriptor): Unit
Sets the data source (AssetFileDescriptor) to use. It is the caller's responsibility to close the file descriptor. It is safe to do so as soon as this call returns.
Parameters | |
---|---|
afd |
AssetFileDescriptor: the AssetFileDescriptor for the file you want to play This value cannot be null . |
Exceptions | |
---|---|
java.lang.IllegalStateException |
if it is called in an invalid state |
java.lang.IllegalArgumentException |
if afd is not a valid AssetFileDescriptor |
java.io.IOException |
if afd can not be read |
setDataSource
open fun setDataSource(dataSource: MediaDataSource!): Unit
Sets the data source (MediaDataSource) to use.
Parameters | |
---|---|
dataSource |
MediaDataSource!: the MediaDataSource for the media you want to play |
Exceptions | |
---|---|
java.lang.IllegalStateException |
if it is called in an invalid state |
java.lang.IllegalArgumentException |
if dataSource is not a valid MediaDataSource |
setDataSource
open fun setDataSource(fd: FileDescriptor!): Unit
Sets the data source (FileDescriptor) to use. It is the caller's responsibility to close the file descriptor. It is safe to do so as soon as this call returns.
Parameters | |
---|---|
fd |
FileDescriptor!: the FileDescriptor for the file you want to play |
Exceptions | |
---|---|
java.lang.IllegalStateException |
if it is called in an invalid state |
java.lang.IllegalArgumentException |
if fd is not a valid FileDescriptor |
java.io.IOException |
if fd can not be read |
setDataSource
open fun setDataSource(
fd: FileDescriptor!,
offset: Long,
length: Long
): Unit
Sets the data source (FileDescriptor) to use. The FileDescriptor must be seekable (N.B. a LocalSocket is not seekable). It is the caller's responsibility to close the file descriptor. It is safe to do so as soon as this call returns.
Parameters | |
---|---|
fd |
FileDescriptor!: the FileDescriptor for the file you want to play |
offset |
Long: the offset into the file where the data to be played starts, in bytes |
length |
Long: the length in bytes of the data to be played |
Exceptions | |
---|---|
java.lang.IllegalStateException |
if it is called in an invalid state |
java.lang.IllegalArgumentException |
if fd is not a valid FileDescriptor |
java.io.IOException |
if fd can not be read |
setDataSource
open fun setDataSource(path: String!): Unit
Sets the data source (file-path or http/rtsp URL) to use.
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(java.io.FileDescriptor)
.
Parameters | |
---|---|
path |
String!: the path of the file, or the http/rtsp URL of the stream you want to play |
Exceptions | |
---|---|
java.lang.IllegalStateException |
if it is called in an invalid state |
setDisplay
open fun setDisplay(sh: SurfaceHolder!): Unit
Sets the 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(android.view.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.
Parameters | |
---|---|
sh |
SurfaceHolder!: the SurfaceHolder to use for video display |
Exceptions | |
---|---|
java.lang.IllegalStateException |
if the internal player engine has not been initialized or has been released. |
setDrmPropertyString
open fun setDrmPropertyString(
propertyName: String,
value: String
): Unit
Set a DRM engine plugin String property value.
Parameters | |
---|---|
propertyName |
String: the property name This value cannot be null . Value is android.media.MediaDrm#PROPERTY_VENDOR , android.media.MediaDrm#PROPERTY_VERSION , android.media.MediaDrm#PROPERTY_DESCRIPTION , or android.media.MediaDrm#PROPERTY_ALGORITHMS |
value |
String: the property value Standard fields names are: MediaDrm.PROPERTY_VENDOR , MediaDrm.PROPERTY_VERSION , MediaDrm.PROPERTY_DESCRIPTION , MediaDrm.PROPERTY_ALGORITHMS This value cannot be null . |
setLooping
open fun setLooping(looping: Boolean): Unit
Sets the player to be looping or non-looping.
Parameters | |
---|---|
looping |
Boolean: whether to loop or not |
setNextMediaPlayer
open fun setNextMediaPlayer(next: MediaPlayer!): Unit
Set the MediaPlayer to start when this MediaPlayer finishes playback (i.e. reaches the end of the stream). The media framework will attempt to transition from this player to the next as seamlessly as possible. The next player can be set at any time before completion, but shall be after setDataSource has been called successfully. The next player must be prepared by the app, and the application should not call start() on it. The next MediaPlayer must be different from 'this'. An exception will be thrown if next == this. The application may call setNextMediaPlayer(null) to indicate no next player should be started at the end of playback. If the current player is looping, it will keep looping and the next player will not be started.
Parameters | |
---|---|
next |
MediaPlayer!: the player to start after this one completes playback. |
setOnBufferingUpdateListener
open fun setOnBufferingUpdateListener(listener: MediaPlayer.OnBufferingUpdateListener!): Unit
Register a callback to be invoked when the status of a network stream's buffer has changed.
Parameters | |
---|---|
listener |
MediaPlayer.OnBufferingUpdateListener!: the callback that will be run. |
setOnCompletionListener
open fun setOnCompletionListener(listener: MediaPlayer.OnCompletionListener!): Unit
Register a callback to be invoked when the end of a media source has been reached during playback.
Parameters | |
---|---|
listener |
MediaPlayer.OnCompletionListener!: the callback that will be run |
setOnDrmConfigHelper
open fun setOnDrmConfigHelper(listener: MediaPlayer.OnDrmConfigHelper!): Unit
Register a callback to be invoked for configuration of the DRM object before the session is created. The callback will be invoked synchronously during the execution of prepareDrm(java.util.UUID)
.
Parameters | |
---|---|
listener |
MediaPlayer.OnDrmConfigHelper!: the callback that will be run |
setOnDrmInfoListener
open fun setOnDrmInfoListener(listener: MediaPlayer.OnDrmInfoListener!): Unit
Register a callback to be invoked when the DRM info is known.
Parameters | |
---|---|
listener |
MediaPlayer.OnDrmInfoListener!: the callback that will be run |
setOnDrmInfoListener
open fun setOnDrmInfoListener(
listener: MediaPlayer.OnDrmInfoListener!,
handler: Handler!
): Unit
Register a callback to be invoked when the DRM info is known.
Parameters | |
---|---|
listener |
MediaPlayer.OnDrmInfoListener!: the callback that will be run |
setOnDrmPreparedListener
open fun setOnDrmPreparedListener(listener: MediaPlayer.OnDrmPreparedListener!): Unit
Register a callback to be invoked when the DRM object is prepared.
Parameters | |
---|---|
listener |
MediaPlayer.OnDrmPreparedListener!: the callback that will be run |
setOnDrmPreparedListener
open fun setOnDrmPreparedListener(
listener: MediaPlayer.OnDrmPreparedListener!,
handler: Handler!
): Unit
Register a callback to be invoked when the DRM object is prepared.
Parameters | |
---|---|
listener |
MediaPlayer.OnDrmPreparedListener!: the callback that will be run |
handler |
Handler!: the Handler that will receive the callback |
setOnErrorListener
open fun setOnErrorListener(listener: MediaPlayer.OnErrorListener!): Unit
Register a callback to be invoked when an error has happened during an asynchronous operation.
Parameters | |
---|---|
listener |
MediaPlayer.OnErrorListener!: the callback that will be run |
setOnInfoListener
open fun setOnInfoListener(listener: MediaPlayer.OnInfoListener!): Unit
Register a callback to be invoked when an info/warning is available.
Parameters | |
---|---|
listener |
MediaPlayer.OnInfoListener!: the callback that will be run |
setOnMediaTimeDiscontinuityListener
open fun setOnMediaTimeDiscontinuityListener(listener: MediaPlayer.OnMediaTimeDiscontinuityListener): Unit
Sets the listener to be invoked when a media time discontinuity is encountered. The listener will be called on the same thread as the one in which the MediaPlayer was created.
Parameters | |
---|---|
listener |
MediaPlayer.OnMediaTimeDiscontinuityListener: the listener called after a discontinuity This value cannot be null . |
setOnMediaTimeDiscontinuityListener
open fun setOnMediaTimeDiscontinuityListener(
listener: MediaPlayer.OnMediaTimeDiscontinuityListener,
handler: Handler
): Unit
Sets the listener to be invoked when a media time discontinuity is encountered.
Parameters | |
---|---|
listener |
MediaPlayer.OnMediaTimeDiscontinuityListener: the listener called after a discontinuity This value cannot be null . |
handler |
Handler: the Handler that receives the listener events This value cannot be null . |
setOnPreparedListener
open fun setOnPreparedListener(listener: MediaPlayer.OnPreparedListener!): Unit
Register a callback to be invoked when the media source is ready for playback.
Parameters | |
---|---|
listener |
MediaPlayer.OnPreparedListener!: the callback that will be run |
setOnSeekCompleteListener
open fun setOnSeekCompleteListener(listener: MediaPlayer.OnSeekCompleteListener!): Unit
Register a callback to be invoked when a seek operation has been completed.
Parameters | |
---|---|
listener |
MediaPlayer.OnSeekCompleteListener!: the callback that will be run |
setOnSubtitleDataListener
open fun setOnSubtitleDataListener(listener: MediaPlayer.OnSubtitleDataListener): Unit
Sets the listener to be invoked when a subtitle track has new data available. The subtitle data comes from a subtitle track previously selected with selectTrack(int)
. Use getTrackInfo()
to determine which tracks are subtitles (of type TrackInfo.MEDIA_TRACK_TYPE_SUBTITLE
), Subtitle track encodings can be determined by TrackInfo.getFormat()
).
See SubtitleData
for an example of querying subtitle encoding.
The listener will be called on the same thread as the one in which the MediaPlayer was created.
Parameters | |
---|---|
listener |
MediaPlayer.OnSubtitleDataListener: the listener called when new data is available This value cannot be null . |
setOnSubtitleDataListener
open fun setOnSubtitleDataListener(
listener: MediaPlayer.OnSubtitleDataListener,
handler: Handler
): Unit
Sets the listener to be invoked when a subtitle track has new data available. The subtitle data comes from a subtitle track previously selected with selectTrack(int)
. Use getTrackInfo()
to determine which tracks are subtitles (of type TrackInfo.MEDIA_TRACK_TYPE_SUBTITLE
), Subtitle track encodings can be determined by TrackInfo.getFormat()
).
See SubtitleData
for an example of querying subtitle encoding.
Parameters | |
---|---|
listener |
MediaPlayer.OnSubtitleDataListener: the listener called when new data is available This value cannot be null . |
handler |
Handler: the Handler that receives the listener events This value cannot be null . |
setOnTimedMetaDataAvailableListener
open fun setOnTimedMetaDataAvailableListener(listener: MediaPlayer.OnTimedMetaDataAvailableListener!): Unit
Register a callback to be invoked when a selected track has timed metadata available.
Currently only HTTP live streaming data URI's embedded with timed ID3 tags generates TimedMetaData
.
Parameters | |
---|---|
listener |
MediaPlayer.OnTimedMetaDataAvailableListener!: the callback that will be run |
setOnTimedTextListener
open fun setOnTimedTextListener(listener: MediaPlayer.OnTimedTextListener!): Unit
Register a callback to be invoked when a timed text is available for display.
Parameters | |
---|---|
listener |
MediaPlayer.OnTimedTextListener!: the callback that will be run |
setOnVideoSizeChangedListener
open fun setOnVideoSizeChangedListener(listener: MediaPlayer.OnVideoSizeChangedListener!): Unit
Register a callback to be invoked when the video size is known or updated.
Parameters | |
---|---|
listener |
MediaPlayer.OnVideoSizeChangedListener!: the callback that will be run |
setPlaybackParams
open fun setPlaybackParams(params: PlaybackParams): Unit
Sets playback rate using PlaybackParams
. The object sets its internal PlaybackParams to the input, except that the object remembers previous speed when input speed is zero. This allows the object to resume at previous speed when start() is called. Calling it before the object is prepared does not change the object state. After the object is prepared, calling it with zero speed is equivalent to calling pause(). After the object is prepared, calling it with non-zero speed is equivalent to calling start().
Parameters | |
---|---|
params |
PlaybackParams: the playback params. This value cannot be null . |
Exceptions | |
---|---|
java.lang.IllegalStateException |
if the internal player engine has not been initialized or has been released. |
java.lang.IllegalArgumentException |
if params is not supported. |
setPreferredDevice
open fun setPreferredDevice(deviceInfo: AudioDeviceInfo!): Boolean
Specifies an audio device (via an AudioDeviceInfo
object) to route the output from this MediaPlayer.
Parameters | |
---|---|
deviceInfo |
AudioDeviceInfo!: The AudioDeviceInfo specifying the audio sink or source. If deviceInfo is null, default routing is restored. |
Return | |
---|---|
Boolean |
true if succesful, false if the specified AudioDeviceInfo is non-null and does not correspond to a valid audio device. |
setScreenOnWhilePlaying
open fun setScreenOnWhilePlaying(screenOn: Boolean): Unit
Control whether we should use the attached SurfaceHolder to keep the screen on while video playback is occurring. This is the preferred method over setWakeMode
where possible, since it doesn't require that the application have permission for low-level wake lock access.
Parameters | |
---|---|
screenOn |
Boolean: Supply true to keep the screen on, false to allow it to turn off. |
setSurface
open fun setSurface(surface: Surface!): Unit
Sets the Surface
to be used as the sink for the video portion of the media. This is similar to setDisplay(android.view.SurfaceHolder)
, but does not support setScreenOnWhilePlaying(boolean)
. 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.
Parameters | |
---|---|
surface |
Surface!: The Surface to be used for the video portion of the media. |
Exceptions | |
---|---|
java.lang.IllegalStateException |
if the internal player engine has not been initialized or has been released. |
setSyncParams
open fun setSyncParams(params: SyncParams): Unit
Sets A/V sync mode.
Parameters | |
---|---|
params |
SyncParams: the A/V sync params to apply This value cannot be null . |
Exceptions | |
---|---|
java.lang.IllegalStateException |
if the internal player engine has not been initialized. |
java.lang.IllegalArgumentException |
if params are not supported. |
setVideoScalingMode
open fun setVideoScalingMode(mode: Int): Unit
Sets video scaling mode. To make the target video scaling mode effective during playback, this method must be called after data source is set. If not called, the default video scaling mode is VIDEO_SCALING_MODE_SCALE_TO_FIT
.
The supported video scaling modes are:
Parameters | |
---|---|
mode |
Int: target video scaling mode. Must be one of the supported video scaling modes; otherwise, IllegalArgumentException will be thrown. |
setVolume
open fun setVolume(
leftVolume: Float,
rightVolume: Float
): Unit
Sets the volume on this player. This API is recommended for balancing the output of audio streams within an application. Unless you are writing an application to control user settings, this API should be used in preference to 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.
Parameters | |
---|---|
leftVolume |
Float: left volume scalar |
rightVolume |
Float: right volume scalar |
setWakeMode
open fun setWakeMode(
context: Context!,
mode: Int
): Unit
Set the low-level power management behavior for this MediaPlayer. This can be used when the MediaPlayer is not playing through a SurfaceHolder set with setDisplay(android.view.SurfaceHolder)
and thus can use the high-level setScreenOnWhilePlaying(boolean)
feature.
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 android.os.PowerManager
wake flags. Use of this method requires android.Manifest.permission#WAKE_LOCK
permission. By default, no attempt is made to keep the device awake during playback.
Parameters | |
---|---|
context |
Context!: the Context to use |
mode |
Int: the power/wake mode to set |
See Also
start
open fun start(): Unit
Starts or resumes playback. If playback had previously been paused, playback will continue from where it was paused. If playback had been stopped, or never started before, playback will start at the beginning.
Exceptions | |
---|---|
java.lang.IllegalStateException |
if it is called in an invalid state |
stop
open fun stop(): Unit
Stops playback after playback has been started or paused.
Exceptions | |
---|---|
java.lang.IllegalStateException |
if the internal player engine has not been initialized. |
Protected methods
finalize
protected open fun finalize(): Unit
Exceptions | |
---|---|
java.lang.Throwable |
the Exception raised by this method |