Skip to content

Commit c9c7337

Browse files
authored
Fixed Bug in Playlist Feature.
App crashes on some google devices while checking and updating playlist audio files.
1 parent 714a7c4 commit c9c7337

File tree

1 file changed

+13
-5
lines changed
  • app/src/main/java/com/harshRajpurohit/musicPlayer

1 file changed

+13
-5
lines changed

app/src/main/java/com/harshRajpurohit/musicPlayer/Music.kt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Playlist{
2020
lateinit var createdBy: String
2121
lateinit var createdOn: String
2222
}
23+
2324
class MusicPlaylist{
2425
var ref: ArrayList<Playlist> = ArrayList()
2526
}
@@ -30,11 +31,13 @@ fun formatDuration(duration: Long):String{
3031
minutes*TimeUnit.SECONDS.convert(1, TimeUnit.MINUTES))
3132
return String.format("%02d:%02d", minutes, seconds)
3233
}
34+
3335
fun getImgArt(path: String): ByteArray? {
3436
val retriever = MediaMetadataRetriever()
3537
retriever.setDataSource(path)
3638
return retriever.embeddedPicture
3739
}
40+
3841
fun setSongPosition(increment: Boolean){
3942
if(!PlayerActivity.repeat){
4043
if(increment)
@@ -49,6 +52,7 @@ fun setSongPosition(increment: Boolean){
4952
}
5053
}
5154
}
55+
5256
fun exitApplication(){
5357
if(PlayerActivity.musicService != null){
5458
PlayerActivity.musicService!!.audioManager.abandonAudioFocus(PlayerActivity.musicService)
@@ -68,13 +72,17 @@ fun favouriteChecker(id: String): Int{
6872
}
6973
return -1
7074
}
75+
7176
fun checkPlaylist(playlist: ArrayList<Music>): ArrayList<Music>{
77+
val indicesToRemove = mutableListOf<Int>()
78+
7279
playlist.forEachIndexed { index, music ->
73-
val file = File(music.path)
74-
if(!file.exists())
75-
playlist.removeAt(index)
80+
if (!File(music.path).exists()) indicesToRemove.add(index)
7681
}
77-
return playlist
82+
83+
indicesToRemove.sortDescending()
84+
indicesToRemove.forEach { index -> playlist.removeAt(index) }
85+
return playlist
7886
}
7987

8088
fun setDialogBtnBackground(context: Context, dialog: AlertDialog){
@@ -100,4 +108,4 @@ fun checkPlaylist(playlist: ArrayList<Music>): ArrayList<Music>{
100108
val color = newImg.getPixel(0, 0)
101109
newImg.recycle()
102110
return color
103-
}
111+
}

0 commit comments

Comments
 (0)