Some ways to use Android MediaPlayer CV

Source: Internet
Author: User

Here are some simple ways to MediaPlayer, easy to use it later!

1) How to obtain MediaPlayer instances: You can use the direct new method: MediaPlayer MP = new MediaPlayer (), or you can use the Create method, such as: MediaPlayer MP = Mediaplayer.create (this, r.raw.test);//You do not have to call Setdatasource 2) How to set the file to play: MediaPlayer to play the file mainly includes 3 sources: A. Resource resources that the user has in advance in the application such as: Mediaplayer.create (this, r.raw.test), B. media files stored in the SD card or other file path for example: Mp.setdatasource ("/sdcard/ Test.mp3 "); C. media files on the network such as: Mp.setdatasource ("Http://"); MediaPlayer's Setdatasource altogether four methods: Setdatasource (String path) setdatasource (FileDescriptor fd) Setdatasource (Context Context, Uri Uri) Setdatasource (filedescriptor fd, long offset, long length) where filedescriptor is used, the file needs to be placed on the same peer as the Res folder Assetsfolder, then use: assetfiledescriptor FileDescriptor = Getassets (). OPENFD ("Rain.mp3");
M_mediaplayer.setdatasource (Filedescriptor.getfiledescriptor (), Filedescriptor.getstartoffset (), Filedescriptor.getlength ()); To set DataSource 3) The main control method for the player: Android controls the playback of media files by controlling the state of the player, where: Prepare () and Prepareasync () provides both synchronous and asynchronous settings for the player to enter the prepare state, it is important to note that if the MediaPlayer instance was created by the Create method, then prepare () will not need to be called before the first start of playback. Because the Create method has already been called. Start () is a real way to start the file playback, pause () and stop () is relatively simple, play the role of pause and stop play,
Seekto () is a positioning method that allows the player to start playing from the specified position, noting that the method is an asynchronous method, which means that the method returns does not mean that the location is complete, especially the network file being played, Onseekcomplete.onseekcomplete () is triggered when the true location is complete and can be handled by invoking the Setonseekcompletelistener (Onseekcompletelistener) Setup listener if necessary. Release () frees the resource that the player occupies and should be called as soon as possible when the player is no longer in use. Reset () Restores the player from the error state and resumes to the idle state. 4) Player Listener Method Mmediaplayer.setoncompletionlistener ()//When playback ends call this method Mmediaplayer.setonpreparedlistener ()// This listener is triggered when the Mmediaplayer.prepareasync () method is called, registering a callback function that calls Mmediaplayer.setonerrorlistener () when the media source is ready to play, or when an error occurs during an asynchronous operation. Register a callback function to call if playback fails and call this method Mmediaplayer.setonseekcompletelistener ()//This method is when you call Mediaplayer.seekto () to locate from where to start playing, This method is recalled when the true location is successful

Some ways to use Android MediaPlayer CV

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: 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.