title: Android: How to play audio file from sd card
body: <p>Can anyone help me to solve this issue. Below is my code which runs properly, but i want to play music from sd card's folder like /sdcard/Music/song.mp3.</p>
  
  <pre><code>public class MainActivity extends Activity implements View.OnClickListener,SeekBar.OnSeekBarChangeListener {
  
      private MediaPlayer mediaPlayer;
      public TextView songName, duration,tvTotalTime;
      private double timeElapsed = 0, finalTime = 0;
      private int forwardTime = 2000, backwardTime = 2000;
      private Handler durationHandler = new Handler();
      private SeekBar seekbar;
      private ImageButton imgPlay,imgPause;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          getid();
          initializeViews();
          setListner();
      }
  
      private void setListner() {
          imgPlay.setOnClickListener(this);
          imgPause.setOnClickListener(this);
          seekbar.setOnSeekBarChangeListener(this);
      }
  
      private void getid() {
  
          songName = (TextView) findViewById(R.id.songName);
          tvTotalTime =(TextView)findViewById(R.id.tvTotalTime);
          mediaPlayer = MediaPlayer.create(this, R.raw.dheere);
          duration = (TextView) findViewById(R.id.duration);
          seekbar = (SeekBar) findViewById(R.id.seekBar);
          imgPlay =(ImageButton)findViewById(R.id.imgPlay);
          imgPause =(ImageButton)findViewById(R.id.imgPause);
      }
  
      public void initializeViews(){
  
          finalTime = mediaPlayer.getDuration();
          songName.setText("Sample_Song.mp3");
          seekbar.setMax((int) finalTime);
          seekbar.setClickable(true);
  
          }
  
  
  
      //handler to change seekBarTime
          private Runnable updateSeekBarTime = new Runnable() {
          public void run() {
              //get current position
              timeElapsed = mediaPlayer.getCurrentPosition();
              //set seekbar progress
              seekbar.setProgress((int) timeElapsed);
  
  
             tvTotalTime.setText(String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes((long) finalTime), TimeUnit.MILLISECONDS.toSeconds((long) finalTime)));
  
              //set time remaing
              double timeRemaining = finalTime - timeElapsed;
              duration.setText(String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining), TimeUnit.MILLISECONDS.toSeconds((long) timeRemaining) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining))));
  
              //repeat yourself that again in 100 miliseconds
              durationHandler.postDelayed(this, 100);
          }
      };
  
  
      @Override
          public void onClick(View v) {
  
          try {
              switch (v.getId())
              {
                  case R.id.imgPlay:
  
                      mediaPlayer.start();
                      timeElapsed = mediaPlayer.getCurrentPosition();
                      seekbar.setProgress((int) timeElapsed);
                      durationHandler.postDelayed(updateSeekBarTime, 100);
  
                      break;
  
                  case R.id.imgPause:
  
                      mediaPlayer.pause();
  
                      break;
  
              }
          } catch (IllegalStateException e) {
              e.printStackTrace();
          }
      }
  
      @Override
      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
  
          if (fromUser) {
              mediaPlayer.seekTo(progress);
          }
  
      }
  
      @Override
      public void onStartTrackingTouch(SeekBar seekBar) {
  
      }
  
      @Override
      public void onStopTrackingTouch(SeekBar seekBar) {
  
      }
  }
  </code></pre>