Android MediaPlayer State Machine

Source: Internet
Author: User
Tags call back

Control over the playback of audio/video files and streams is managed through a state machine. Displays aMediaPlayerThe object is supported by the playback control operation driven by the life cycle and state. Ellipse representsMediaPlayerThe state in which the object may reside. ARC indicates the driveMediaPlayerA playback control operation that migrates between various states.There are two types of arcs here. Arcs that start with an arrow represent synchronous method calls, whereas arcs represented by a double-headed arrow represent asynchronous method calls



Through this picture, we can know aMediaPlayerThe object has the following status:

1) When a MediaPlayer object has just been created with the new operator or called the Reset () method, 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.1) in a newly builtMediaPlayerObject and a call to theReset ()Method ofMediaPlayerThere is a small but very important difference between the objects. When you are in the idle state, call thegetcurrentposition (),getduration (),getvideoheight (),getvideowidth (),setaudiostreamtype (int),setlooping (Boolean),SetVolume (float, float),Pause (),Start (),Stop (),seekto (int),Prepare ()OrPrepareasync ()Methods are programming errors. When aMediaPlayerWhen the object has just been built, the internal playback engine and the state of the object have not changed, at this time to call the above methods, the framework will not be able to callback the client program registrationOnerrorlistener.onerror ()method, but if the MediaPlayer object calls theReset ()method, the internal playback engine will call back the client program to register theOnerrorlistener.onerror ()method, and the wrong state is passed in.
1.2) We recommend that once aMediaPlayerobject is no longer being used, it should be called immediatelyrelease ()method to release the inside of the playback engine with thisMediaPlayerThe resource associated with the object. Resources may include single-state components, such as hardware-accelerated components, if not calledrelease ()Method may result in subsequentMediaPlayerObject instances cannot use this single-state hardware resource to bounce back to the software implementation or fail to run. OnceMediaPlayerThe object enters the end state, it can no longer be used, and there is no way to migrate to another state.
1.3) In addition, useNewOperator creates aMediaPlayerobject is in an idle state, and those that pass the overloadedCreate ()The convenience method creates theMediaPlayerThe object is not in an idle state. In fact, if you successfully call the overloadedCreate ()method, then these objects are already in the prepare state.

2)in general, some playback control operations may fail for a variety of reasons, such as unsupported audio/video formats, missing interlaced audio/video, high resolution, stream timeouts, 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.
2.1) Once an error occurs,MediaPlayerThe object enters the error state.
2.2) in order to reuse an error stateMediaPlayerObject that can be calledReset ()method to restore the object to an idle state.
2.3) Register AOnerrorlistenerIt is good programming practice to know the errors that occur in the internal playback engine.
2.4) Call some methods in an illegal state, such asprepare (), Prepareasync ()AndSetdatasource ()Method will throwillegalstateexceptionAbnormal.

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.
3.1) If thisMediaPlayerIn other states, call theSetdatasource ()method, it throwsillegalstateexceptionAbnormal.
3.2) Good programming habit is not to neglect the callSetdatasource ()Method may be thrown at the time of theillegalargumentexceptionExceptions andIOExceptionAbnormal.

4) Before starting playback, the MediaPlayer object must enter the prepared state.
4.1) There are two methods (synchronous and asynchronous) that can makeMediaPlayerObject enters prepared state: either callPrepare ()MethodSync), this method returns a representation of theMediaPlayerObject has entered the prepared state;Prepareasync ()MethodAsynchronous), this method causes theMediaPlayerThe object enters the preparing state and returns, while the internal playback engine continues the unfinished preparations. When a synchronous version is returned or when the preparation of an asynchronous version is fully completed, the client programmer is calledonpreparedlistener.onprepared ()Monitoring method. Can callMediaplayer.setonpreparedlistener (Android.media.MediaPlayer.OnPreparedListener)method to registerOnpreparedlistener.
4.2) Preparing is an intermediate state in which the result of invoking any method with an edge effect is unknown!
4.3) call in an inappropriate statePrepare ()AndPrepareasync ()Method will throwillegalstateexceptionAbnormal. WhenMediaPlayerObject is inPreparedStatus, you can adjust the audio/video properties, such as the volume, whether the playback is always on, loop playback, etc.

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.
5.1) When in the started state, the internal playback engine calls the client programmer to provideonbufferingupdatelistener.onbufferingupdate ()callback method that allows the application to track the buffered state of the stream playback.
5.2) for an already in started stateMediaPlayerObject invocationStart ()Method has no effect.

6) Playback can be paused, stop, and adjust 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.
6.1) CallStart ()Method will let a paused state of theMediaPlayerThe object resumes playback from where it was previously paused. When callingStart ()When the method returns,MediaPlayerThe state of the object becomes the started state again.
6.2) for an already in paused stateMediaPlayerObjectPause ()Method has no effect.

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.
7.1) for an already in stopped stateMediaPlayerObjectStop ()Method has no effect.

8) Call the Seekto () method to adjust the playback position.
8.1)seekto (int)The method is executed asynchronously, so it can be returned immediately, but the actual location play operation may take some time to complete, especially in the form of streaming audio/video. When the actual location play operation is complete, the internal playback engine calls the client programmer for theOnseekcomplete.onseekcomplete ()callback method. can be done bySetonseekcompletelistener (Onseekcompletelistener)Method registration.
8.2) Note thatseekto (int)Methods can also be called in other states, such as prepared,paused and playbackcompleted states. In addition, the current playback position can actually be calledgetcurrentposition ()method to get, it can help the application such as music player to constantly update the playback progress

9) When playing to the end of the stream, playback is complete.
9.1) If you call thesetlooping (Boolean)Method turns on the loop mode, then thisMediaPlayerThe object is re-entered into the started state.
9.2) If no loop mode is turned on, the internal playback engine will call the client programmer to provideoncompletion.oncompletion ()callback method. Can be called by theMediaplayer.setoncompletionlistener (Oncompletionlistener)method to set the. The internal playback engine once called theoncompletion.oncompletion ()Callback method that explains theMediaPlayerThe object entered the playbackcompleted state.
9.3) When you are in the playbackcompleted state, you can callStart ()method to make thisMediaPlayerThe object then enters the started state.

Android MediaPlayer State Machine

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.