In Android, the MediaPlayer class is used for playing audio and video files. It provides a variety of methods to control and manage media playback. Here are some of the commonly used methods provided by the MediaPlayer class:
1. create(): This method creates an instance of the MediaPlayer class.
2. setDataSource(): This method sets the data source for the MediaPlayer, which can be a local file path, a URL, or a file descriptor. It prepares the MediaPlayer to play the specified media.
3. prepare(): This method prepares the MediaPlayer asynchronously for playback. It performs the necessary initialization and buffering of the media.
4. start(): This method starts or resumes playback of the media. If the media was previously paused, it resumes from where it was paused. If the media was stopped, it starts from the beginning.
5. pause(): This method pauses the playback of the media. It can be used to temporarily suspend the playback and resume it later using the start() method.
6. stop(): This method stops the playback of the media. It resets the MediaPlayer to its uninitialized state, and if start() is called again, the media will start from the beginning.
7. seekTo(): This method seeks to a specified position in the media file. It allows you to jump to a specific time in the media playback. The position is specified in milliseconds.
8. isPlaying(): This method returns a boolean value indicating whether the MediaPlayer is currently playing.
9. getDuration(): This method returns the total duration of the media in milliseconds. It can be used to determine the length of the media file.
10. getCurrentPosition(): This method returns the current playback position in milliseconds. It can be used to track the progress of the media playback.
11. setVolume(): This method sets the volume for the left and right audio channels of the MediaPlayer. It allows you to adjust the volume dynamically during playback.
12. setOnCompletionListener(): This method sets a listener to be called when the media playback is completed. It allows you to perform actions or trigger additional logic after the media has finished playing.
13. setOnErrorListener(): This method sets a listener to be called when an error occurs during media playback. It allows you to handle and respond to playback errors.
These are just a few examples of the methods provided by the MediaPlayer class. There are additional methods available for handling audio focus, looping, audio effects, and more. It's important to properly manage the MediaPlayer instance and handle its lifecycle to ensure efficient media playback in your Android application.