Media Player Federation methods and methods

Source: Internet
Author: User

7.2.2 mediaplayer status

The playback process of audio and video is the state conversion process of the mediaplayer object. A deep understanding of mediaplayer's state machine is the basis for flexible control of ophone multimedia programming. Figure 7-3 shows the status chart of the mediaplayer. The status of the mediaplayer is marked with an elliptical shape. Status switching is represented by an arrow. A single arrow indicates that status switching is a synchronization operation, the double arrow indicates that the status switch is asynchronous.

1. create and destroy

Mediaplayer can be created using the static method mediaplayer. Create () or new operator. The mediaplayer objects created using the two methods are in different States. The mediaplayer objects created using the CREATE () method are in the prepared state, because the system has called the setdatasource () and prepare () Methods Based on the Resource ID of the parameter, the mediaplayer object created using the new operator is in the idle state. Except that the created mediaplayer object is in the idle state, the mediaplayer status after the reset () method is called is also in the idle state. Because the idle mediaplayer does not have a data source set, it cannot obtain multimedia length, video height, and other information. Therefore, the following method is a typical programming error. The following method is called for the created mediaplayer object. The underlying system does not call the onerrorlistener registered by mediaplayer, and the status of mediaplayer does not change. If you call the following method for a mediaplayer object that enters the idle state by calling the reset () method, the underlying system calls the onerrorlistener. onerror () method, and the mediaplayer object enters the error state.

 
(Click to view the big chart) Figure 7-3 mediaplayer status chart

Getcurrentposition ()

Getdruation ()

Getvideowidth ()

Getvideoheight ()

Setaudiostreamtype ()

Setlooping ()

Setvolume ()

Pause ()

Start ()

Stop ()

Seekto ()

Prepare ()

Prepareasync ()

For a mediaplayer object that is no longer needed, you must call the release () method to bring it into the end state, because it is related to the release of resources. If the mediaplayer object does not release resources such as hardware accelerators, then the created mediaplayer object will not be able to use this unique resource, or even cause creation failure. A mediaplayer in the end state means that its lifecycle ends and cannot be returned to other States.

2. Initialization

Before playing audio and video, you must initialize the mediaplayer. This is done in two steps. Calling the overloaded setdatasource () method will enable the mediaplayer object to enter the initialized state. Then calling the prepare () or prepareasync () method will enable the mediaplayer object to enter the prepared state. Since the prepareasync () method is asynchronous, it is usually used to register onpreparedlistener () for the mediaplayer and start the player in the onprepare () method. When the mediaplayer object is in the prepared state, it means that the caller can obtain the multimedia duration and other information. In this case, you can call the mediaplayer method to set the player attributes. For example, call setvolume (float leftvolume, float rightvolume) to set the player volume.

3. Play, pause, and stop

Call the START () method and the mediaplayer enters the started state. The isplaying () method can be used to determine whether the mediaplayer is in the started state. When the mediaplayer plays a multimedia file from the network, you can use onbufferingupdatelistener. onbufferingupdate (mediaplayer MP, int percent) to listen on the buffer progress, where percent is 0 ~ An integer of 100, indicating the percentage of buffered multimedia data.

Call the pause () method and the mediaplayer enters the paused state. It should be noted that the transition from started to paused and from paused to started is an asynchronous process, that is, the mediaplayer status may be updated after a period of time. Consider this when you call isplaying () to query the player status.

Call the stop () method and the mediaplayer enters the stopped state. Once the mediaplayer enters the stopped state, you must call prepare () or prepareasyn () again to make it into the prepared State, so that the mediaplayer object can be reused and multimedia files can be played again.

4. Fast forward and fast return

You can call the seekto () method to adjust the mediaplayer media time to implement the fast return and fast forward functions. The seekto () method is asynchronous and returns immediately, but it may take some time to adjust the media time. If onseekcompletelistener is set for mediaplayer, The onseekcomplete () method is called. It must be noted that seekto () can be called not only in started state, but also in paused, prepared, and playbackcompleted state.

5. Playback End status

If the playback status ends naturally, the mediaplayer may enter two possible statuses. When the loop playback mode is set to true, the mediaplayer object remains in the started state; when the loop playback mode is set to false, the mediaplayer object's oncompletionlistener. the oncompletion () method is called, and the mediaplayer object enters the playbackcompleted state. For a player in the playbackcompleted status, call the START () method again to play the audio/video file again. It should be noted that when the player ends, the duration and size of the audio/video can still be obtained by calling getduration (), getvideowidth (), getvideoheight (), and other methods.

6. handle errors

Various Errors may occur when the player plays audio/video files, such as IO errors and multimedia file format errors. It is particularly important to correctly handle various errors during playback. To listen for error messages, you can register an onerrorlistener listener for the mediaplayer object. When an error occurs, the onerrorlistener. onerror () method is called, and the mediaplayer object enters the error state. If you want to reuse the mediaplayer object and restore it from the error, you can call the reset () method to enable the mediaplayer to enter the idle state again. In short, it is very important to monitor the mediaplayer status. When an error occurs, prompting the user and restoring the player status is the correct solution.

In addition to the preceding errors, if a method is called at an inappropriate time, the illegalstateexception will be thrown. in the program, try/catch blocks should be used to catch such programming errors.

Now, we have introduced the status chart of mediaplayer in detail. The following describes how to use mediaplayer APIs through a media player instance.

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.