显示在SD卡上所有的音乐 [英] Display all music on SD card
本文介绍了显示在SD卡上所有的音乐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用code组成本页面:
I'm using code form this page:
<一个href="http://z4android.blogspot.com/2011/06/displaying-list-of-music-files-stored.html">http://z4android.blogspot.com/2011/06/displaying-list-of-music-files-stored.html
在code正在工作,但不洙不错。 当我试图向下滚动,ListView中不断重复的歌曲列表。
The code is working, but not soo good. When I'm trying to scroll down, the ListView keeps repeating the songs in the list.
我一直在寻找一些替代code,但我还没有发现任何。
I have been looking for some alternative code, but I have not found any.
感谢您的帮助。
推荐答案
我不完全知道到底是什么原因导致你提到的问题,但试试这个code。
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();
}
这篇关于显示在SD卡上所有的音乐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文