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://www.citynorth.cn/music/confucius.mp3"); 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
folder, 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