MediaStore Content Provider

Following code demonstrates retrieving audio files stored in sdcard and displaying list in listview. When user double clicks any of the song that music file will be played.


public class Test1Activity extends Activity 
{
/** Called when the activity is first created. */
ListView musiclist;
Cursor musiccursor;
int music_column_index;
int count;
MediaPlayer mMediaPlayer;

@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() {
	@Override
	public void onItemClick(AdapterView parent, View v, int position,
				long id) {
	// TODO Auto-generated method stub
	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;
		}
	}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s