Download-Manager für Audio fehlgeschlagenAndroid

Forum für diejenigen, die für Android programmieren
Anonymous
 Download-Manager für Audio fehlgeschlagen

Post by Anonymous »

Wenn ich ein Lied von Webview herunterlade, wird es in den Ordner heruntergeladen, aber es ist nicht erfolgreich und ich kann es nicht anhören. Ich verstehe nicht, was falsch ist. Ich habe die URL selbst festgelegt, um sicherzustellen, dass sie korrekt ist.
API ist 36

Code: Select all

@RequiresApi(Build.VERSION_CODES.Q)
@Composable
fun DownloadMusicScreen(
innerPadding: PaddingValues,
applicationContext: Context,
musicViewModel: MusicViewModel = hiltViewModel()
) {
val url = "https://rus.hitmotop.com/"

AndroidView(factory = {
WebView(it).apply {
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
webViewClient = WebViewClient()
loadUrl(url)
setDownloadListener { url, _, contentDisposition, mimeType, _ ->
setupDownloadListener(
"https://eu.hitmo-top.com/get/music/20251124/Kjenni_MS_Dymka_-_Vorona_80311772.mp3",
contentDisposition,
mimeType,
applicationContext
)
musicViewModel.fetchAudioFromDevice(true)
}
}
}, update = {
it.loadUrl(url)
})
}

private fun setupDownloadListener(
url: String,
contentDisposition: String,
mimeType: String,
context: Context
) {
val filename = getFileName(contentDisposition, url)

val request = DownloadManager.Request(url.toUri())
.setMimeType(mimeType)
.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setTitle(filename)
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename)

val downloadManager = context.getSystemService(DownloadManager::class.java)
downloadManager.enqueue(request)
Toast.makeText(context, "Download started", Toast.LENGTH_SHORT).show()
}

private fun getFileName(contentDisposition: String?, url: String): String {
return if (contentDisposition != null && contentDisposition.contains("filename=")) {
contentDisposition.substringAfter("filename=").replace("\"", "")
} else {
url.substringAfterLast("/")
}
}

package com.example.coroutineapp.domain

class DownloadCompletedReciever: BroadcastReceiver() {
private lateinit var downloadManager: DownloadManager

override fun onReceive(context: Context?, intent: Intent?) {
downloadManager = context?.getSystemService(DownloadManager::class.java)!!

if(intent?.action == "android.intent.action.DOWNLOAD_COMPLETE"){
val id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1L)
val query = DownloadManager.Query().setFilterByStatus(DownloadManager.STATUS_FAILED)

if(id != -1L){
println("download with id $id finished!")
}
}
}
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post