Skip to content

Instantly share code, notes, and snippets.

@bengongon97
Created August 7, 2020 08:08
Show Gist options
  • Save bengongon97/9c513259cf3616cf0606c47f5668d887 to your computer and use it in GitHub Desktop.
Save bengongon97/9c513259cf3616cf0606c47f5668d887 to your computer and use it in GitHub Desktop.
public List<HwAudioPlayItem> getLocalPlayList(Context context) {
List<HwAudioPlayItem> playItemList = new ArrayList<>();
Cursor cursor = null;
try {
ContentResolver contentResolver = context.getContentResolver();
if (contentResolver == null) {
return playItemList;
}
String selection = MediaStore.Audio.Media.IS_MUSIC + "!=0";
cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
null,
selection,
null,
MediaStore.Audio.Media.TITLE);
HwAudioPlayItem songItem;
if (cursor != null) {
if(!cursor.moveToNext()){
//there is no music, do sth
return playItemList; //return empty list for now
}
else{
while(cursor.moveToNext()) {
String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
if (new File(path).exists()) {
songItem = new HwAudioPlayItem();
songItem.setAudioTitle(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME)));
songItem.setAudioId(cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID)) + "");
songItem.setFilePath(path);
songItem.setOnline(0);
songItem.setIsOnline(0);
songItem.setDuration(cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)));
songItem.setSinger(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)));
playItemList.add(songItem);
}
}
}
}
else{
Toast.makeText(this, "We have a serious cursor problem here!", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Log.e("TAG,", "EXCEPTION", e);
} finally {
if (cursor != null) {
cursor.close();
}
}
Log.i("LOCALITEMSIZE", "getLocalPlayList: " + playItemList.size());
return playItemList;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment