0

I am creating an app in which I have to play audio from a number of URL links. At the first ever run of the app I am providing user the option of either downloading all of the audio files at once. Right now I am downloading each URL individually using Async Task. The problem with this approach is that since I have some 6000 url links for audio files it takes very long.

My questions is that is there any way I can quickly download audio files from these 6000 urls.

Below I am also providing the code that I am using for downloading each url.

public class DownloadAudio extends AsyncTask<Void, Void, String> {

    public static final String TAG = DownloadAudio.class.getSimpleName();

    ProgressDialog mProgressDialog;

    Context context;
    String stringUrl;
    int index;

    public DownloadAudio(Context context,String url, int randomNumber) {
            this.context = context;

            stringUrl=url;

            index = randomNumber;

        }

        protected void onPreExecute() {
            mProgressDialog = ProgressDialog.show(context, "Please wait", "Download …");
        }

        @Override
        protected String doInBackground(Void... voids) {
            try {

                URL url = new URL(stringUrl);
                HttpURLConnection c = (HttpURLConnection) url.openConnection();
                c.setRequestMethod("GET");
                c.setDoOutput(true);
                c.connect();
                String[] path = url.getPath().split("/");
                int temp = path.length - 1;
                String mp3 = path[temp];
                int lengthOfFile = c.getContentLength();

                String PATH = Environment.getExternalStorageDirectory()+ "/MyAudioApp/" ;
                Log.v(TAG, "PATH: " + PATH);
                File file = new File(PATH);
                file.mkdirs();

                String fileName = mp3;

                File outputFile = new File(file , fileName);


                FileOutputStream fos = new FileOutputStream(outputFile);

                InputStream is = c.getInputStream();

                byte[] buffer = new byte[1024];
                int len1 = 0;
                while ((len1 = is.read(buffer)) != -1) {

                    fos.write(buffer, 0, len1);
                }
                fos.close();
                is.close();

            } catch (IOException e) {
                e.printStackTrace();
            }

        return "done";
    }

    protected void onPostExecute(String result) {
        if (result.equals("done")) {
            mProgressDialog.dismiss();
        }
    }

}

Please any suggestions would be very helpful.

RCR
  • 43
  • 7
  • If you have access to the server side you might think about creating a zip. How is your download implemented, is it one at a time or all at the same time? – Arne Fischer Aug 03 '19 at 22:07
  • "is there any way I can quickly download audio files from these 6000 urls" -- not really. I am guessing that those audio files are at least a couple of MB apiece, which means you are looking at trying to download 10+ GB. That is not going to be very fast. You can improve your throughput by [using a modern HTTP client and a better downloading pattern](https://stackoverflow.com/a/29012988/115145), and by doing a few downloads in parallel. However, particularly if the user is not on WiFi, they may be rather unhappy with you for downloading all this stuff. – CommonsWare Aug 03 '19 at 22:14
  • @ArneFischer I am downloading them one at a time – RCR Aug 04 '19 at 11:13
  • Try to load 5 to 10 parallel. It should greatly improve your performance. – Arne Fischer Aug 04 '19 at 11:37
  • @ArneFischer Thank you for the reply. Is your suggestion possible with HttpURLConnection? – RCR Aug 06 '19 at 00:59
  • Well you have to do this manually. Basically start 10 Async tasks in parallel. Alternatively have a look at ThreadPools they can manage multiple parallel threads for you: https://docs.oracle.com/javase/tutorial/essential/concurrency/pools.html Be aware that when doing stuff in parallel, concurrency starts to be a problem, so if you store your data in a map you will have to synchronise it. – Arne Fischer Aug 06 '19 at 07:27

0 Answers0