Media Store Content Provider to reterive audio files from sdcard

Following code demonstrates retrieving audio files from sdcard and displaying in listview. When user selects any of the listview item that audio file should be played.

To run this program pull some audio files to emulator using DDMS perspective.
Goto DDMS perspective Choose Devices tab -> choose emulator on File Explorer tab
choose /mnt/sdcard and pull audio files by clicking on Pull a file from device.

MainActivity.java

package com.example.mediastoremusic;

import java.io.IOException;

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.util.Log;
import android.view.Menu;
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.Toast;

public class MainActivity extends Activity {

	Cursor c;
	int index;
	int count;
	MediaPlayer mp;
	@Override

	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		String cols[] = {MediaStore.Audio.Media.DISPLAY_NAME,
				MediaStore.Audio.Media.DURATION,
				MediaStore.Audio.Media.DATA
		};
		c = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, cols,null,null,null);
		for (int i=0;i<c.getColumnCount();i++)
			Log.d("col_info", c.getColumnName(i));
		count = c.getCount();
		Toast.makeText(getApplicationContext(), count+"", 50).show();
		ListView lv = (ListView)findViewById(R.id.listView1);
		lv.setAdapter(new MusicAdapter(getApplicationContext()));
		mp = new MediaPlayer();
		lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				c.moveToPosition(arg2);
				String fn = c.getString(2);
				try
				{
					if (mp.isPlaying())
						mp.reset();
					mp.setDataSource(fn);
					mp.prepare();
					mp.start();

				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
	private class MusicAdapter extends BaseAdapter
	{
		Context music_context;

		private MusicAdapter(Context c)
		{
			music_context = c;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return count;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			TextView tv = new TextView(music_context.getApplicationContext());
			c.moveToPosition(position);
			String s = c.getString(0);
			s+= c.getString(1);
			tv.setText(s);
			return tv;
		}

	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
}

layout – activity_main.xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>
Advertisements

2 thoughts on “Media Store Content Provider to reterive audio files from sdcard

  1. May Error Occurs like : “Eclipse console shows:’Failed to push selection: Read-only file system”

    Just go to

    C:\Documents and Settings\\.android\avd

    take ‘properties’ of your avd folder (there is a folder for each of the avd’s)

    uncheck ‘Read only’ -> OK

    May Helpful to you all…

    Thanks

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