Fixed a crash when opening music file from a external file manager

This commit is contained in:
Prathamesh More 2022-04-14 23:36:30 +05:30
parent 986216e6ad
commit 2119e0c754
3 changed files with 7 additions and 32 deletions

View file

@ -179,7 +179,7 @@ class MainActivity : AbsCastActivity(), OnSharedPreferenceChangeListener {
handled = true
}
if (uri != null && uri.toString().isNotEmpty()) {
MusicPlayerRemote.playFromUri(uri)
MusicPlayerRemote.playFromUri(this@MainActivity, uri)
handled = true
} else if (MediaStore.Audio.Playlists.CONTENT_TYPE == mimeType) {
val id = parseLongFromIntent(intent, "playlistId", "playlist")

View file

@ -172,7 +172,7 @@ object MusicPlayerRemote : KoinComponent {
return cursor.getString(columnIndex)
}
} catch (e: Exception) {
println(e.message)
e.printStackTrace()
} finally {
cursor?.close()
}
@ -419,7 +419,7 @@ object MusicPlayerRemote : KoinComponent {
}
@JvmStatic
fun playFromUri(uri: Uri) {
fun playFromUri(context: Context, uri: Uri) {
if (musicService != null) {
var songs: List<Song>? = null
@ -431,10 +431,8 @@ object MusicPlayerRemote : KoinComponent {
} else if (uri.authority == "media") {
songId = uri.lastPathSegment
}
songs = if (songId != null) {
songRepository.songs(songId)
} else {
songRepository.songsIgnoreBlacklist(uri)
if (songId != null) {
songs = songRepository.songs(songId)
}
}
}
@ -447,7 +445,7 @@ object MusicPlayerRemote : KoinComponent {
)
}
if (songFile == null) {
val path = getFilePathFromUri(musicService!!, uri)
val path = getFilePathFromUri(context, uri)
if (path != null)
songFile = File(path)
}
@ -462,6 +460,7 @@ object MusicPlayerRemote : KoinComponent {
openQueue(songs, 0, true)
} else {
// TODO the file is not listed in the media store
context.showToast(R.string.unplayable_file)
println("The file is not listed in the media store")
}
}

View file

@ -52,8 +52,6 @@ interface SongRepository {
fun song(cursor: Cursor?): Song
fun song(songId: Long): Song
fun songsIgnoreBlacklist(uri: Uri): List<Song>
}
class RealSongRepository(private val context: Context) : SongRepository {
@ -127,28 +125,6 @@ class RealSongRepository(private val context: Context) : SongRepository {
)
}
override fun songsIgnoreBlacklist(uri: Uri): List<Song> {
var filePath = ""
context.contentResolver.query(
uri,
arrayOf(AudioColumns.DATA),
null,
null,
null
).use { cursor ->
if (cursor != null) {
if (cursor.count != 0) {
cursor.moveToFirst()
filePath = cursor.getString(AudioColumns.DATA)
println("File Path: $filePath")
}
}
}
return songsByFilePath(
filePath, true
)
}
private fun getSongFromCursorImpl(
cursor: Cursor
): Song {