如何从我的列表视图中 SD 卡上的特殊文件夹加载歌曲并播放? [英] How load songs from special folder on sd card in my listview and play it?
本文介绍了如何从我的列表视图中 SD 卡上的特殊文件夹加载歌曲并播放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从我在 sd 卡上制作的自定义文件夹中加载歌曲并通过我的应用程序播放.
I want to load songs from my custom folder which I made on sd card and play it in via my App.
此文件夹的路径在:/sdcard/Music/MyFolder
the path of this folder is on: /sdcard/Music/MyFolder
现在我有一个代码可以加载位于 sc 卡上的所有音乐.此代码正在工作.但我不想要 sd 卡中的所有音乐,我只想要我文件夹 MyFolder 中的歌曲
Now I have a code which loads all music which is located on the sc card. This code is working. But I dont want all music from sd card, I just want the songs in my folder MyFolder
这是我现在拥有的代码:
Here's the code I have now:
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];
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;
}
private void playSong(String path) throws IllegalArgumentException,
IllegalStateException, IOException {
mMediaPlayer.reset();
mMediaPlayer.setDataSource(path);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
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();
}
}
});
}
有人知道怎么做吗?
推荐答案
在选择中使用 where 条件,像这样.
use where condition in selection, like this.
final Cursor mCursor = getContentResolver()
.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DATA }, MediaStore.Audio.Media.DATA
+ " LIKE '/storage/sdcard0/Music/MyFolder/%'", null, "LOWER("
+ MediaStore.Audio.Media.TITLE + ") ASC");
这篇关于如何从我的列表视图中 SD 卡上的特殊文件夹加载歌曲并播放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文