Менеджер загрузок неудачно аудиоAndroid

Форум для тех, кто программирует под Android
Ответить
Anonymous
 Менеджер загрузок неудачно аудио

Сообщение Anonymous »

Когда я загружаю песню из веб-просмотра, она загружается в папку downloaded/audio, но загрузка не удалась, и я не могу прослушать аудио. Я не понимаю, что не так, я сам установил URL-адрес, чтобы убедиться, что он правильный.
API — 36, заданный контекст — это контекст активности, я тоже пробовал LocalContext.current

@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/2025 ... 311772.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!")
}
}
}
}

















Подробнее здесь: https://stackoverflow.com/questions/798 ... sful-audio
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Android»