How To Implement Media Player In Android Studio?

Android provides a media player to play music (including audio and video files). It helps users to control the playback including start, stop, rewind, and pause.

Somish Kakadiya

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.

Syntax:

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.

Methods:

(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.

(2) Pause() :

This method is used to stop the music.

(3) Reset() :

This method is used to reset the media player and start music from the beginning.

(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.

(6) GetCurrentPosition() :

This method will return the current position of the song in milliseconds.

(7) GetDuration() :

This method returns the total time duration of the song in milliseconds

(8) Release() :

This method will release any resource attached to MediaPlayer.

(9) SetOnCompletionListener :

This used for to check the media player is complete and the user will perform some task on the complete media player.

Attach Seekbar With MediaPlayer

Complete Code Of MediaPlayer With Seekbar:
AndroidManifest.xml:
MainActivity.xml:
MainActivity.java

Conclusion:

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.

img

Somish Kakadiya

CTO of Vasundhara Infotech, a leading Software development company in the USA. His technological interests has helped the company in making useful decisions.

Categories

message

Have a project in mind? Drop a message to Bansi Pipaliya & start the discussion!

Get a Newsletter

Sign Up to our newsletter to get latest updates staight in your inbox.

Vasundhara respects your privancy. No Spam!

Get a Newsletter

Sign Up to our newsletter to get latest updates staight in your inbox.

Vasundhara respects your privancy. No Spam!

message

Have a project in mind? Drop a message to Bansi Pipaliya & start the discussion!