Android is providing a MediaPlayer class to access built-in media player services like playing audio, video e.t.c.
In Order To use MediaPlayer We have to call a static method to create() of this class.
MediaPlayer mediaplayer = MediaPLayer.create(this,R.raw.song1);
In this, the second parameter is the name of the song which you want to play.
You have to create a new folder under the project with the name raw and placed the music into it.
Once you have created the object of MediaPlayer you can call some predefined methods for start and stop the music.
(1) start() :
This method is used for playing music from the beginning.
If this method is called after pause() method, the music would start from where it is pause not from the beginning.
Ex. mediaplayer.start();
(2) Pause() :
This method is used to stop the music.
Ex. mediaplayer.pause();
(3) Reset() :
This method is used to reset the media player and start music from the beginning.
Ex. mediaplayer.reset();
(4) IsPlaying() :
This method is used to check the song is playing or not.
This method returns just true/false value.
(5) SeekTo(Position) :
This method takes an integer value and used to move the song to that particular position.
Ex. mediaplayer.seekTo(position);
(6) GetCurrentPosition() :
This method will return the current position of the song in milliseconds.
Ex. mediaplayer.getCurrentPosition();
(7) GetDuration() :
This method returns the total time duration of the song in milliseconds
Ex. mediaplayer.getDuration();
(8) Release() :
This method will release any resource attached to MediaPlayer.
Ex. mediaplayer.release();
(9) SetOnCompletionListener :
This used for to check the media player is complete and the user will perform some task on the complete media player.
(1) Initialize seekbar :
SeekBar seekbar = findViewById(R.id.seekbar);
(2) Set the seekbar Max progress to the MediaPlayer Duration:
Set seekbar maximum progress to the MediaPlayer Duration. It will take int value.
Ex. seekbar.setMax((int) duration);
Where the duration is the media player duration time.
(3) Use TimerTask for update seekbar progress :
Timer task will update the seekbar progress to the media player current position on every 1000 milliseconds.
Example
(4) Set seekbar progress change listener :
Set SeekBar Progress Change Listener for if a user updates the progress of the seekbar so that media player seek to that position of the seekbar.
Follow the above-mentioned steps and (ready-to-use) codes to implement them during the development process of your Android application.
It is a must-have feature of an application. Hence, learn to implement a media player in Android Studio and understand the process properly in this blog.
CTO of Vasundhara Infotech, a leading Software development company in the USA. His technological interests has helped the company in making useful decisions.
Sign Up to our newsletter to get latest updates staight in your inbox.
Vasundhara respects your privancy. No Spam!
Sign Up to our newsletter to get latest updates staight in your inbox.
Vasundhara respects your privancy. No Spam!