Reputation: 421
I have 5 folders and every folder contains 5 audio files. When Someone clicks on a folder, The program should start playing all the audio files from that folder. I am new to android, So i have a very little Idea about it. I tried doing it, but it shows all the audio files together. I want it to show folder wise.
fun abs(){
var songsURI = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
var selection = MediaStore.Audio.Media.IS_MUSIC + "!=0"
val cursor = contentResolver.query(songsURI,null,selection,null,null)
if(cursor!= null)
{
if (cursor!!.moveToFirst()){
do
{
var songURL = cursor!!.getString(cursor!!.getColumnIndex(MediaStore.Audio.Media.DATA))
var songAuth =cursor!!.getString(cursor!!.getColumnIndex(MediaStore.Audio.Media.ARTIST))
var SongNAme = cursor!!.getString(cursor!!.getColumnIndex(MediaStore.Audio.Media.TITLE))
listSongs.add(Songinfo(songURL,songAuth,SongNAme))
}while (cursor!!.moveToNext())
}
cursor!!.close()
adapter = MySongAdapter(listSongs)
hello.adapter = adapter
}
}
Please Help
Upvotes: 0
Views: 442
Reputation: 11622
Below code will give you the list of songs in each directory, you should change the adapter to show the directory name and the song list.
data class SongInfo(var songURL: String, var songAuth: String, var songNAme: String)
data class DirInfo(var dir: String, var songInfo: ArrayList<SongInfo>)
private fun getAudioDirectories(): ArrayList<DirInfo> {
var result = ArrayList<DirInfo>()
val directories = LinkedHashMap<String, ArrayList<SongInfo>>()
val uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
val selection = MediaStore.Audio.Media.IS_MUSIC + "!=0"
val order = MediaStore.Audio.Media.DATE_MODIFIED + " DESC"
val cursor = getContentResolver().query(uri, null, selection, null, order)
cursor.let {
it.moveToFirst()
val pathIndex = it.getColumnIndex(MediaStore.Images.Media.DATA)
do {
val path = it.getString(pathIndex)
val file = File(path)
if (!file.exists()) {
continue
}
val fileDir = file.getParent()
var songURL = it.getString(it.getColumnIndex(MediaStore.Audio.Media.DATA))
var songAuth = it.getString(it.getColumnIndex(MediaStore.Audio.Media.ARTIST))
var songName = it.getString(it.getColumnIndex(MediaStore.Audio.Media.TITLE))
if (directories.containsKey(fileDir)) {
var songs = directories.getValue(fileDir);
var song = SongInfo(songURL, songAuth, songName)
songs.add(song)
directories.put(fileDir, songs)
} else {
var song = SongInfo(songURL, songAuth, songName)
var songs = ArrayList<SongInfo>()
songs.add(song)
directories.put(fileDir, songs)
}
} while (it.moveToNext())
for (dir in directories) {
var dirInfo: DirInfo = DirInfo(dir.key, dir.value);
result.add(dirInfo)
}
}
return result
}
Using your Adapter we can't show list of Album with songs list, you can use this or you can take any of these libs from Android-Arsenal
Upvotes: 1