I'm using code form this page:
http://z4android.blogspot.com/2011/06/displaying-list-of-music-files-stored.html
The code is working, but not soo good. When I'm trying to scroll down, the ListView keeps repeating the songs in the list.
I have been looking for some alternative code, but I have not found any.
Thanks for any help.
I'm not entirely sure exactly what causes the problems you mention, but try this code.
private MediaPlayer mMediaPlayer;
private String[] mMusicList;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMediaPlayer = new MediaPlayer();
ListView mListView = (ListView) findViewById(R.id.listView1);
mMusicList = getMusic();
ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mMusicList);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
try {
playSong(mMusicList[arg2]);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private String[] getMusic() {
final Cursor mCursor = managedQuery(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Audio.Media.DISPLAY_NAME }, null, null,
"LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");
int count = mCursor.getCount();
String[] songs = new String[count];
int i = 0;
if (mCursor.moveToFirst()) {
do {
songs[i] = mCursor.getString(0);
i++;
} while (mCursor.moveToNext());
}
mCursor.close();
return songs;
}
private void playSong(String path) throws IllegalArgumentException,
IllegalStateException, IOException {
String extStorageDirectory = Environment.getExternalStorageDirectory()
.toString();
path = extStorageDirectory + File.separator + path;
mMediaPlayer.reset();
mMediaPlayer.setDataSource(path);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
Andreas answer is the right way to do this but that code does not get the absolute file path.
This causes the mMediaPlayer.prepare();
to throw IOException: Prepare failed. status=0x1
.
Here is the code to get the file path along with the file name:
private String[] mAudioPath;
private MediaPlayer mMediaPlayer;
private String[] mMusicList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMediaPlayer = new MediaPlayer();
ListView mListView = (ListView) findViewById(R.id.listView1);
mMusicList = getAudioList();
ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mMusicList);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
try {
playSong(mAudioPath[arg2]);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private String[] getAudioList() {
final Cursor mCursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DATA }, null, null,
"LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");
int count = mCursor.getCount();
String[] songs = new String[count];
String[] mAudioPath = new String[count];
int i = 0;
if (mCursor.moveToFirst()) {
do {
songs[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
mAudioPath[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
i++;
} while (mCursor.moveToNext());
}
mCursor.close();
return songs;
}
Now that we have the absolute path, we don't need to get the path again. So:
private void playSong(String path) throws IllegalArgumentException,
IllegalStateException, IOException {
Log.d("ringtone", "playSong :: " + path);
mMediaPlayer.reset();
mMediaPlayer.setDataSource(path);
//mMediaPlayer.setLooping(true);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
Make sure to use:
playSong(mAudioPath[arg2]);
instead of:
playSong(mMusicList[arg2]);
in the listview OnItemClickListener.
To get only the title of the track (Looks more elegant than the whole file name with extension) use:
`MediaStore.Audio.Media.TITLE`
instead of:
`MediaStore.Audio.Media.DISPLAY_NAME`
The code there is super-buggy.
The getView
doesn't make any sense. What is that gc() doing there?! What's with the all moveToPosition() calls.
Here's the code rewritten
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = null;
String id = null;
if (convertView == null) {
tv = new TextView(getApplicationContext());
} else
tv = (TextView) convertView;
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
id = musiccursor.getString(music_column_index);
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
id += " Size(KB): " + musiccursor.getString(music_column_index);
tv.setText(id);
return tv;
}
try this code
public View getView(int position, View convertView, ViewGroup parent) {
System.gc();
String id = null;
TextView tv;
if (convertView == null) {
tv = new TextView(mContext.getApplicationContext());
} else{
tv = (TextView) convertView;
}
musiccursor.moveToPosition(position);
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
id = musiccursor.getString(music_column_index);
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
id += " Size(KB):" + musiccursor.getString(music_column_index);
tv.setText(id);
return tv;
}
来源:https://stackoverflow.com/questions/8994625/display-all-music-on-sd-card