Android Development Tools

Android Devlopment Tools

Advertisements

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>