How to play audio file in MediaPlayer Android systems
6 minute(s) read
Published on: May 03, 2021
Updated on: Dec 14, 2021
Using MediaPlayer, the user can play different audio files and categorize them or access them through audio albums. This program has features that include the ability to show the song's duration, rewind or forward songs, change songs, and access all audio files on the device through this MediaPlayer.
What you will learn in this tutorial is as follows:
There are different ways to play audio in Android, one of which is very useful and convenient is MediaPlayer. In MediaPlayer, some methods can be used to play audio.
- Start () method:
Using this method, you can play the audio. If this method is called, the audio can start playing from the beginning.
- Pause () method:
This method is also used to stop playing audio files. In this case, if the audio has already been played using the start () method, you can use pause () to stop it. Of course, if we stop the audio and try to play it again, we use the start () method. In this way, using this method, the audio will be played from where it was, not from the beginning.
- Reset () method:
It is used to reset MediaPlayer.
- IsPlaying () method:
This method is also used to check if music is playing or not. It also returns true or false values and says whether the music is playing or not.
- SeekTo () method:
It should be noted that this method takes a value of type int and moves the audio to the same position. For example, giving a value of 26 to this method can cause the audio to start playing from 26 seconds.
This method can also send the current position of the player. Returns the audio in any situation.
Using this method, you can return the playing time of the audio.
If we use this method, we will cause all audio files to be played in sequence.
This method is used to adjust and increase or decrease the audio.
Note that this method takes an int value that can be used to select an audio file from the playlist.
Using this method, you can also return all the information related to the audio.
The inputs of this method are boolean. So, they return the values true or false as output. Of course, it should be noted that if the output of this method is true, the audio file will be repeated indefinitely until we use the pause () or stop () methods.
2- The type of activity that we use in this part of the training and project is Empty Activity.
3- The language used in this project is Java.
4- Define a TextView and six Buttons in the Activity Layout.
5- Each of the buttons we have defined is used to call a method.
6- Use TextView to show the current playback status of the audio file, its duration, and playback status.
7- The codes that should be in the activity_main.xml section are as follows:
8- Add a folder named raw to the res folder of the Android project.
9- We add a file in .mp3 format in the created folder.
10- Then, we complete the codes as below.
11- The codes that should be in the MainActivity.java section are as follows:
In the code above, we created a MediaPlayer class with the arbitrary name MediaPlayer.
Inside the onCreate () method, using MediaPlayer.create (), we introduced an audio file called song to medPlayer. The first inputs of this method are context and the second inputs are the location of the audio file.
Then I set true to SetLooping () in the code above, repeating until the music stops playing.
To save the audio file's duration and its related items, we define a variable of type int called timeInt.
In the code above, the audio file's duration is taken from medPlayer using GetDuration () and stored in milliseconds at timeInt. The audioTime () method is then called inside the activity and defined after onCreate ():
After inserting the above code, the audio file starts by pressing a button, and then its playback time is displayed in TextView.
In the code above, a value of 20,000 is defined for the SeekTo () method, the jump method. This means it can start again in 20 seconds.
For the status button, we define a condition that checks whether music is playing or not and if after checking the result, it is that the music is playing a message related to it. Otherwise, the message related to not playing is displayed will give.
12- We will implement the project.
13- After playing, we will see that the music will be played after pressing the button, and its playing time will also be displayed in TextView.
14- Then, if we select the jump button, the music will be transferred to 20 seconds in every second and any situation, and from there, it will start playing.
15- If we select the status button, the appropriate message will be displayed in TextView after checking.
16- If we click the current position button, it will call the getCurrentPosition () method and display the current position of the music player in TextView.
17- If we press the pause and stop buttons, they will perform the related operations.
Of course, it should be noted that MediaPlayer has other methods to call audio files from the URI and URL that can be used to play other items such as Alarm, Ringtone, online music playback, and so on.
In this tutorial, we learned how to use MediaPlayer and some of its most widely used methods, and we were able to create a project with several buttons, each of which calls a method. If you follow all the steps in this article carefully, you can easily do this project successfully and see the applications of each method.About our Android and iOS app development services