Custom Media Player on Android


Finally this post is possible.Before going through the blog you have two ways to finish up this tutorial.
  1. just copy paste the code provided in the snippets n u r done
  2. Read and learn through out this post and try do something new.
  3. or if you looking for your problems , i hope u will get something from here.
Here i'm trying to create a custom media player with seekbars to control volume and control status of mp3 audio. Also i hv built a custom file_dialog/file_Browser to help select files from the Android phone.




Building the File Browser:
For this fileExplorer i have created a new Intent and then added a AlertDialog to it to display files n directories.

step 1:  Create Item object that stores file name and icon associated with it.
step 2:  Using FileNameFilter go through all the files in the system and fill the ArrayList of Items with the corresponding File names and Icons.
step 3: Use ArrayAdapter to set the view of list of files on the Dialog.
step 4: Using dialogBuilder.setAdapter() method define the click event listener
If a directory is selected  then refresh the dialog.
else if a file is selected then we return a string representing the Path to the media file we wanna play.

So now to exchange values between Intents follow this link to learn using-intents-to-pass-data-and-return-results-between-activities/
and yeah u need to download some file icon required for this one to work n prefer 64x64 sizes , well i googled it :)


Intent's layout xml file is here..


Building the Media Player:


This is the layout of the media player i will building here.
I named it the Mango Player .It has basic media player functions like file selector , play and stop button.
And further more i tried to put up a seekbar to show status and also to seek the song by dragging it which is the toughest part.
Another seekbar for volume control which is easiest part.



Step 1: File selection 
Hey this part is done through the file explore  which will return the full path of the song we want to play.
Browserbutton.setOnClickListner() will create a intent for fileExplore and hence that will return the string SongUrl.


Step 2: Setting up Volume seek bar 
For this we need AudioManager object to adjust volume of media.
audioManager.getStreamMaxVolume()  returns max media volume ,audioManager.getStreamVolume() returns current volume level  and audioManager.setStreamVolume()  to set stream volume.
Seekbar.setOnSeekListener() will handle the seek events and update the volume accordingly.

Step 3: Setting up Media player
Easy as pie ......no effort at all read the documentation at Android MediaPlayer.
mp.reset();
mp.setDataSource(songUrl);
mp.prepare();

that's it....now use mp.start() mp.pause() or mp.stop() according to your need.

Step 4: Setting up the Media control Seek Bar
Now the tough part.
As you can see mediaSeekbar has to updated at the same time music is being played so there is a clear requirement for some multiTHREADing stuff.
So i got one that will continuously update the mediaSeekbar status as long as song is being played.

Now what else is done in the actual code is just to make above things possible.

Here is the code and layout xml .






  • Playlist creation is left for you guys.. :) 
  • MultiFile selection capability can also be added..will look into it sooner or later.


hmm no thought's for next post...... but most probably something on GoogleMap API. 
So that's it for today.... hope you won't have any doubts regarding the code, if there is any you can write to me.






Comments

  1. Guys for the next post i will be dealing with a binary tree data structure + A way to create a image of the tree just created.

    where u can even color the nodes of the tree.

    A fully Functional Visual Representation of a Binary Tree.
    System requirement: Linux

    ReplyDelete
  2. I will continue with my Binary tree stuff in the next post within two weeks so stay tuned guys

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. @Praveen it's working dude it's the only thing that i targeted..
    may be u got it and hence deleted the post..

    ReplyDelete

Post a Comment