Detailed explanation of the basic usage of mediaplayer developed by Android
This article describes the basic usage of Android mediaplayer with examples. Share with you for your reference, as follows:
The simplest example of playing audio or video using mediaplayer:
Java code section:
Layout file main.xml:
Procedure description:
This example only describes the basic use steps and methods of mediaplayer. Mediaplayer also has a variety of use methods and methods, which are not limited to the one introduced in the example. Specifically:
1) How to get a mediaplayer instance:
You can use the direct new method:
You can also use the Create method, such as:
2) How to set a file to play:
The files to be played by mediaplayer mainly include three sources:
a. Resource resources provided by the user in advance in the application
For example:
b. Media files stored on SD card or other file paths
For example:
c. Media files on the network
For example:
Mediaplayer's setdatasource has four methods:
① setDataSource(String path) ② setDataSource(FileDescriptor fd) ③ setDataSource(Context context,Uri uri) ④ setDataSource(FileDescriptor fd,long offset,long length)
3) Main control methods for player:
Android controls the playback of media files by controlling the state of the player, where:
Prepare () and prepareasync () provide synchronous and asynchronous methods to set the player to the prepare state. It should be noted that if the mediaplayer instance is created by the Create method, it is not necessary to call prepare () before starting playback for the first time, because it has been called in the Create method. Start() is a method to start file playback. Pause() and stop() are relatively simple and play the role of pausing and stopping playback. Seekto() is a positioning 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 positioning is completed, especially for the played network files, Onseekcomplete. Onseekcomplete() will be triggered when the real positioning is completed. If necessary, you can call setonseekcompletelistener (onseekcompletelistener) to set the listener for processing. Release () can release the resources occupied by the player. Once it is determined that the player is no longer used, it should be called as soon as possible to release resources. Reset () enables the player to recover from the error state and return to the idle state.
4) Set the listener for 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,
For example:
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.
More readers interested in Android related content can view the special topics of this site: summary of Android multimedia operation skills (audio, video, recording, etc.), introduction and advanced tutorial of Android development, summary of Android view skills, summary of activity operation skills of Android programming, summary of Android file operation skills, summary of Android resource operation skills And Android control usage summary
I hope this article will help you in Android programming.