Display all music on SD card

I am using the code on this page:

http://z4android.blogspot.com/2011/06/displaying-list-of-music-files-stored.html

The code works, but not very well. When I try to scroll down, ListView keeps repeating the songs in the list.

I was looking for alternative code, but I could not find it.

Thanks for any help.

+7
android listview sd-card music
source share
4 answers

I'm not quite sure what exactly causes the problems you are talking about, 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(); } 
+10
source share

Andreas's answer is the right way to do this, but this code does not get the absolute path to the file. This forces mMediaPlayer.prepare(); throw an IOException: Prepare failed. status=0x1 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 do not 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(); } 

Be sure to use:

 playSong(mAudioPath[arg2]); 

instead:

 playSong(mMusicList[arg2]); 

in the ListView OnItemClickListener list.

To get only the name of the track (looks more elegant than the whole filename with the extension), use:

 `MediaStore.Audio.Media.TITLE` 

instead:

 `MediaStore.Audio.Media.DISPLAY_NAME` 
+11
source share

There are super buggies in the code.

getView makes no sense. What does gc () do there? What happens to all calls to moveToPosition ().

Here the code is 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; } 
-one
source share

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; } 
-one
source share

All Articles