// 建立HTTP连接 val url = URL("http://example.com/video.php") val connection = url.openConnection() as HttpURLConnection connection.requestMethod = "GET"
// 接收数据块并拼接 val base64 = StringBuilder() val input: InputStream = connection.inputStream val buffer = ByteArray(1024) var bytesRead: Int while (input.read(buffer).also { bytesRead = it } > 0) { base64.append(String(buffer, 0, bytesRead)) }
// 解码并播放视频 val base64String = base64.toString() valdata = Base64.decode(base64String, Base64.DEFAULT) val file = File(getExternalFilesDir(null), "video.mp4") val output = FileOutputStream(file) output.write(data) output.close()
val downloadUrl = "视频路径 .mp4" val fileName = "myvideo.mp4" val request = DownloadManager.Request(Uri.parse(downloadUrl)) .setTitle(fileName) .setDescription("Downloading") .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName) val downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager downloadManager.enqueue(request)
val responseCode = connection.responseCode // 获取连接的响应代码。 if (responseCode == HttpURLConnection.HTTP_OK) { // 检查响应代码是否为HTTP OK(200)。
val inputStream = connection.inputStream // 获取连接的输入流。 val base64 = StringBuilder() // 初始化一个新的StringBuilder对象以存储Base64编码的视频数据。 val buffer = ByteArray(1024) // 初始化一个新的字节数组以存储数据。 var bytesRead: Int// 初始化一个新的bytesRead变量。 while (inputStream.read(buffer).also { bytesRead = it } > 0) { // 逐块读取数据。 base64.append(String(buffer, 0, bytesRead)) // 将每块数据作为字符串追加到StringBuilder对象中。 } Log.d("MainActivity", "Response received: $base64") // 将响应记录到控制台中。 // 解码并播放视频 val base64String = base64.toString() // 获取Base64字符串。 valdata = Base64.decode(base64String, Base64.DEFAULT) // 将Base64字符串解码为字节数组。 val file = File(getExternalFilesDir(null), "video.mp4") // 初始化一个新的File对象以指定本地文件路径。 val output = FileOutputStream(file) // 初始化一个新的FileOutputStream对象以将字节数组写入文件。 output.write(data) // 将字节数组写入文件。 output.close() // 关闭FileOutputStream对象。 val videoView: VideoView = bd.videoView file.writeBytes(data) withContext(Dispatchers.Main){ videoView.setVideoPath(file.absolutePath) videoView.start() } } }
1 2 3
while (inputStream.read(buffer).also { bytesRead = it } > 0) { // 逐块读取数据。 base64.append(String(buffer, 0, bytesRead)) // 将每块数据作为字符串追加到StringBuilder对象中。 }
从连接的输入流中逐块读取视频数据并将其存储在一个StringBuilder对象中,以便后续进行Base64解码和写入本地文件。在循环中,每次从输入流中读取的数据块都将存储在一个指定大小的字节数组中。 inputStream.read(buffer) 将数据块读入缓冲区中,并返回读取的字节数,返回值为-1时表示已到达流的结尾。通过 also 和 it 确保读取到的字节数能够赋值给 bytesRead 变量,并且该变量的值在每次循环迭代中都会被更新。
//设置权限,没有异常处理 val MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 123
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE) }
//下载视频 val downloadUrl = "http://……/……/…….mp4" val fileName = "video.mp4" val request = DownloadManager.Request(Uri.parse(downloadUrl)) .setTitle(fileName) .setDescription("Downloading") .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName) val downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager downloadManager.enqueue(request)
val filePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).absolutePath + "/video.mp4" val videoView = findViewById<VideoView>(R.id.videoView) videoView.setVideoPath(filePath) videoView.setOnPreparedListener { mp -> mp.start() }