小编Tom*_*mmy的帖子

Kotlin - 如何下载 .mp3 文件并保存到内部存储

我正在尝试从 url 下载 .mp3 文件并保存到内部存储。我已经能够下载数据并保存它,但音频文件听起来不正确。听起来一点都不像原版。

我可以选择 View -> Tool Windows -> Device File Explorer 然后打开 data/data/[myPackageName]/files 并保存 audio.mp3 文件然后播放它但时间不正确,字节大小错误,音频听起来不像

这是我的 AsyncTask 类:

    class DownloadAudioFromUrl(val context: Context): AsyncTask<String, String, String>() {

        override fun doInBackground(vararg p0: String?): String {
            val url  = URL(p0[0])
            val connection = url.openConnection()
            connection.connect()
            val inputStream = BufferedInputStream(url.openStream())
            val filename = "audio.mp3"
            val outputStream = context.openFileOutput(filename, Context.MODE_PRIVATE)
            val data = ByteArray(1024)
            var total:Long = 0
            var count = 0
            while (inputStream.read(data) != -1) {
                count = inputStream.read(data)
                total …
Run Code Online (Sandbox Code Playgroud)

audio android android-asynctask kotlin

5
推荐指数
1
解决办法
2049
查看次数

标签 统计

android ×1

android-asynctask ×1

audio ×1

kotlin ×1