fix: crash when starting audio player

This commit is contained in:
Bnyro 2024-11-18 16:22:50 +01:00
parent 131369470f
commit c56986a9ba
3 changed files with 22 additions and 17 deletions

View File

@ -3,9 +3,11 @@ package com.github.libretube.helpers
import android.content.Context import android.content.Context
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.Color import android.graphics.Color
import android.graphics.drawable.Drawable
import android.net.Uri
import android.widget.ImageView import android.widget.ImageView
import androidx.core.graphics.drawable.toBitmap
import androidx.core.graphics.drawable.toBitmapOrNull import androidx.core.graphics.drawable.toBitmapOrNull
import androidx.core.net.toUri
import coil.ImageLoader import coil.ImageLoader
import coil.disk.DiskCache import coil.disk.DiskCache
import coil.request.CachePolicy import coil.request.CachePolicy
@ -87,17 +89,12 @@ object ImageHelper {
if (DataSaverMode.isEnabled(target.context) || url.isNullOrEmpty()) return if (DataSaverMode.isEnabled(target.context) || url.isNullOrEmpty()) return
val urlToLoad = ProxyHelper.unwrapImageUrl(url) val urlToLoad = ProxyHelper.unwrapImageUrl(url)
val request = ImageRequest.Builder(target.context) getImageWithCallback(target.context, urlToLoad) { result ->
.data(urlToLoad) // set the background to white for transparent images
.listener { _, result -> if (whiteBackground) target.setBackgroundColor(Color.WHITE)
// set the background to white for transparent images
if (whiteBackground) target.setBackgroundColor(Color.WHITE)
target.setImageDrawable(result.drawable) target.setImageDrawable(result)
} }
.build()
imageLoader.enqueue(request)
} }
suspend fun downloadImage(context: Context, url: String, path: Path) { suspend fun downloadImage(context: Context, url: String, path: Path) {
@ -110,6 +107,10 @@ object ImageHelper {
} }
suspend fun getImage(context: Context, url: String?): Bitmap? { suspend fun getImage(context: Context, url: String?): Bitmap? {
return getImage(context, url?.toUri())
}
suspend fun getImage(context: Context, url: Uri?): Bitmap? {
val request = ImageRequest.Builder(context) val request = ImageRequest.Builder(context)
.data(url) .data(url)
.build() .build()
@ -117,11 +118,11 @@ object ImageHelper {
return imageLoader.execute(request).drawable?.toBitmapOrNull() return imageLoader.execute(request).drawable?.toBitmapOrNull()
} }
fun getImageWithCallback(context: Context, url: String?, onBitmap: (Bitmap) -> Unit) { private fun getImageWithCallback(context: Context, url: String?, onSuccess: (Drawable) -> Unit) {
val request = ImageRequest.Builder(context) val request = ImageRequest.Builder(context)
.data(url) .data(url)
.target { drawable -> .target { drawable ->
onBitmap(drawable.toBitmap()) onSuccess(drawable)
} }
.build() .build()

View File

@ -53,6 +53,8 @@ open class OfflinePlayerService : AbstractPlayerService() {
} }
override suspend fun onServiceCreated(args: Bundle) { override suspend fun onServiceCreated(args: Bundle) {
if (args.isEmpty) return
downloadTab = args.serializable(IntentData.downloadTab)!! downloadTab = args.serializable(IntentData.downloadTab)!!
shuffle = args.getBoolean(IntentData.shuffle, false) shuffle = args.getBoolean(IntentData.shuffle, false)
noInternetService = args.getBoolean(IntentData.noInternet, false) noInternetService = args.getBoolean(IntentData.noInternet, false)

View File

@ -2,10 +2,12 @@ package com.github.libretube.ui.fragments
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.graphics.Color import android.graphics.Color
import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.os.Handler import android.os.Handler
import android.os.Looper import android.os.Looper
import android.text.format.DateUtils import android.text.format.DateUtils
import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -297,13 +299,13 @@ class AudioPlayerFragment : Fragment(), AudioPlayerOptions {
NavigationHelper.navigateChannel(requireContext(), uploaderId) NavigationHelper.navigateChannel(requireContext(), uploaderId)
} }
metadata.artworkUri?.let { updateThumbnailAsync(it.toString()) } metadata.artworkUri?.let { updateThumbnailAsync(it) }
initializeSeekBar() initializeSeekBar()
} }
private fun updateThumbnailAsync(thumbnailUrl: String) { private fun updateThumbnailAsync(thumbnailUri: Uri) {
if (DataSaverMode.isEnabled(requireContext())) { if (DataSaverMode.isEnabled(requireContext()) && !isOffline) {
binding.progress.isVisible = false binding.progress.isVisible = false
binding.thumbnail.setImageResource(R.drawable.ic_launcher_monochrome) binding.thumbnail.setImageResource(R.drawable.ic_launcher_monochrome)
val primaryColor = ThemeHelper.getThemeColor( val primaryColor = ThemeHelper.getThemeColor(
@ -321,7 +323,7 @@ class AudioPlayerFragment : Fragment(), AudioPlayerOptions {
lifecycleScope.launch { lifecycleScope.launch {
val binding = _binding ?: return@launch val binding = _binding ?: return@launch
val bitmap = ImageHelper.getImage(requireContext(), thumbnailUrl) val bitmap = ImageHelper.getImage(requireContext(), thumbnailUri)
binding.thumbnail.setImageBitmap(bitmap) binding.thumbnail.setImageBitmap(bitmap)
binding.miniPlayerThumbnail.setImageBitmap(bitmap) binding.miniPlayerThumbnail.setImageBitmap(bitmap)
binding.thumbnail.isVisible = true binding.thumbnail.isVisible = true