以下是我从设备获取歌曲的代码。但我无法播放任何内容。
try {
Cursor cursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
if (cursor == null) {
// Query failed...
} else if (!cursor.moveToFirst()) {
// Nothing to query. There is no music on the device.
}else {
// add each song to mItems.
//these is the part that gets each item from the cursor and adds them to the list.
do {
int artistColumn = cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
// add each song to mItems.
int titleColumn = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
// add each song to mItems.
int albumColumn = cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM);
// add each song to mItems.
int albumArt = cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID);
// add each song to mItems.
int durationColumn = cursor.getColumnIndex(MediaStore.Audio.Media.DURATION);
// add each song to mItems.
int idColumn = cursor.getColumnIndex(MediaStore.Audio.Media._ID);
// add each song to mItems.
int filePathIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
int art = cursor.getColumnIndex(MediaStore.Audio.AlbumColumns.ALBUM_ART);
// these section adds the media info(MediaInfo.class) to the mediaList.
MediaFileInfo audio = new MediaFileInfo();
audio.setFileName(cursor.getString(titleColumn));
audio.setFilePath(cursor.getString(filePathIndex));
audio.setImgId(cursor.getInt(albumArt));
audio.setFileType(type);
audio.setImgId(cursor.getInt(art));
//am adding all the items to the list(medaiList).
mediaList.add(audio);
} while (cursor.moveToNext());
}
} catch (Exception e) {
e.printStackTrace();
}
答案 0 :(得分:0)
我认为您必须设置光标路径的数据源意味着您的歌曲路径。如果您使用的是内容提供商,URI查询,MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
。
然后你必须在歌曲路径光标中设置你返回的光标。像这样,
String path = cursor.getString(getColumnIndex(Audio.Media.DATA));
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(path);
mp.prepare();
mp.start();
它会帮助你......