Pages

Thursday, June 2, 2011

Displaying the List of music files Stored in SD card and playing music in the background

The foolowing example used to list all the music files stored in SDcard and you can play the music
in the backgroung by selecting the file from list.

main.xml
--------
<?xml version="1.0" encoding="utf-8"?>
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical" android:layout_width="fill_parent"
      android:layout_height="fill_parent">
      < ListView
            android:id="@+id/PhoneMusicList"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
</LinearLayout>

MusicActivity.java
-------------------
package sample.music;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class MusicActivity extends Activity {
      ListView musiclist;
      Cursor musiccursor;
      int music_column_index;
      int count;
      MediaPlayer mMediaPlayer;

      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            init_phone_music_grid();
      }

      private void init_phone_music_grid() {
            System.gc();
            String[] proj = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Video.Media.SIZE };
            musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
proj, null, null, null);
            count = musiccursor.getCount();
            musiclist = (ListView) findViewById(R.id.PhoneMusicList);
            musiclist.setAdapter(new MusicAdapter(getApplicationContext()));

            musiclist.setOnItemClickListener(musicgridlistener);
            mMediaPlayer = new MediaPlayer();
      }

      private OnItemClickListener musicgridlistener = new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position,
long id) {
                  System.gc();
                  music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
                  musiccursor.moveToPosition(position);
                  String filename = musiccursor.getString(music_column_index);

                  try {
                        if (mMediaPlayer.isPlaying()) {
                              mMediaPlayer.reset();
                        }
                        mMediaPlayer.setDataSource(filename);
                        mMediaPlayer.prepare();
                        mMediaPlayer.start();
                  } catch (Exception e) {

                  }
            }
      };

      public class MusicAdapter extends BaseAdapter {
            private Context mContext;

            public MusicAdapter(Context c) {
                  mContext = c;
            }

            public int getCount() {
                  return count;
            }

            public Object getItem(int position) {
                  return position;
            }

            public long getItemId(int position) {
                  return position;
            }

            public View getView(int position, View convertView, ViewGroup parent) {
                  System.gc();
                  TextView tv = new TextView(mContext.getApplicationContext());
                  String id = null;
                  if (convertView == null) {
                        music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
                        musiccursor.moveToPosition(position);
                        id = musiccursor.getString(music_column_index);
                        music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
                        musiccursor.moveToPosition(position);
                        id += " Size(KB):" + musiccursor.getString(music_column_index);
                        tv.setText(id);
                  } else
                        tv = (TextView) convertView;
                  return tv;
            }
      }
}

43 comments:

  1. very good .... thanks

    ReplyDelete
  2. I tried building a music app using this code but it keep on giving me error message. Any help

    ReplyDelete
  3. how to dispaly the Artist Name below song name

    ReplyDelete
  4. are there any permissions required in the manifest?

    ReplyDelete
  5. how to make it with the help of Recycler View

    ReplyDelete
  6. Nice Post.
    For best music samples and loops
    on Indian Flutes visit mangoloops

    ReplyDelete
  7. These ways are very simple and very much useful, as a beginner level these helped me a lot thanks fore sharing these kinds of useful

    and knowledgeable information.
    Mobile App Development Company
    Mobile App Development Company in India
    Mobile App Development Companies

    ReplyDelete
  8. It's not working for me,when I'm trying to open on my mobile, it shows unfortunately stopped. 😭😭😭😭

    ReplyDelete
  9. Thanks, worked well for me, specially the code for OnItemClick() method.

    ReplyDelete
  10. any permission required for it

    ReplyDelete
  11. MY APP DO NOT SHOW ANY ERROR. BUT ON STARTING IT STARTS AND CLOSED EMMIDIATELY

    ReplyDelete
  12. https://z4android.blogspot.com/2012/07/how-to-check-sdcard-free-space-in.html?showComment=1602054079394#c6749768584916159611

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

    ReplyDelete
  14. https://thesecondhandrose.blogspot.com/2013/09/fabulous-fashionistas-six-women-that.html?sc=1732459294585#c4992189367674483401 rJmKp7jAzl

    ReplyDelete

Popular Posts