Dig into the use of Android MediaPlayer

  • 2020-05-10 18:51:20
  • OfStack

1) how to get an MediaPlayer instance:
You can use direct new:
MediaPlayer mp = new MediaPlayer();
You can also use create as follows:
MediaPlayer mp = MediaPlayer.create (this, R.raw.test); // then you don't have to call setDataSource

2) how to set the file to be played:
The files to be played by MediaPlayer mainly include three sources:
a. resource resources that users have built into their applications
For example: MediaPlayer.create (this, R.raw.test);
b. Media files stored under an SD card or other file path
For example: mp setDataSource (". / sdcard test mp3 ");
c. Media files on the Internet
. For example: mp setDataSource (" http: / / www. citynorth. cn/music/confucius mp3 ");

MediaPlayer setDataSource1 has four methods:
setDataSource (String path)
setDataSource (FileDescriptor fd)
setDataSource (Context context, Uri uri)
setDataSource (FileDescriptor fd, long offset, long length)
 
When FileDescriptor is used, you need to put the files in the assets folder at the same level as the res folder, and then use:
AssetFileDescriptor fileDescriptor = getAssets().openFd("rain.mp3");
m_mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(),fileDescriptor.getStartOffset(), fileDescriptor.getLength());
To set up the datasource

3) main control methods of 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 of setting the player into the prepare state. It is important to note that if the MediaPlayer instance is created by the create method, there is no need to call prepare () before starting play for the first time, because it has already been called in the create method.
start() is the way to actually start the file playback,
pause() and stop() are easy to pause and stop,

seekTo () is positioning method, can let the players start playing from the specified location, it's important to note that this method is an asynchronous method, that is to say, when the method returns does not mean that positioning is completed, especially network file, real positioning to complete triggered when OnSeekComplete. onSeekComplete (), if you need can be called setOnSeekCompleteListener (OnSeekCompleteListener) set up the listener to handle.
release() can free up the resources occupied by the player, which should be called as early as possible once the player is no longer in use.
reset() will restore the player from the Error state to the Idle state.

4) set the listener of the player:
MediaPlayer provides some 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.
For example: setOnCompletionListener(MediaPlayer.OnCompletionListener listener),
setOnErrorListener(MediaPlayer.OnErrorListener listener), etc., when setting up the player, it is necessary to consider the possible situation of the player and set up the listening and processing logic, so as to keep the player robust.


Related articles: