For one of my recent project, I need to list down all the media files (audio files to be specific) available in sd-card. First I was trying to scan the sd-card and filtering out only the audio files so on. My code got so messy.
Later via this question on StackOverflow I figured out my way into the code.
First I did a list view as below. List of Audio files and relevant details are queried. MediaStore.Audio.Media.DATA< can be given to MediaPlayer to play this audio.
import java.util.ArrayList; import java.util.List; import android.app.ListActivity; import android.content.Intent; import android.database.Cursor; import android.media.MediaPlayer; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class MediaPlayerNewListActivity extends ListActivity { public static List songs = new ArrayList(); public static List dataSongs = new ArrayList(); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.main); final Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; final String[] cursor_cols = { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA, }; final String where = MediaStore.Audio.Media.IS_MUSIC + “=1”; final Cursor cursor = getContentResolver().query(uri, cursor_cols, where, null, null); //cursor.moveToNext(); while (cursor.moveToNext()) { String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)); String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM)); String track = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)); String data = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)); doSomethingHere(artist, album, track, data); } setListAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1, songs)); getListView().setTextFilterEnabled(true); ListView lv = getListView(); lv.setTextFilterEnabled(true); lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { Intent mediaPlayer = new Intent(MediaPlayerNewListActivity.this, MediaPlayerActivity.class); mediaPlayer.putExtra(“index”, String.valueOf(arg2)); startActivity(mediaPlayer); Toast.makeText(MediaPlayerNewListActivity.this, String.valueOf(arg2), 2000).show(); } }); } private void doSomethingHere(String artist, String album, String track, String data) { songs.add(“n Artist : “+artist+”n Album : “+album+”n Track : “+track); dataSongs.add(data); } }
Then I created another Activity to Play the selected Audio File in the media player. Added extra feature such as navigation (Next and previous)
import java.io.IOException; import android.app.Activity; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MediaPlayerActivity extends Activity implements OnClickListener{ MediaPlayer mp; int index; private boolean isPasue = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.media_player); mp = new MediaPlayer(); Bundle extras = getIntent().getExtras(); index = Integer.valueOf(extras.getString("index")); //Toast.makeText(MediaPlayerActivity.this, data, 2000).show(); TextView info = (TextView)findViewById(R.id.textViewInfo); info.setText((CharSequence) MediaPlayerNewListActivity.songs.get(index)); Button btnStartMusic = (Button)findViewById(R.id.buttonStart); btnStartMusic.setOnClickListener(this); Button prev = (Button)findViewById(R.id.buttonPrev); prev.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(index > 0){ index--; mp.release(); TextView info = (TextView)findViewById(R.id.textViewInfo); info.setText((CharSequence) MediaPlayerNewListActivity.songs.get(index)); startMusic(); } } }); Button next = (Button)findViewById(R.id.buttonNext); next.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(index == MediaPlayerNewListActivity.songs.size()-1){ } else{ index++; mp.release(); TextView info = (TextView)findViewById(R.id.textViewInfo); info.setText((CharSequence) MediaPlayerNewListActivity.songs.get(index)); startMusic(); } } }); } private void startMusic() { Button btnStartMusic = (Button)findViewById(R.id.buttonStart); btnStartMusic.setText("Pause"); mp = new MediaPlayer(); isPasue = false; try { mp.setDataSource(String.valueOf(MediaPlayerNewListActivity.dataSongs.get(index))); mp.prepare(); mp.start(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } mp.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { if(index == MediaPlayerNewListActivity.songs.size()-1){ } else{ index++; mp.release(); TextView info = (TextView)findViewById(R.id.textViewInfo); info.setText((CharSequence) MediaPlayerNewListActivity.songs.get(index)); startMusic(); } } }); } @Override public void onClick(View v) { if(mp.isPlaying()){ pauseMusic(); } else if(isPasue){ mp.start(); isPasue = false; Button btnStartMusic = (Button)findViewById(R.id.buttonStart); btnStartMusic.setText("Pause"); } else{ startMusic(); } } private void pauseMusic() { Button btnStartMusic = (Button)findViewById(R.id.buttonStart); btnStartMusic.setText("Play"); isPasue = true; mp.pause(); } }
You can check out some screen shots here
You can download the full source here and spice up the UI as you wish. Good luck.
Subscribe to our mailing list
Subscribe to our mailing list and receive a free udpates
Hanz says
it’s error,can you give complete source code to my email?
Mayu says
I’ll update the source code attachment here. BTW wot is the error you are getting? check the logcat and tell me.
Lets try to figure out the issue.
Pratap says
Hi Mayu,
can u Please provide the source code . There is no link found to download.
sreeshty says
can u gv me d whole code