Table of contents
不积跬步,无以至千里;不积小流,无以成江海。要沉下心来,诗和远方的路费真的很贵!
[Android] MediaPlayer life cycle
Reference blog: MediaPlayer state diagram and life cycle
-
MediaPlayer
It isAndroid
a multimedia playback class in , which can control the playback process of audio and video, streaming files or local audio and video resources. -
The following figure shows
MediaPlayer
the state diagram of the entire life cycle.
-
In the figure above, the blue ovals represent
MediaPlayer
the various states, and the arcs represent the methods between state changes. Among them, the single-arrow arc indicates the call of the synchronous function, and the double-arrow arc indicates the call of the asynchronous function. -
MediaPlayer
The process fromIdle
state toEnd
state is its life cycle.
1. Idle state (ready state)
- Use
new
the method to createMediaPlayer
an instance. MediaPlayer
method usedreset()
.
2. End state
MediaPlayer
method usedrelease()
.- When
MediaPlayer
it is inEnd
the state, it can no longer be used, and it cannot return toMediaPlayer
other states at this time, because this life cycle has terminated.
3. Error state
-
after
MediaPlayer
callingreset()
the function. The upper layer application callsgetCurrentPosition()
,getDuration()
,getVideoHeight()
,getVideoWidth()
,setAudioAttributes(android.media.AudioAttributes)
,setLooping(boolean)
,setVolume(float, float)
,pause()
,start()
,stop()
,seekTo(long, int)
,prepare()
orprepareAsync()
these functions will fail. -
After an error, it will call back to the callback method provided by the user
OnErrorListener.onError()
, andMediaPlayer
it is inError
the state at this time. So once it is no longer usedMediaPlayer
, the function needs to be calledrelease()
so thatMediaPlayer
the resources can be released reasonably. -
In general, some playback control operations may fail for various reasons. Reasons like unsupported audio/video format, lack of interlaced audio/video, too high resolution, stream timeout, etc.
-
Once an error occurs,
MediaPlayer
the object entersError
the state. To reuse an object inError
stateMediaPlayer
,reset()
methods can be called to restore the object toIdle
state.
4. Initialized state
- Invoking
setDataSource()
a method of a type causesIdle
an object in state to transition toInitialized
state. - If the method
MediaPlayer
is called while in other states, an exceptionsetDataSource()
will be thrownIllegalStateException
.
5. Prepared state
- Before starting playback,
MediaPlayer
the object must enterPrepared
the state. - Entering
Prepared
the state, you can use both synchronous and asynchronous methods. - Synchronous method -
prepare()
, will return after the object entersPrepared
the state. - Asynchronous method - , will return
prepareAsync()
when the object is in the state, you need to register the monitoring method to determine whether to enter the state.Preparing
OnPreparedListener.onPrepared()
Prepared
- Only when
MediaPlayer
the object entersPrepared
the state can the properties of the audio and video be adjusted.
6. Prepare status
- Enter using
prepareAsync()
method.
7. Started status
- When
MediaPlayer
the object is inPrepared
the state, usestart()
the method to enterStarted
the state. - If
MediaPlayer
it is already inStarted
the state, callingstart()
the function again has no effect.
8. Paused state
-
When
MediaPlayer
the object is inStarted
the state, usepause()
the method to enterPaused
the state. -
When
MediaPlayer
the object is inPaused
the state, usestart()
the method to make it play again. -
If
MediaPlayer
it is already inPaused
the state, callingpause()
the function again has no effect. -
Started
Transitions between states andPaused
states are instantaneous, an asynchronous process inside the player.
9. Stopped status
-
When the function is called
stop()
,MediaPlayer
no matter which state is inStarted
,Paused
, orPrepared
, the state will be entered.PlaybackCompleted
Stopped
-
Once in
Stopped
the state,start()
the method cannot start until the callprepare()
orprepareAsync()
function is re-invoked, andPrepared
it can start while in the state. -
If
MediaPlayer
it is already inStopped
the state, callingstop()
the function again has no effect. -
seekTo
Functions can be called in other states, such asPrepared
the ,Paused
andPlaybackCompleted
states.
10. PlaybackCompleted state
-
The current playback position can
getCurrentPosition
be obtained through the function, which can track the playback progress of the player. -
When
MediaPlayer
the playback reaches the end of the data stream, a playback process is complete. -
Called in
MediaPlayer
advancesetLooping(boolean)
and set totrue
, it means looping, andMediaPlayer
it is still inStarted
the state. -
If it is called
setLooping(boolean)
and set tofalse
, and the listenerMediaPlayer
is registered on it in advance, the player will call back the function internally. This indicates the beginning of entry state.setOnCompletionListener
onCompletion
MediaPlayer
PlaybackCompleted
-
When in
PlaybackCompleted
the state, callingstart()
the function will restart the player to play the data from the beginning.
Summarize
-
The above process from
Idle
state to state is the life cycle of .End
MediaPlayer
-
Changes between states, what function to use, there are requirements. Which state must be in, and which functions can be used to change between states. Otherwise, an illegal exception will be thrown -
IllegalStateException
. -
When using a function, we need to pay attention to its current state, so understanding its life cycle is more beneficial for us to use it for audio and video playback control.