[Translation] class android. Media. mediaplayer

Source: Internet
Author: User
Tags call back
Mediaplayer can be used to control the playback of audio/video files or streams. For more information about how to use this class, see VideoviewClass documentation.

1. Status chart

The control of playing audio/video files and streams is managed by a state machine. ShowMediaplayerThe lifecycle and status of the playback control operation drive supported by the object. Elliptical RepresentationMediaplayerThe status where the object may reside. Arc represents driveMediaplayerPlayback control operations for migration between different statuses.There are two types of arcs. An arc starting with an arrow indicates synchronous method call, while an arc starting with a double arrow indicates Asynchronous Method call..

 

Through this figure, we can knowMediaplayerThe object has the following states:

1) whenMediaplayerThe object has just been usedNewOperator created or calledReset ()Method, it is inIdleStatus. WhenRelease ()Method, it is inEndStatus. Between the two States isMediaplayerObject lifecycle.

1.1) in a newly built Mediaplayer Object and a called Reset () Method Mediaplayer There is a small but important difference between objects. InIdle Status, called Getcurrentposition () , Getduration () , Getvideoheight () , Getvideowidth () , Setaudiostreamtype (INT) , Setlooping (Boolean) , Setvolume (float, float) , Pause () , Start () ,Stop () , Seekto (INT) , Prepare () Or Prepareasync () All methods are programming errors. When Mediaplayer When an object is built, the internal playback engine and object status remain unchanged. When the above methods are called, the framework cannot call back the client. Program Registered Onerrorlistener. onerror () Method. However, if the mediaplayer object is called Reset() After the method is called, the internal playback engine calls back the method registered by the client. Onerrorlistener. onerror () Method, and pass in the error status.

1.2) we recommend that you, once a mediaplayer object is no longer used, call release () immediately () to release the internal playback engine and mediaplayer The resource associated with the object. Resources may include single-State components such as hardware acceleration components. If release () is not called () method may cause mediaplayer the object instance cannot use this single-State hardware resource, so it is returned to the software implementation or failed to run. Once the mediaplayer object enters the end status. It cannot be used any more and cannot be migrated to another status.

1.3) In addition, use the New operator to create a the mediaplayer object is in the idle status, the Create () mediaplayer It is not in the idle status. In fact, if the Create () method is successfully called, these objects are already in the prepare status.

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 callsOnerrorlistener. onerror ()Method. The client programmer can callMediaplayer. setonerrorlistener (Android. Media. mediaplayer. onerrorlistener)Method to registerOnerrorlistener.

2.1) once an error occurs,MediaplayerThe object will enterErrorStatus.

2.2) to reuseErrorStatusMediaplayerObject that can be calledReset ()Method to restore this objectIdleStatus.

2.3) registerOnerrorlistenerIt is a good programming habit to learn about internal playback engine errors.

2.4) call some methods in an invalid state, suchPrepare (),Prepareasync ()AndSetdatasource ()Method will throwIllegalstateexceptionException.

3)CallSetdatasource (filedescriptor)Method, orSetdatasource (string)Method, orSetdatasource (context, Uri)Method, orSetdatasource (filedescriptor, long, long)Method will makeIdleStatus object migrationInitializedStatus.

3.1) IfMediaplayerIn another State, callSetdatasource ()Method will throwIllegalstateexceptionException.

3.2) Good Programming habits are not to neglect the callSetdatasource ()Method may throwIllegalargumentexceptionException andIoexceptionException.

4) before playing the video,MediaplayerThe object must enterPreparedStatus.

4.1) two methods (synchronous and asynchronous) can be used Mediaplayer Object entry Prepared Status: either call Prepare () Method ( Synchronization ).Mediaplayer The object is already in Prepared Status; or call Prepareasync () Method ( Asynchronous ), This method will make this Mediaplayer Object entry Preparing Status and return, while the internal playing engine continues the unfinished preparation work. When the synchronous version is returned or the asynchronous version preparation is complete, the client programmer will call Onpreparedlistener. onprepared () Listener method. Yes Mediaplayer. setonpreparedlistener (Android. Media. mediaplayer. onpreparedlistener) Method to register Onpreparedlistener .

4.2)PreparingIs an intermediate state. In this state, the result of calling any method with edge impact is unknown!

4.3) called in an inappropriate statusPrepare ()AndPrepareasync ()Method will throwIllegalstateexceptionException. WhenMediaplayerThe object is inPreparedStatus, 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, you must callStart ()Method. When this method is returned successfully,MediaplayerObject inStartedStatus.Isplaying ()Method can be called to testMediaplayerWhether the object is inStartedStatus.

5.1) WhenStartedThe internal playback engine callsOnbufferingupdatelistener. onbufferingupdate ()Callback method, which allows applications to track the buffer status of stream playback.

5.2)StartedStatusMediaplayerObject callStart ()Method has no effect.

6) you can pause, stop, and adjust the current playback position. WhenPause ()When the method is returnedMediaplayerObject entryPausedStatus. Note:StartedAndPausedMutual state conversion is asynchronous in the internal playback engine. So it may take some timeIsplaying ()Method to update the status. If the stream content is played, it may take several seconds.

6.1) CallStart ()Method will makePausedStatusMediaplayerThe object resumes playing from where it was paused. WhenStart ()When the method returns,MediaplayerThe object status changesStartedStatus.

6.2)PausedStatusMediaplayerObjectPause ()Method has no effect.

7) CallStop ()The method will stop playing and makeStarted,Paused,PreparedOrPlaybackcompletedStatusMediaplayerEnterStoppedStatus.

7.1)StoppedStatusMediaplayerObjectStop ()Method has no effect.

8) CallSeekto ()You can adjust the playback position.

8.1)Seekto (INT)The method is executed asynchronously, so it can return immediately, but it may take some time to complete the actual positioning playback operation, especially in the form of stream audio/video. The internal playback engine callsOnseekcomplete. onseekcomplete ()Callback method. You can useSetonseekcompletelistener (onseekcompletelistener)Method registration.

8.2) Note,Seekto (INT)Methods can also be called in other states, suchPrepared,PausedAndPlaybackcompletedStatus. In addition, you can call the current playback position.Getcurrentposition ()Method to update the playback progress.

9) when the stream is played to the end, the playback is complete.

9.1) IfSetlooping (Boolean)Method to enable the loop mode.MediaplayerThe object will enter againStartedStatus.

9.2) if the loop mode is not enabled, the internal playback engine will callOncompletion. oncompletion ()Callback method. You can callMediaplayer. setoncompletionlistener (oncompletionlistener)Method. Once the internal playback engine is calledOncompletion. oncompletion ()Callback method.MediaplayerThe object entersPlaybackcompletedStatus.

9.3) WhenPlaybackcompletedStatus, you can callStart ()Method To make thisMediaplayerObjectStartedStatus.

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.