Simple Android mp3 media player example

Here i want to write about my first experience in Android Dev.
This application just play embedded in application mp3 file. That’s all.
Below you can download the source code.
In first step i create a simple Android UI. Below you can see this:

In my simple mp3 media player i have few string constans, like “PLAY”, “PAUSE”, etc. The best practice is place this constats in special part of project. That part call values. In this part i Eclipse create for me strings.xml file and change it. Bellow you can see strings.xml content:

It’s very simple and useful practice.

Next i make changes in Activity file (Mp3player.java). Bellow i give you some comments about it:

Download it from github

32 thoughts on “Simple Android mp3 media player example

  • By Dominick Balistreri - Reply

    Hi..

    Thank you so much for the tutorial on the media player..
    Unlike some other tutorials, it worked perfectly right away and I learned quite a bit from it, so thank you again.

    I wanted to know if you could help me get the “SEEKBAR” to follow the position of the song as it plays.

    Since, I’m new to Android and java, I’m still having some trouble getting my arms around the big picture.

    Thank you again,

    Nick

  • By Dominick Balistreri - Reply

    Hi again,

    Below is my source code. I pretty much just changed a couple of small things from your example above. However, the seekbar doesn’t follow the music.

    Any ideas as to what I’m doing wrong ???

    // beginning my code

    package com.dt.player.one;

    import android.app.Activity;
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.os.Handler;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.SeekBar;
    import android.widget.SeekBar.OnSeekBarChangeListener;

    public class dtMenu extends Activity {
    private Button buttonPlayStop;
    private MediaPlayer mPlayer;
    private SeekBar seekBar;

    private final Handler handler = new Handler();

    // Here i override onCreate method.
    //
    // setContentView() method set the layout that you will see then
    // the application will starts
    //

    @Override
    public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    initViews();
    }

    // This method set the setOnClickListener and method for it (buttonClick())
    private void initViews() {
    buttonPlayStop = (Button) findViewById(R.id.ButtonPlayStop);
    buttonPlayStop.setOnClickListener(new OnClickListener()
    {public void onClick(View v) {buttonClick();}});

    mPlayer = MediaPlayer.create(this, R.raw.sqeefoot);

    seekBar = (SeekBar) findViewById(R.id.SeekBar01);
    seekBar.setMax(mPlayer.getDuration());

    seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

    public void onStopTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub
    }

    public void onStartTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub
    }

    public void onProgressChanged(SeekBar seekBar, int progress,
    boolean fromUser) {
    seekChange(seekBar, progress, fromUser);

    }
    });
    }

    // This makes the seekbar follow the music
    public void startPlayProgressUpdater() {
    seekBar.setProgress(mPlayer.getCurrentPosition());

    if (mPlayer.isPlaying()) {
    Runnable notification = new Runnable() {
    public void run() {
    startPlayProgressUpdater();
    }
    };
    handler.postDelayed(notification,1000);
    }else{
    mPlayer.pause();
    buttonPlayStop.setText(getString(R.string.play_str));
    seekBar.setProgress(0);
    }
    }

    // This is event handler thumb moving event
    private void seekChange(SeekBar seekBar, int progress,
    boolean fromUser){
    mPlayer.seekTo(progress);
    }

    // This is event handler for buttonClick event
    private void buttonClick(){
    if (buttonPlayStop.getText() == getString(R.string.play_str)) {
    buttonPlayStop.setText(getString(R.string.pause_str));
    try{
    mPlayer.start();
    }catch (IllegalStateException e) {
    mPlayer.pause();
    }
    }else {
    buttonPlayStop.setText(getString(R.string.play_str));
    mPlayer.pause();
    }
    }
    }

  • By Igor - Reply

    Hi, Dominick Balistreri.
    Thanks for your comment.
    I just made some source code updates. Please, look it. It must helps you

  • By Krishna - Reply

    Thanks for writing this tutorial… It is really helpful..
    Thanks again

  • By Alejandro - Reply

    This was a great help, thank you for your effort.

  • By shrenik - Reply

    hi Dominick Balistreri
    I copied your program. Its working but seek bar doesn’t synchronize with the song. please publish the solution for that.

  • By wos - Reply

    sip… trimakasih banyak untuk demo projeknya..

  • By Ishaq - Reply

    Thanks, I really find this very usefull.

  • By gokul - Reply

    Thanks a lot. I am using this. Its very help full

  • By Iram - Reply

    it ws gud n simple example for trying out media player example.
    howevr ders a simple prb in d app…..dat is whn user returns back to d app…he is able to play d song again …which leads to same media file playing more den one time in background….and dat should not happen!
    so to overcome dis prb u jst need to make a simple chng in your androidManifest file within tag set android:launchMode=”singleTask”
    thnx keep posting :)

  • By Slavi - Reply

    Hi,

    thanks for the example & download code!

  • By Fajr - Reply

    Thx for the code. i really need this for my project.
    in this code

    seekBar.setProgress(0);

    i think it means that every time buttonPLayStop click to pause the progress will be default to the start again, So how to make the progress bar always update every second the music pause. I really apreciate that thx, sorry my english was really bad :D

  • By JackLampro - Reply

    Thanks for your source code.

  • By JackLampro - Reply

    hi admin,
    i have used your code, but i configurated SDK 4 and android 4.0 and i happend some problem:

    ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1573
    can you help ??

  • By JackLampro - Reply

    *SDK 14

  • By toto - Reply

    thank you very much for sharing this simple mp3 player.
    This is very help for me :)

  • By ADS - Reply

    DFGD

  • By SivaKumarK - Reply

    Thanks for your code…………will u plz give me the code for asking a password before the call came

  • By SivaKumarK - Reply

    I have a good idea,but to implement that i want the above code….so plz help me….Is it possible to access only the games not any other applicaion within our application

  • By Franklin DN - Reply

    That is great!
    Thank a lot Igor!

  • By umesh - Reply

    Thanks Dear for this tutorial.

  • By Fra - Reply

    Hi, very useful tutorial.

    I was wondering, how to implement something like the seek bar to control the volume?

  • By kimonas - Reply

    thanx you.. Very helpful..!!!

  • By Dr.jacky - Reply

    hi, its play even i push back button!
    how solve it?!

  • By dilip - Reply

    thanks this code helpful great

  • By Samir - Reply

    Just simple and useful… helped me in completing my app…
    Thanks for the post

  • By sajjad - Reply

    bro from where it will get music files?

  • By Igor Khrupin - Reply

    Hi @sajjad,

    The music file located in the raw recourse dir. Here is link from github https://github.com/ihrupin/samples/blob/master/android/Mp3_Player/res/raw/testsong_20_sec.mp3

  • By zia - Reply

    I am using code for android version 4.4.2 and API 19 , after using the code clicking PLAY nothing happens.
    Could you please help me.

  • By abdul - Reply

    hi, this work fine thanks for the tutorial can i show the lyrics of the song as audio plays..
    please help me with this ploblem..

    • By Igor Khrupin - Reply

      Hi Abdul,

      You can do it if your server api support this functionality. You unable to get lyrics from audio file in runtime using Android SDK. Correct me please if you know doe feature for it.

  • By Navid - Reply

    thanks for the nice code, it works perfectly

Leave a Reply

Your email address will not be published. Required fields are marked *


− one = 8

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">