How to make an Audio player in Android?

Playing musics, sounds and tones are  very popular functions that are mostly required for a smartphone. By defaults, there is always at least one or more applications for this purpose implemented on the phone you’ve just bought from a shop. These things look almost completed and satisfy most of needs for using phones with audios. However, sometimes these idiots cannot fit your needs with some special styles or using more personally, not like others do. Maybe they will always load all you music files storing in your phone but you don’t want they doing like that. Or sometimes you just wanna hear musics on your phone but why those applications always request you to create some kinds of account and sign in for playing?… In these case, the defaults applications on your phone have doing more things than expected, they make you confused, and falling in troubles. Any more? They are not only doing lots of things than expected but also all things they do are not enough for what you need. Audio files are often recorded with different volume levels, this cause you have to adjust volume of speaker many times while listening a playlist with a number of songs. Why did these rubbishes do many things but nothing for this problem?

It is the time for you creating an Audio Player for yourself! Is it easy to do? – Yes, it is.

And today, I’m going to make a tutorial that will guide you how to program an Audio Player application on Android step by step. Follows this, you will understand the main flows and critical things that Android provide to developers for creating the useful and powerful applications doing this stuff.

The first thing for your reference is Android Developer site, which is launched at http://developer.android.com. At this time, when i am writing this tutorial, Android API latest version is currently level 21, Android L. And ofcourse, you can also find out a little information about programming to play audio files on Android at this link: http://developer.android.com/reference/android/media/MediaPlayer.html.

The most essential class is android.media.MediaPlayer which enables you to play an audio file on Android. Let take a look on a short Flow Diagram on how to use MediaPlayer to play an audio file below:

 

The first thing you need is creating an instance of class MediaPlayer through the default public constructor, then calls prepare() before start() to begin playing an audio data. While the MediaPlayer is playing your song, you can use method seekTo(int) with one argument being the time from beginning of song where you want to jump to. This time is calculated in milliseconds. Once Media Player finishes playing your song, it will stop automatically. But if you want to stop playing at some incomplete points, you can call method stop() to stop playing immediately.

private MediaPlayer mPlayer;

 

protected void onCreated(Bundle savedState) {

 

mPlayer = new MediaPlayer();

mPlayer.setDataResource(“/sdcard/test.mp3”);

mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

 

mPlayer.prepare();

mPlayer.start();

}

 

protected void onStop() {

mPlayer.stop();

mPlayer.release();

}

How about pausing and resuming while playing? Certainly you can. Looking at following Flow Diagram:

Sequence Diagram Audio Player - Pause and Resume

Sequence Diagram Audio Player – Pause and Resume

Calling start() when Player is stopping will tell Player start playing your song from the beginning. On another hand, Media Player will resume where you stopped before when you called pause() method, and start playing from that point.

Every instance of Media Player can play one song at once. You might want to play more songs at the same time, in that case, you are able to create more instances for more songs and start all of them. But when you want to reuse an instance after finishing one songs to play another song, please do not release it, call reset() instead. After that, you have to reconfigure all information for this instance and prepare() and start() it again. release() method is called only when you finish all things, and you cannot do anything after then.

To repeat playing one song many times, please use setLooping(boolean) method defined for an instance of Media Player. Otherwise, there is another solution which is very useful and flexible. It is using Listeners. When an error occurs while playing a song, or completes playing a song, system will notify for your application if it registered some listeners before.

 

Audio Player Sequence Diagram - Using Listener

Audio Player Sequence Diagram – Using Listener

 

When a listener is invoked, you can easily do some useful tasks for current circumstance. Eg: next to another song after completing playing a song, or reset and try to replay after an error occurring. Then your application will become very reliable and flexible.

Tags: , , , , ,

No comments yet.

Leave a Reply