In depth explanation of how to use Android mediaplayer

1) How to obtain mediaplayer instance: you can use the direct new method: mediaplayer MP = new mediaplayer(); You can also use the Create method, such as mediaplayer MP = mediaplayer. Create (this, r.raw. Test)// At this time, you don't need to call setdatasource. 2) how to set the file to be played: the file to be played by mediaplayer mainly includes three sources: a. the user's own resource resources in the application, such as mediaplayer. Create (this, r.raw. Test); b. Media files stored in SD card or other file paths, for example: mp.setdatasource ("/ sdcard / test. Mp3"); c. Media files on the network, for example: mp.setdatasource(“ http://www.citynorth.cn/music/confucius.mp3 "); There are four setdatasource methods in mediaplayer: setdatasource (string path) setdatasource (filedescriptor FD) setdatasource (context context, URI) setdatasource (filedescriptor FD, long offset, long length). When using filedescriptor, you need to put the file in the assets folder at the same level as res folder, Then use: assetfiledescriptor filedescriptor = getassets(). Openfd ("rain. Mp3"); m_ mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(),fileDescriptor.getStartOffset(),fileDescriptor.getLength()); To set the main control method of datasource 3) to the player: Android controls the playback of media files by controlling the player's state. Prepare() and prepareasync() provide synchronous and asynchronous ways to set the player to enter the prepare state. It should be noted that if the mediaplayer instance is created by the Create method, Then you don't need to call prepare () before starting playback for the first time, because it has been called in the Create method. Start () is the method to really start file playback. Pause () and stop () are relatively simple and play the role of pausing and stopping playback,

Seekto() is a location method that allows the player to start playing from the specified location. It should be noted that this method is an asynchronous method, that is, when this method returns, it does not mean that the location is completed, especially for the played network files. Onseekcomplete. Onseekcomplete() will be triggered when the real location is completed. If necessary, setonseekcompletelistener can be called (onseekcompletelistener) set the listener to handle. Release() can release the resources occupied by the player. Once it is determined that the player is no longer in use, it should be called as soon as possible to release the resources. Reset() can recover the player from the error state and return to the idle state.

4) Set the listener of the player: mediaplayer provides some methods to set different listeners to better monitor the working status of the player, so as to deal with various situations in time, such as setoncompletionlistener (mediaplayer. Oncompletionlistener listener), setonerrorlistener (mediaplayer. Onerrorlistener listener), etc, When setting the player, you need to consider the possible situations of the player and set the monitoring and processing logic to maintain the robustness of the player.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>