Wyświetl całą muzykę na karcie SD
Używam kodu z tej strony:
Http://z4android.blogspot.com/2011/06/displaying-list-of-music-files-stored.html
Kod działa, ale nie tak dobrze. Gdy próbuję przewijać w dół, Widok listy powtarza utwory na liście.
Szukałem jakiegoś alternatywnego kodu, ale żadnego nie znalazłem.
Dzięki za pomoc.
4 answers
Nie jestem do końca pewien, co dokładnie powoduje problemy, o których wspominasz, ale spróbuj tego kodu.
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();
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-01-25 00:24:46
Andreas answer jest właściwym sposobem, aby to zrobić, ale ten kod nie otrzymuje bezwzględnej ścieżki do pliku.
Powoduje to, że mMediaPlayer.prepare();
rzuca IOException: Prepare failed. status=0x1
.
Oto kod do pobrania ścieżki pliku wraz z nazwą pliku:
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;
}
Teraz, kiedy mamy ścieżkę absolutną, nie musimy jej ponownie zdobywać. Więc:
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();
}
Upewnij się, że używasz:
playSong(mAudioPath[arg2]);
Zamiast:
playSong(mMusicList[arg2]);
W listview OnItemClickListener.
Aby uzyskać tylko tytuł utworu (wygląda bardziej elegancko niż cała nazwa pliku z rozszerzeniem) użyj:
`MediaStore.Audio.Media.TITLE`
Zamiast:
`MediaStore.Audio.Media.DISPLAY_NAME`
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-08 10:51:52
Kod jest super-buggy.
getView
nie ma sensu. Co tam robi ten gc ()?! O co chodzi z wywołaniami all moveToPosition ().
Oto przepisany kod
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;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-01-25 02:17:33
Wypróbuj ten kod
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;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-11 08:35:36