[Android] MediaPlayer life cycle


不积跬步,无以至千里;不积小流,无以成江海。要沉下心来,诗和远方的路费真的很贵!

[Android] MediaPlayer life cycle

Reference blog: MediaPlayer state diagram and life cycle

  • MediaPlayerIt is Androida 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 MediaPlayerthe state diagram of the entire life cycle.
    insert image description here

  • In the figure above, the blue ovals represent MediaPlayerthe 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.

  • MediaPlayerThe process from Idlestate to Endstate is its life cycle.

1. Idle state (ready state)

  • Use newthe method to create MediaPlayeran instance.
  • MediaPlayermethod used reset().

2. End state

  • MediaPlayermethod used release().
  • When MediaPlayerit is in Endthe state, it can no longer be used, and it cannot return to MediaPlayerother states at this time, because this life cycle has terminated.

3. Error state

  • after MediaPlayercalling reset()the function. The upper layer application calls getCurrentPosition(), getDuration(), getVideoHeight(), getVideoWidth(), setAudioAttributes(android.media.AudioAttributes), setLooping(boolean), setVolume(float, float), pause(), start(), stop(), seekTo(long, int), prepare()or prepareAsync()these functions will fail.

  • After an error, it will call back to the callback method provided by the user OnErrorListener.onError(), and MediaPlayerit is in Errorthe state at this time. So once it is no longer used MediaPlayer, the function needs to be called release()so that MediaPlayerthe 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, MediaPlayerthe object enters Errorthe state. To reuse an object in Errorstate MediaPlayer, reset()methods can be called to restore the object to Idlestate.

4. Initialized state

  • Invoking setDataSource()a method of a type causes Idlean object in state to transition to Initializedstate.
  • If the method MediaPlayeris called while in other states, an exception setDataSource()will be thrown IllegalStateException.

5. Prepared state

  • Before starting playback, MediaPlayerthe object must enter Preparedthe state.
  • Entering Preparedthe state, you can use both synchronous and asynchronous methods.
  • Synchronous method - prepare(), will return after the object enters Preparedthe 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.PreparingOnPreparedListener.onPrepared()Prepared
  • Only when MediaPlayerthe object enters Preparedthe state can the properties of the audio and video be adjusted.

6. Prepare status

  • Enter using prepareAsync()method.

7. Started status

  • When MediaPlayerthe object is in Preparedthe state, use start()the method to enter Startedthe state.
  • If MediaPlayerit is already in Startedthe state, calling start()the function again has no effect.

8. Paused state

  • When MediaPlayerthe object is in Startedthe state, use pause()the method to enter Pausedthe state.

  • When MediaPlayerthe object is in Pausedthe state, use start()the method to make it play again.

  • If MediaPlayerit is already in Pausedthe state, calling pause()the function again has no effect.

  • StartedTransitions between states and Pausedstates are instantaneous, an asynchronous process inside the player.

9. Stopped status

  • When the function is called stop(), MediaPlayerno matter which state is in Started, Paused, orPrepared , the state will be entered.PlaybackCompletedStopped

  • Once in Stoppedthe state, start()the method cannot start until the call prepare()or prepareAsync()function is re-invoked, and Preparedit can start while in the state.

  • If MediaPlayerit is already in Stoppedthe state, calling stop()the function again has no effect.

  • seekToFunctions can be called in other states, such as Preparedthe , Pausedand PlaybackCompletedstates.

10. PlaybackCompleted state

  • The current playback position can getCurrentPositionbe obtained through the function, which can track the playback progress of the player.

  • When MediaPlayerthe playback reaches the end of the data stream, a playback process is complete.

  • Called in MediaPlayeradvance setLooping(boolean)and set to true, it means looping, and MediaPlayerit is still in Startedthe state.

  • If it is called setLooping(boolean)and set to false, and the listenerMediaPlayer is registered on it in advance, the player will call back the function internally. This indicates the beginning of entry state.setOnCompletionListeneronCompletionMediaPlayerPlaybackCompleted

  • When in PlaybackCompletedthe state, calling start()the function will restart the player to play the data from the beginning.

Summarize

  • The above process from Idlestate to state is the life cycle of .EndMediaPlayer

  • 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.

Guess you like

Origin blog.csdn.net/qq_46546793/article/details/126728442