CustomAdapter->
class CustomAdapteSongList(
val context: Context,
val lifecycle: LifecycleOwner,
val layout: Int,
) : RecyclerView.Adapter<CustomAdapteSongList.ViewHolder>() {
private var ListSongs: List<Song> = emptyList()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val viewlayout = layoutInflater.inflate(layout, parent, false)
return ViewHolder(viewlayout, context)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val itemsong = ListSongs[position]
holder.bind(itemsong, lifecycle)
}
override fun getItemCount(): Int {
return ListSongs.size
}
internal fun setSongs(songs: List<Song>) {
this.ListSongs = songs
notifyDataSetChanged()
}
class ViewHolder(viewlayout: View, val context: Context) : RecyclerView.ViewHolder(viewlayout) {
fun bind(dataitem: Song, lifecycle: LifecycleOwner) {
// itemview es el item de diseño
// al que hay que poner los datos del objeto dataItem
itemView.findViewById<TextView>(R.id.title_song).text = dataitem.title
itemView.findViewById<TextView>(R.id.artist_name).text = dataitem.artistName
var song_artWork = Uri.parse("content://media/external/audio/albumart")
song_artWork = ContentUris.withAppendedId(song_artWork, dataitem.id)
val file = File(song_artWork.getPath())
Picasso.get()
.load(file)
.error(R.drawable.ic_standard_image_song)
.into(itemView.findViewById<ImageView>(R.id.iv_image_song))
itemView.tag = dataitem
}
}
}
LocalSongsProvider->
object LocalSongsProvider {
private val allDeviceSongs = ArrayList<Song>()
val projection = arrayOf(
MediaStore.Audio.AudioColumns._ID,
MediaStore.Audio.AudioColumns.TITLE,
MediaStore.Audio.AudioColumns.TRACK,
MediaStore.Audio.AudioColumns.YEAR,
MediaStore.Audio.AudioColumns.DURATION,
MediaStore.Audio.AudioColumns.ALBUM,
MediaStore.Audio.AudioColumns.ARTIST_ID,
MediaStore.Audio.AudioColumns.ARTIST,
)
private fun getSongs(cursor: Cursor?): List<Song> {
if (cursor != null) {
// Cache column indices.
val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID)
val titleColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)
val trackColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TRACK)
val yearColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.YEAR)
val durationColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)
val albumColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM)
val artistIdColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST_ID)
val artistColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)
while (cursor.moveToNext()) {
// Get values of columns for a given video.
val id = cursor.getLong(idColumn)
val title = cursor.getString(titleColumn)
val track = cursor.getInt(trackColumn)
val year = cursor.getInt(yearColumn)
val duration = cursor.getInt(durationColumn)
val album = cursor.getString(albumColumn)
val artistId = cursor.getInt(artistIdColumn)
val artist = cursor.getString(artistColumn)
val contentUri: Uri = ContentUris.withAppendedId(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
id
)
// Stores column values and the contentUri in a local object
// that represents the media file.
allDeviceSongs.add(Song(id, contentUri, title, track, year, duration, album, artistId, artist))
}
}
return allDeviceSongs.reversed()
}
fun getAllDeviceSongs(context: Context): List<Song> {
val cursor = BuildSongCursor(context)
Log.d("lista", getSongs(cursor).toString())
return getSongs(cursor)
}
private fun BuildSongCursor(context: Context): Cursor? {
try {
return context.contentResolver.query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection,
null,
null,
null)
} catch (e: SecurityException) {
return null
}
}
}