Skip to content

Instantly share code, notes, and snippets.

@Redman1037
Created December 21, 2017 07:48
Show Gist options
  • Save Redman1037/4878fc922141fc54b367f2ba0b29a2e8 to your computer and use it in GitHub Desktop.
Save Redman1037/4878fc922141fc54b367f2ba0b29a2e8 to your computer and use it in GitHub Desktop.
Pick Audio Files from android with duration
//music file pojo
public class MusicFile {
String fileName;
String filePath;
String album;
String artist;
Long duration;
public Long getDuration() {
return duration;
}
public void setDuration(Long duration) {
this.duration = duration;
}
public String getAlbum() {
return album;
}
public void setAlbum(String album) {
this.album = album;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
}
// Ask for Write storage permission and call this function
public List<MusicFile> getAllAudioFromDevice(final Context context) {
final List<MusicFile> tempAudioList = new ArrayList<>();
// Uri uri1 = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.Audio.AudioColumns.DATA, MediaStore.Audio.AudioColumns.ALBUM,
MediaStore.Audio.ArtistColumns.ARTIST,MediaStore.Audio.AudioColumns.DURATION};
// Cursor c = context.getContentResolver().query(uri, projection, MediaStore.Audio.Media.DATA + " like ? ", new String[]{"%yourFolderName%"}, null);
Cursor c = context.getContentResolver().query(uri,
projection,
null,
null,
null);
if (c != null) {
while (c.moveToNext()) {
MusicFile audioModel = new MusicFile();
String path = c.getString(0);
String album = c.getString(1);
String artist = c.getString(2);
Long duration=c.getLong(3);
String name = path.substring(path.lastIndexOf("/") + 1);
audioModel.setFileName(name);
audioModel.setAlbum(album);
audioModel.setArtist(artist);
audioModel.setFilePath(path);
audioModel.setDuration(duration);
// Log.e("Name :" + name, " Album :" + album);
// Log.e("Path :" + path, " Artist :" + artist);
tempAudioList.add(audioModel);
}
c.close();
}
return tempAudioList;
}
//converting long duration to hhmmss
public static String convertMillieToHMmSsNew(long millie) {
long seconds = (millie / 1000);
long second = seconds % 60;
long minute = (seconds / 60) % 60;
long hour = (seconds / (60 * 60)) % 24;
String result = "";
if (hour > 0) {
return String.format("%02d:%02d:%02d", hour, minute, second);
}
else {
return String.format("%02d:%02d" , minute, second);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment