1 How to obtain MediaPlayer example:
You can use the direct new method:
MediaPlayer MP = new MediaPlayer ();
You can also use the method of create, such as:
MediaPlayer MP = Mediaplayer.create (this, r.raw.test);/Then you don't have to call Setdatasource.
2 How to set the file to play:
MediaPlayer the files to be played mainly include 3 sources:
A. Resource resources that users have taken in advance from their applications
For example: Mediaplayer.create (this, r.raw.test);
b. media files stored in SD card or other file path
For example: Mp.setdatasource ("/sdcard/test.mp3");
c. Media files on the network
For example: 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)
When using FileDescriptor, you need to place the file in the Assets folder at the peer of the Res folder, and then use:
Assetfiledescriptor filedescriptor = Getassets (). OPENFD ("Rain.mp3");
M_mediaplayer.setdatasource (Filedescriptor.getfiledescriptor (), Filedescriptor.getstartoffset (), Filedescriptor.getlength ());
To set the DataSource
3 The main control methods for the player:
Android controls the playback of media files by controlling the state of the player, where:
Prepare () and Prepareasync () provide both synchronous and asynchronous ways to set the player into the prepare state, and it should be noted that if the MediaPlayer instance was created by the Create method, You do not need to call prepare () before you start playback for the first time, because the Create method has already been invoked.
Start () is the way to actually start the file playback.
Pause () and stop () are simpler, play a role in pausing and stopping playback,
Seekto () is a positioning method that lets the player play from a specified location, and it should be noted that the method is an asynchronous method, meaning that the method returns does not mean that the location is complete, especially the network files that are playing. The Onseekcomplete.onseekcomplete () is triggered when the true location is complete, and can be handled by invoking the Setonseekcompletelistener (Onseekcompletelistener) settings listener.
Release () releases the resources that the player occupies and should be invoked as soon as possible when it is determined that the player should no longer be used.
Reset () allows the player to recover from the error state and return to the idle state.
4 Set the listener for the viewer:
MediaPlayer provides a number of ways to set up different listeners to better monitor the working state of the player in order to deal with various situations in a timely manner,
such as: Setoncompletionlistener (Mediaplayer.oncompletionlistener listener),
Setonerrorlistener (Mediaplayer.onerrorlistener Listener) and so on, set the player need to take into account the player may be the situation set up listening and processing logic to maintain the player's robustness.