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) {

MusicDroid - Audio Player Part II

SDK Version: 

In part 2 of the MusicDroid media player tutorial series, we will move the MediaPlayer object into a remote service so that you can use the rest of the phone while playing your music. We will setup this service so that it will be simple to add the controls in part 3 for Pause, Stop, Skip forward, Skip back, etc. This service will also provide a marquee notification each time a new song starts, and a notification icon for play/pause status.

MusicDroid - Audio Player Part I

SDK Version: 

In part 1 of this 4 part series, we will setup a very basic music player that will list all songs on the SD card and allow the user to select a song to play. Over the course of this series we will build a fully functional MP3 player with animated controls and IDE3 tag support.

Syndicate content