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 handled = true
} }
if (uri != null && uri.toString().isNotEmpty()) { if (uri != null && uri.toString().isNotEmpty()) {
MusicPlayerRemote.playFromUri(uri) MusicPlayerRemote.playFromUri(this@MainActivity, uri)
handled = true handled = true
} else if (MediaStore.Audio.Playlists.CONTENT_TYPE == mimeType) { } else if (MediaStore.Audio.Playlists.CONTENT_TYPE == mimeType) {
val id = parseLongFromIntent(intent, "playlistId", "playlist") val id = parseLongFromIntent(intent, "playlistId", "playlist")

View file

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