MusicDroid.java
package com.helloandroid.android.musicdroid;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
class Mp3Filter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".mp3"));
}
}
public class MusicDroid extends ListActivity {
private static final String MEDIA_PATH = new String("/mnt/sdcard/Songs");
private List songs = new ArrayList();
private MediaPlayer mp = new MediaPlayer();
@Override
public void onCreate(Bundle icicle) {
try {
super.onCreate(icicle);
setContentView(R.layout.songlist);
updateSongList();
} catch (NullPointerException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
}
public void updateSongList() {
File home = new File(MEDIA_PATH);
if (home.listFiles( new Mp3Filter()).length > 0) {
for (File file : home.listFiles( new Mp3Filter())) {
songs.add(file.getName());
}
ArrayAdapter songList = new ArrayAdapter(this,R.layout.song_item,songs);
setListAdapter(songList);
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
try {
mp.reset();
mp.setDataSource(MEDIA_PATH + songs.get(position));
mp.setOnPreparedListener((OnPreparedListener) this);
mp.prepare();
mp.start();
} catch(IOException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
}
}
5 comments:
hai, here you posted as when the user click the list item,the particular song will be played. I need to play all the songs continuously without users selection. could you help me
hi
i used this same code and i am able to see the songs list but when i click on any item it is not playing.Please share the xml files as well.
This tutorial is worthless for begineers without the source especially the xml layout files :)
Not bad... but a tutorial containing just plain source code without explanations/comments isn't a tutorial, or at least isn't a good tutorial. Just my opinion.
Anyway, thanks, because it helped me a bit to clarify things.
Step by step ANDROID TUTORIALS
VISIT
http://www.androidituts.blogspot.com
Post a Comment