Android mediaplayer state machine

Source: Internet
Author: User
Tags call back

The control of playing audio/video files and streams is managed by a state machine. Displays the lifecycle and status of a mediaplayer object that supports playback control operations. An ellipse represents the State in which the mediaplayer object may reside. An arc represents the playback control operation that drives mediaplayer migration between different States. There are two types of arcs. An arc starting with an arrow represents synchronous method calls, while an arc starting with a double arrow represents Asynchronous Method calls.
 

Through this figure, we can know that a mediaplayer object has the following states:

1. When a mediaplayer object is created with the new operator or the reset () method is called, it is in the idle state. When the release () method is called, it is in the end state. The two States are the lifecycle of the mediaplayer object.

  • There is a small but important difference between a newly built mediaplayer object and a mediaplayer object that calls the reset () method. Call getcurrentposition (), getduration (), getvideoheight (), getvideowidth (), setaudiostreamtype (INT), setlooping (Boolean), setvolume (float, float ), the pause (), start (), stop (), seekto (INT), prepare (), or prepareasync () methods are all programming errors. When a mediaplayer object is built, the internal playback engine and object status remain unchanged. At this time, call the above methods, the framework will not be able to call back the onerrorlistener registered by the client. onerror () method. However, if the mediaplayer object calls the reset () method and then calls the preceding methods, the internal playback engine calls back the onerrorlistener registered by the client. onerror () method, and pass in the error status.
  • Once a mediaplayer object is no longer used, we recommend that you call the release () method to release resources associated with this mediaplayer object in the internal playback engine. Resources may include single-State components such as hardware acceleration components. If the release () method is not called, The mediaplayer object instance may fail to use such single-State hardware resources, return to the software implementation or operation failure. Once the mediaplayer object enters the end state, it cannot be used any more and cannot be migrated to another State.
  • In addition, mediaplayer objects created using the new operator are in the idle state, while mediaplayer objects created using the reload create () method are not in the idle state. In fact, if the overloaded create () method is successfully called, these objects are already in the prepare state.

 


2. in general, some playback control operations may fail due to various reasons, such as unsupported audio/video formats and the absence of audio/video scanned by line. The resolution is too high, reasons such as stream timeout, etc. Therefore, Error Reporting and recovery are very important in this case. Sometimes, due to a programming error, a playback control operation may be called when it is in an invalid state. Under all these error conditions, the internal playing engine calls an onerrorlistener. onerror () method provided by the client programmer. Client programmers can register onerrorlistener by calling mediaplayer. setonerrorlistener (Android. Media. mediaplayer. onerrorlistener.

  • If an error occurs, the mediaplayer object enters the error state.
  • To reuse a mediaplayer object in the error state, you can call the reset () method to restore the object to the idle state.
  • It is a good programming habit to register an onerrorlistener to learn the internal playback engine errors.
  • If you call some methods in an invalid state, such as prepare (), prepareasync (), and setdatasource (), an illegalstateexception exception is thrown.


3. call the setdatasource (filedescriptor) method, the setdatasource (string) method, the setdatasource (context, Uri) method, or the setdatasource (filedescriptor, long, long) method) method to migrate an object in the idle state to the initialized state.

  • If the mediaplayer is in another State and the setdatasource () method is called, an illegalstateexception is thrown.
  • A good programming habit is to avoid ignoring the illegalargumentexception and ioexception that may be thrown when the setdatasource () method is called.


4. Before playing the video, the mediaplayer object must enter the prepared state.

  • There are two ways (synchronous and asynchronous) to bring the mediaplayer object into the prepared State: or call the prepare () method (synchronous ), if this method is returned, it means that the mediaplayer object has already entered the prepared state; or call the prepareasync () method (asynchronous). This method will make the mediaplayer object enter the preparing State and return, the internal playing engine will continue the unfinished preparation work. The onpreparedlistener. onprepared () Listener method provided by the client programmer is called when the synchronization version or asynchronous version preparation is complete. You can call mediaplayer. setonpreparedlistener (Android. Media. mediaplayer. onpreparedlistener) to register onpreparedlistener.
  • Preparing is an intermediate state. In this state, the result of calling any method with edge impact is unknown!
  • When prepare () and prepareasync () methods are called improperly, an illegalstateexception exception is thrown. When the mediaplayer object is in the prepared state, you can adjust the audio/video attributes, such as the volume, whether the screen is always on during playback, and loop playback.


5. To start playing the video, you must call the START () method. When this method is returned 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.

  • When it is in the started state, the internal playback engine calls the onbufferingupdatelistener. onbufferingupdate () callback method provided by the client programmer. This callback method allows the application to track the buffer state of stream playback.
  • The START () method is called for a mediaplayer object that is already in the started state.


6. You can pause, stop, and adjust the current playback position. When you call the pause () method and return it, The mediaplayer object enters the paused state. Note that the switching between the started and paused statuses is asynchronous in the internal playback engine. Therefore, it may take some time to update the status in the isplaying () method. If the stream content is being played, it may take several seconds.

  • Call the START () method to resume a paused mediaplayer object from the paused position. When the START () method is called, the status of the mediaplayer object changes to started.
  • The pause () method of a mediaplayer object that is already in the paused state has no effect.


7. Calling the stop () method will stop playing, and a mediaplayer in the started, paused, prepared, or playbackcompleted State will also enter the stopped state.

  • The stop () method of a mediaplayer object that is already in the stopped ped state has no effect.


8. Call the seekto () method to adjust the playback position.

  • The seekto (INT) method is executed asynchronously, so it can return immediately. However, it may take some time for the actual location playback operation to complete, especially for audio/video streams. After the actual location playback operation is completed, the internal playback engine calls the onseekcomplete. onseekcomplete () callback method provided by the client programmer. You can use the setonseekcompletelistener (onseekcompletelistener) method for registration.
  • Note that the seekto (INT) method can also be called in other states, such as prepared, paused, and playbackcompleted. In addition, you can call the getcurrentposition () method to obtain the current playback position, which can help applications such as music players continuously update the playback progress.


9. When the stream is played to the end, the playback is complete.

  • If the setlooping (Boolean) method is called to enable the loop mode, the mediaplayer object enters the started state again.
  • If the loop mode is not enabled, the internal playback engine calls the oncompletion. oncompletion () callback method provided by the client programmer. You can call mediaplayer. setoncompletionlistener (oncompletionlistener) to set the parameters. Once the internal playback engine calls the oncompletion. oncompletion () callback method, this mediaplayer object enters the playbackcompleted state.
  • When playbackcompleted is in the state, you can call the START () method to make the mediaplayer object enter the started state again.
Related Article

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.