如何播放列表中的歌曲。我能够获得歌曲,但我无法播放任何内容
问题描述:
以下是我的代码,用于从设备获取歌曲。但我无法播放任何内容。如何播放列表中的歌曲。我能够获得歌曲,但我无法播放任何内容
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();
}
答
我认为你必须设置你的光标路径的数据源是指你的歌path.And如果使用的是内容提供商,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();
它会帮助你...
+0
感谢@ER维沙尔Senjaliya –
+0
,如果它是你的作品,然后接受作为一个答案...谢谢 –
你需要阅读'MediaPlayer' API文档 – pskink
改善格式化 – Vickyexpert