Android mediaplayer usage

Source: Internet
Author: User
1) how to obtain a mediaplayer instance: You can use the new method directly: Mediaplayer MP = new mediaplayer (); You can also use the Create method, such: Mediaplayer MP = mediaplayer. Create (this, R. Raw. Test); // you do not need to call setdatasource. 2) how to set the file to be played: Mediaplayer files to be played mainly include three sources: A. resource provided by the user in the Application Example: mediaplayer. Create (this, R. Raw. test ); B. Media files stored in SD cards or other file paths For example: MP. setdatasource ("/sdcard/testcard "); C. Media files on the network For example: MP. setdatasource (" Http://www.citynorth.cn/music/confucius.mp3"); The setdatasource of mediaplayer has four methods: Setdatasource (string path) Setdatasource (filedescriptor FD) Setdatasource (context, Uri URI) Setdatasource (filedescriptor FD, long offset, long length) Use When filedescriptor is used, you need to put the file at the same level as the res folder. AssetsFolder, and then use: Assetfiledescriptor filedescriptor = getassets (). openfd ("rainsets ");
M_mediaplayer.setdatasource (filedescriptor. getfiledescriptor (), filedescriptor. getstartoffset (), filedescriptor. getlength ()); To set datasource 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.
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.
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.