Before writing this article, let me explain that this article may not really be for Android beginners, because there are some concepts that I will not be able to explain. In addition, in the blog's coherence, this article is also deficient. Time is pressing, only as a record article in the development process, for everyone to reference it!
First introduce the various states:
Define a concept in which the state between idle (after new MediaPlayer) and End (Mediaplayer.reset ()) is the life cycle of MediaPlayer
Idle state: When a MediaPlayer object is created using the new () method or its reset () method is called, the MediaPlayer object is in an idle state. An important difference between the two methods is that if you call the Getduration () method in this state (equivalent to an incorrect call timing), entering the idle state via the Reset () method will trigger Onerrorlistener.onerror (), And MediaPlayer will enter the error state, and if it is a newly created MediaPlayer object, it will not trigger onerror () and will not enter the error state.
End state: The release () method can enter the end state, as long as the MediaPlayer object is no longer in use, it should be released as soon as possible through the release () method to release the related hardware and software component resources, some of which are only one copy (equivalent to a critical resource). If the MediaPlayer object enters the end state, it will not enter any other state.
Initialized state: This state is relatively simple, MediaPlayer call Setdatasource () method to enter the Initialized state, indicating that the file to play at this time has been set up.
Prepared state: After initialization is completed, it is also necessary to call the prepare () or Prepareasync () method, one of which is synchronous, one is asynchronous, and only enters the Prepared state to indicate that MediaPlayer so far has no errors. You can play the file.
Preparing state: This state is better understood, mainly with Prepareasync (), if the asynchronous ready to complete, will trigger onpreparedlistener.onprepared (), and then enter the prepared state.
Started Status: Obviously, once MediaPlayer is ready, the start () method can be called so that the MediaPlayer is in the Started state, indicating that MediaPlayer is playing the file. You can use IsPlaying () to test whether the MediaPlayer is in the started state. If the playback is complete and the loop is set, the MediaPlayer will still be in the started state, similar to if MediaPlayer called Seekto () or start () in that state. Method can let MediaPlayer stay in started state.
Paused state: Started state MediaPlayer call the Pause () method to pause the MediaPlayer to enter the Paused state, MediaPlayer call Start again after pausing () You can continue the MediaPlayer playback, go to the started state, pause the state when you can call the Seekto () method, which will not change state.
Stop status: Started or paused can call stop () stop MediaPlayer, while MediaPlayer in the stop state wants to be re-played through Prepareasync () and prepare () Go back to the previous prepared state to start again.
playbackcompleted Status: The file is played properly, and the loop is not set to enter the state, and will trigger the Oncompletionlistener oncompletion () method. You can now call the start () method to play back the file from the beginning, or stop () MediaPlayer, or you can Seekto () to reposition the playback position.
Error status: If for some reason MediaPlayer error occurs, the Onerrorlistener.onerror () event is triggered, it is important that MediaPlayer go into the error state, and timely capture and properly handle these errors. Can help us release the relevant hardware and software resources in time, but also improve the user experience. The listener can be set through Setonerrorlistener (Android.media.MediaPlayer.OnErrorListener). If the MediaPlayer enters the error state, it can be restored by calling Reset (), allowing the MediaPlayer to return to the idle state again.
State machine
1. When a MediaPlayer object has just been created with the new operator or the Reset () method is called, it is in an idle state. When the release () method is called, it is in the end state. Between these two states is the life cycle of the MediaPlayer object.
1) There is a small but very important difference between a newly constructed MediaPlayer object and a MediaPlayer object that calls the Reset () method.
In the idle state, call GetCurrentPosition (), Getduration (), Getvideoheight (), Getvideowidth (), setaudiostreamtype (int), Setlooping (Boolean), SetVolume (float, float), pause (), start (), Stop (), seekto (int), prepare () or Prepareasync () Methods are programming errors.
When a MediaPlayer object has just been built, the internal playback engine and the state of the object have not changed, at this time to invoke the above methods, the framework will not be able to callback the client program registration of the Onerrorlistener.onerror () method;
However, if the MediaPlayer object calls the Reset () method and then calls the methods above, the internal playback engine will call back the Onerrorlistener.onerror () method registered by the client program and pass in the wrong state.
2) It is recommended that once a MediaPlayer object is no longer in use, the release () method should be called immediately to release the resource associated with the MediaPlayer object in the internal playback engine.
Resources may include single-state components such as hardware-accelerated components, and failing to call the release () method may result in the subsequent MediaPlayer object instance being unable to use the single-state hardware resource, thereby returning to the software implementation or failure. Once the MediaPlayer object enters the end state, it can no longer be used, and there is no way to migrate to another state.
3) In addition, the MediaPlayer object created with the new operator is in an idle state, and the MediaPlayer object created by the overloaded create () convenience method is not in an idle state. In fact, if the overloaded create () method is called successfully, then these objects are already prepare state.
2. In general, for various reasons some playback control operations may fail, such as unsupported audio/video format, lack of interlaced audio/video, resolution too high, stream timeout, and so on. Therefore, error reporting and recovery are very important in this case. Sometimes, because of a programming error, a playback control operation may occur in the case of an invalid state. Under all these error conditions, the internal playback engine invokes a Onerrorlistener.onerror () method provided by the client programmer. Client programmers can register Onerrorlistener by calling the Mediaplayer.setonerrorlistener (Android.media.MediaPlayer.OnErrorListener) method.
1) Once an error occurs, the MediaPlayer object enters the error state.
2) in order to reuse a MediaPlayer object in the error state, you can call the Reset () method to restore the object to an idle state.
3) Registering a onerrorlistener to learn that the internal playback engine error is a good programming habit.
4) Calling some methods in an illegal state, such as prepare (), Prepareasync (), and Setdatasource () methods, throws a IllegalStateException exception.
3. Call the Setdatasource (FileDescriptor) method, or the Setdatasource (String) method, or the Setdatasource (Context,uri) method, or Setdatasource ( The Filedescriptor,long,long) method migrates an idle object to the initialized state.
1) If the MediaPlayer is in a different state, calling the Setdatasource () method throws a IllegalStateException exception.
2) Good programming habits are not to be negligent in calling the Setdatasource () method when the illegalargumentexception exception and the IOException exception may be thrown.
4. Before starting playback, the MediaPlayer object must enter the prepared state.
1) There are two methods (synchronous and asynchronous) that enable the MediaPlayer object to enter the prepared state:
The first is to call the Prepare () method (synchronous), which means that the MediaPlayer object has entered the prepared state;
The second is to call the Prepareasync () method (async), which causes the MediaPlayer object to enter the preparing state and return, while the internal playback engine continues the unfinished preparations. The onpreparedlistener.onprepared () listener method provided by the client programmer is invoked when the synchronous version returns or when the asynchronous version preparation is complete. You can call the Mediaplayer.setonpreparedlistener (Android.media.MediaPlayer.OnPreparedListener) method to register the Onpreparedlistener.
2) Preparing is an intermediate state in which the result of invoking any method with an edge effect is unknown!
3) calling the prepare () and Prepareasync () methods in an inappropriate state throws a IllegalStateException exception. When the MediaPlayer object is in the prepared state, you can adjust the audio/video properties, such as the volume, whether the playback is always on, looping, and so on.
5. To start playback, you must call the start () method. When this method returns successfully, the MediaPlayer object is in the started state. The IsPlaying () method can be called to test whether a MediaPlayer object is in the started state.
1) When in the started state, the internal playback engine invokes the Onbufferingupdatelistener.onbufferingupdate () callback method provided by the client programmer, which allows the application to track the buffered state of the stream playback.
2) There is no effect on invoking the Start () method on a MediaPlayer object that is already in the started state.
6. Playback can be paused, stopped, and adjusts the current playback position. When the pause () method is called and returned, the MediaPlayer object is brought into the paused state. Note that the reciprocal conversion of the started to the paused state is asynchronous in the internal playback engine. So it may take a little time to update the state in the IsPlaying () method, which may take a few seconds if the stream content is being played.
1) calling the start () method causes a MediaPlayer object in the paused state to resume playback from where it was previously paused. When the start () method is called to return, the state of the MediaPlayer object becomes the started state again.
2) The Pause () method has no effect on a MediaPlayer object that is already in the paused state.
7. Calling the Stop () method stops playback and also causes a mediaplayer in the started,paused,prepared or playbackcompleted state to enter the stopped state.
The Stop () method has no effect on a MediaPlayer object that is already in the stopped state.
8. Call the Seekto () method to adjust the playback position.
1) the Seekto (int) method is executed asynchronously, so it can be returned immediately, but the actual location play operation may take some time to complete, especially audio/video in the form of a stream. When the actual location play operation is complete, the internal playback engine invokes the Onseekcomplete.onseekcomplete () callback method provided by the client programmer. Can be registered through the Setonseekcompletelistener (Onseekcompletelistener) method.
2) Note that the Seekto (int) method can also be called in other states, such as prepared,paused and playbackcompleted states. In addition, the current playback position can actually call the GetCurrentPosition () method to get, it can help the application such as music player to constantly update the playback progress
9. When played to the end of the stream, playback is complete.
1) If the Setlooping (Boolean) method is called to turn on the loop mode, the MediaPlayer object will re-enter the started state.
2) If no loop mode is turned on, the internal playback engine invokes the Oncompletion.oncompletion () callback method provided by the client programmer. Can be set by calling the Mediaplayer.setoncompletionlistener (Oncompletionlistener) method. The internal playback engine once called the Oncompletion.oncompletion () callback method, indicating that the MediaPlayer object entered the playbackcompleted state.
3) When in the Playbackcompleted state, the start () method can be called again to let the MediaPlayer object enter the started state again.
Android MediaPlayer Analysis