Wednesday, 10 July 2013

Android Audio File List With Play and Progress

Hello All,

     Today i am going to discuss how to get all the audio files available in android device and view in list with play button. This blog also contain how to play particular file with progress update.

To get the list of available audio file we need to call content resolver to get all the file. managedQuery() is now deprecated from api level 11. So we can also user CursorLoader class to fetch the list of audio files from sd card. There are also other way to achieve this.

// fetch data for audio files available in sd card
// Use this below api level 11

//  Cursor mCursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media._ID,

//    MediaStore.Audio.Media.ALBUM_ID, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.SIZE }, AudioColumns.IS_MUSIC + "!=0", null, "LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");

// User this above api level 11

  CursorLoader cursorLoader=new CursorLoader(AudioFileListActivity.this, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media._ID,

    MediaStore.Audio.Media.ALBUM_ID, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.SIZE }, AudioColumns.IS_MUSIC + "!=0", null, null);

Cursor mCursor=cursorLoader.loadInBackground();

After getting all list of audio files set adapter to list view. Make custom list view with play and multi-select facilities.
Now create new media player object.
private MediaPlayer mp = new MediaPlayer();
to start playing do some code as shown below.
mp.reset();
mp.setDataSource(AudioFileListActivity.this, Uri.parse(filePath));
mp.prepare();
mp.start();

To show progress on seek bar create one broadcast receiver and send broadcast the current progress through thread every second.
/**
  * BroadCastReceiver for showing progress of audio file
  * 
  */
 BroadcastReceiver mReceiver = new BroadcastReceiver() {

  @Override
  public void onReceive(Context context, Intent intent) {

   if (intent.getExtras() != null) {
    int currentProgress = intent.getIntExtra("play_position", 0);
    tvTimeReached.setText(mediaTime((long) currentProgress));
    sbPlayProgress.setProgress(currentProgress);
   }

  }
 };

/**
  * (non-Javadoc)
  * 
  * @see java.lang.Runnable#run()
  */
 @Override
 public void run() {

  int currentPosition = 0;
  int total = mp.getDuration();

  while (mp.isPlaying() && currentPosition < total) {
   currentPosition = mp.getCurrentPosition();

   // broadcast the current progress
   callBroadcast(currentPosition);
  }

 }

 /**
  * Call to BroadCast the audio files current position
  * 
  * @param currentPosition
  */
 private void callBroadcast(int currentPosition) {

  Intent intent = new Intent();
  intent.putExtra("play_position", currentPosition);
  intent.setAction(BROADCAST_CURRENT_POSITION);
  sendBroadcast(intent);

 }


This is the simple demo of getting available audio file in sd card and show in list with play and progress facility. You can download the full source code from here.

Cheers...

1 comment :

  1. Hi. Excellent code. I just have a question, what do i have to modify in order to get the play list from audio files i have in my web server? Thanks in advance.

    ReplyDelete