【问题标题】:How to download music .mp3 files to app storage in android?如何将音乐 .mp3 文件下载到 Android 中的应用程序存储?
【发布时间】:2021-02-27 07:45:05
【问题描述】:

音乐文件应下载到应用程序本身,不应存储在移动内存(文件管理器或 SD 卡)中。

【问题讨论】:

  • 不可能将应用程序用作存储位置。应用特定的存储是可能的。这是设备存储。不清楚你想要什么。

标签: java android android-studio android-mediaplayer


【解决方案1】:

FileManager 类将 mp3 文件存储在应用程序文件夹中。

public class FileManager {

    private final Context context;

    FileManager(Context context) {
        this.context = context;
    }

    public String getRootPath() {
    File file = new File(context.getFilesDir(),"");
    return file.getAbsolutePath();
}
public String getFilePath(String name) {
        File file = new File(context.getFilesDir(), name);
        return file.getAbsolutePath();
    }
  }

下载请求

@RequiresApi(api = Build.VERSION_CODES.O)
    private void initDownload(String soundUri) {
        Log.d(TAG, "initDownload: " + soundUri);

        Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL).build();

        ApiService retrofitInterface = retrofit.create(ApiService.class);
        Call<ResponseBody> request = retrofitInterface.downloadFile(soundUri);

        request.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
               if (response.body() != null) {
                    Thread thread = new Thread(() -> {
                        try {
                            saveVoice(response.body());
                        } catch (IOException e) {
                            Log.d(TAG, "onResponse: " + e);
                        }
                    });
                    thread.start();
                }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {

        }
    });
    }




@RequiresApi(api = Build.VERSION_CODES.O)
private void saveVoice(ResponseBody body) throws IOException {
    try {
        String mp3Key= "YOUR_KEY_TO_STORE_FILE";

        int count;
        byte data[] = new byte[1024 * 4];
        InputStream bis = new BufferedInputStream(body.byteStream(), 1024 * 8);
        File outputFile = new File(fileManager.getRootPath(), voiceKey);
        OutputStream output = new FileOutputStream(outputFile);
        long startTime = System.currentTimeMillis();
        int timeCount = 1;
        while ((count = bis.read(data)) != -1) {

            long currentTime = System.currentTimeMillis() - startTime;

            if (currentTime > 1000 * timeCount) {

                timeCount++;
            }
            output.write(data, 0, count);
        }
        output.flush();
        output.close();
        bis.close();
    } catch (Exception e) {
        Log.d(TAG, "saveVoice: " + e);
    }
}

成功下载后,您可以使用您将文件写入应用存储的密钥读取文件。为此:

String filePath = fileManager.getFilePath(mp3Key); // filePath returns local url of file

例如,你可以这样做:

 mediaPlayer.setDataSource(filePath );

【讨论】:

    猜你喜欢
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    • 2020-03-22
    • 2011-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多