Android mediaplayer for playing audio or video files

Source: Internet
Author: User

 

1. Let's first look at the video or video source:

SD card, mobile phone storage (such as multimedia files carried by applications in RES/raw), network.

 

 

2. for Android, there is no difference in playing various video files. The only difference may be the method of re-loading files. Let's take a look at the ways mediaplayer provides:

 

 

 

Or you can call the following code in a program:

 

 

 

 

 
3) main player control methods:
Android controls the playback of media files by controlling the player status:
Prepare () and prepareasync () provide two synchronous and asynchronous methods to set the player to enter the prepare status. Note that if the mediaplayer instance is created by the create method, therefore, you do not need to call prepare () before starting the playback for the first time, because prepare () has been called in the create method.
Start () is the method for truly starting file playback,
Pause () and stop () are relatively simple and play the role of pausing and stopping playback,

Seekto () is a positioning method that allows the player to start playing from the specified position. It should be noted that this method is an Asynchronous Method. That is to say, when this method is returned, it does not mean that the positioning is complete, in particular, the onseekcomplete is triggered when the network file is located. onseekcomplete (). If needed, you can call setonseekcompletelistener (onseekcompletelistener) to set the listener for processing.
Release () can release the resources occupied by the player. Once it is determined that the player is no longer used, it should be called to release the resources as soon as possible.
Reset () enables the player to recover from the error state and then return to the idle state.

 

Setlooping (Boolean flag); Whether to play cyclically.

4) set the player listener:
Mediaplayer provides some ways to set different listeners to better monitor the player's working status, in order to timely handle various situations,
For example, setoncompletionlistener (mediaplayer. oncompletionlistener listener ),
Setonerrorlistener (mediaplayer. onerrorlistener listener) and so on. When you set the player, you must set the listening and processing logic to ensure the robustness of the player.

In addition to the first time, we found many methods for event Listening Classes in the API:

 

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.