Published on Oct 13,2014
Email Post

Android Media Player

A Media Player is the easier way, if the user wants to play an audio file in the background or somewhere in an application. A Media Player class can be used to control playback of audio/video files and streams.

The following code will give an insight:

MediaPlayer mp;
Mp = MediaPlayer.create(this,;

Inside the Eclipse, we go to:

Folder>Resources>new> . We then create a new folder ‘Raw’. The MediaPlayer object is subsequently created.


Here in the application there is a ‘play’ and a ‘stop’ button. Let us observe the code :

MediaPlayer mp;
Boolean status = false;

The status variable is used to check whether the media is played on or not. A play and a stop button is attached to the click listener:

Here the media file is played and stopped with an ‘if’ statement:
If(status == true){
Status = true;
Mp= MediaPlayer.create(this, R.raw.song_heer);

Here the last file that was played is stopped and the status variable is set to ‘True’.  The onclick listener is attached and then a check is done to see if the media file is played. The ‘stoplistener’ is used to stop the media from playing.

We then go to API and define a path and then it will automatically play. The progress bar is also a part of media player. We then check the output:

If we call on ‘mp.pause()’ when a song is played and then call on ‘mp.start()’ again, it will pick up the file from the position it was paused at. If we use media in any of the activity, we must make sure that whenever we are overriding the on ‘onBackPressed’ , we must make sure the media is stopped so that it does not keep playing in the background. The ‘mp.stop()’ must be executed before we finish on the activity.

Video Player

The video player has the following command line:

VideoView myVideoView=(VideoView)findViewById(;
myVideoView.setMediaController(new MediaController(this));

The video player is given to user using a widget. The video files are to be placed on the same folder (raw folder).

In the Images & Media, we drag and drop the video view object. We take the reference of ‘VideoView’. Whenever we work on ‘Videoview’ we have to give it a file path. The video files are heavy and large in size, but they are not picked by r binding. We must refer to exact file path of video file through the code:

Url path = Uri.parse(“android.resource://” + getPackageName().toString()+”/”;

In this statement, the Ur.parse goes to android resource folder and looks out for the package name. Then it appends a forward slash to find the raw file.  We then set the “VideuURI” to the path and the Media controller is set as well:

vv.setMediaController(new MediaController(this));

The Output will give the video player inside the application.

Got a question for us? Mention them in the comments section and we will get back to you.

Related Posts:

Beginner’s Guide to Android Architecture

Android Development Course

About Author
Published on Oct 13,2014

Share on

Browse Categories