user2401554
user2401554

Reputation: 121

how to display music files in listview in android

Hi i need to display array of music files in listview,and when i click that music file i need play that song,i tried using below code to display songs in listview,but it showing null pointer exception in adding array to textview line,but same code working for displaying images in listview,where i did mistake any one suggest me..

public class CustomListViewExample extends Activity {
    Integer[] text;
    public static ArrayList<Integer> list1 = new ArrayList<Integer>();

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.citylist);
        list1.add(R.raw.apple);
        list1.add(R.raw.intro_letter_report_card);
        list1.add(R.raw.intro_title_page_1);

        text = list1.toArray(new Integer[list1.size()]);

        ListView l1 = (ListView) findViewById(R.id.ListView01);
        l1.setAdapter(new MyCustomAdapter(text));
    }

    class MyCustomAdapter extends BaseAdapter {
        Integer[] data_image;

        MyCustomAdapter() {
            data_image = null;
        }

        MyCustomAdapter(Integer[] text) {
            data_image = text;
        }

        @Override
        public int getCount() {
            return data_image.length;
        }

        @Override
        public String getItem(int position) {
            return null;
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = getLayoutInflater();
            View row;

            row = inflater.inflate(R.layout.city_row_item, parent, false);

            TextView t1=(TextView)row.findViewById(R.id.textView1);

            t1.setText(""+data_image[position]);
            return (row);
        }
    }
}

Upvotes: 0

Views: 6645

Answers (3)

SAURABH_12
SAURABH_12

Reputation: 2300

Use following code to display audio file in ListView and clicking on any one of it, plays that song

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

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.audiolist_activity);
    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;
    }
}
@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    mMediaPlayer.stop();
}
}

Upvotes: 3

sandrstar
sandrstar

Reputation: 12643

For NPE, seems there's no textView1 view in R.layout.city_row_item.

Regarding showing list of music files and playing them, You could refer to this exemple.

Also, I believe You need to refer to this video from 2010 IO for creation of better adapters for ListViews (because in your code You don't use convertView item).

Upvotes: 0

Julian Mancera
Julian Mancera

Reputation: 304

Use sound pool to load the sounds into the memory first. This is how I'm doing it in my app Beat Shaker:

// Sound pool new instance
spool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
// Sound pool on load complete listener
spool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
    @Override
    public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
        Log.i("OnLoadCompleteListener","Sound "+sampleId+" loaded.");
        loaded = true;
    }
});       
// Load the sample IDs
soundId = new int[3];
soundId[0] = spool.load(this, R.raw.clave, 1);
soundId[1] = spool.load(this, R.raw.maracas, 1);
soundId[2] = spool.load(this, R.raw.crash, 1);

Then you can call a thread from the corresponding list selection listener that runs a function similar to this:

public void Sound1(){
    AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    float volume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    int streamID = -1;
    do{
        streamID = spool.play(soundId[0], volume, volume, 1, 0, 1f);
    } while(streamID==0);
};

Upvotes: 0

Related Questions