private fun checkPermissionsAndDownload(url: String, fileName: String) { val permissions = mutableListOf<String>() if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE) } } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) { permissions.add(Manifest.permission.POST_NOTIFICATIONS) } } if (permissions.isNotEmpty()) { permissionLauncher.launch(permissions.toTypedArray()) } else { startDownload(url, fileName) } }
private val connection = object : ServiceConnection { override fun onServiceConnected(name: ComponentName?, service: IBinder?) { val binder = service as VideoDownloaderService.DownloadBinder downloadService = binder.getService() isBound = true } override fun onServiceDisconnected(name: ComponentName?) { isBound = false downloadService = null } } download 4k video from youtube android
private fun loadSampleVideos() { // Sample public domain/Creative Commons video URLs for testing val samples = listOf( SampleVideo("Sample 1 - Big Buck Bunny", "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"), SampleVideo("Sample 2 - For Bigger Blazes", "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerBlazes.mp4"), SampleVideo("Sample 3 - For Bigger Funrides", "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerFunrides.mp4") ) // Display samples in RecyclerView (simplified - add to a list) binding.samplesContainer.removeAllViews() samples.forEach { sample -> val button = com.google.android.material.button.MaterialButton(this).apply { text = sample.name layoutParams = android.view.ViewGroup.LayoutParams( android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT ) setOnClickListener { binding.etUrl.setText(sample.url) binding.etFileName.setText(sample.name.replace(" ", "_").lowercase()) } } binding.samplesContainer.addView(button) } } private fun checkPermissionsAndDownload(url: String
fun downloadVideo(url: String, fileName: String, onProgress: (Float) -> Unit, onComplete: (File?) -> Unit) { serviceScope.launch { try { startForegroundWithNotification() val request = Request.Builder().url(url).build() val response = client.newCall(request).execute() if (!response.isSuccessful) { withContext(Dispatchers.Main) { onComplete(null) } return@launch } val contentLength = response.body?.contentLength() ?: -1L val inputStream = response.body?.byteStream() val downloadsDir = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) val outputFile = File(downloadsDir, "$fileName.mp4") FileOutputStream(outputFile).use { outputStream -> val buffer = ByteArray(8192) var bytesRead: Int var totalBytesRead = 0L while (inputStream?.read(buffer).also { bytesRead = it ?: -1 } != -1) { outputStream.write(buffer, 0, bytesRead) totalBytesRead += bytesRead if (contentLength > 0) { val progress = (totalBytesRead.toFloat() / contentLength) * 100 withContext(Dispatchers.Main) { onProgress(progress) updateNotification(progress.toInt()) } } } } inputStream?.close() withContext(Dispatchers.Main) { onComplete(outputFile) } stopForeground(false) } catch (e: Exception) { e.printStackTrace() withContext(Dispatchers.Main) { onComplete(null) } } } } () if (Build.VERSION.SDK_INT <
override fun onBind(intent: Intent?): IBinder = binder
<com.google.android.material.button.MaterialButton android:id="@+id/btnDownload" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="Download Video" app:icon="@android:drawable/stat_sys_download" />