How to play video and audio on Android Part 2.

SDK Version: 

In this part, I'm going to focus on MediaPlayer. I'm going to show you, how to stop it properly, and how to create a media player more easily.
The default behavior is the same, with every activity, after onPause and onStop, if the activity is not destroyed, it will continue to run in the background, until it is killed so the system can free up the memory it is using. So until the activity is destroyed, the mediaplayer will continue to occupy space in the phones memory.

How to play video and audio on Android

SDK Version: 

There is more than one way, to play media files on an Android phone, let me show you two of them.


MediaPlayer is the easier way, if you just want to play an audio file in the background, somewhere in an appliaction. There are no ui controls here, but of course you can use MediaPlayer.stop(), play(), seekTo() ,etc. Just bind the needed functions to a button, gesture, or event. As you can see, it also throws a lot of exceptions, which you need to catch.

  1.  public void audioPlayer(String path, String fileName){
  2.     //set up MediaPlayer    
  3.     MediaPlayer mp = new MediaPlayer();
  5.     try {
  6.         mp.setDataSource(path+"/"+fileName);
  7.     } catch (IllegalArgumentException e) {
  8.         // TODO Auto-generated catch block
  9.         e.printStackTrace();
  10.     } catch (IllegalStateException e) {

Syndicate content