简体   繁体   中英

Download multiple MP3 files in the background in Android

LEVEL: Beginner
Language: Java
IDE: Android Studio
CASE: Downloading multiple MP3 files from multiple URL's in a row in the background.
ISSUE: download task in the loop to download all the urls. the songs did download in the following path but aren't fully downloaded (every song is downloaded for 3 seconds/4 seconds basically very less). don't know what's the problem, It'll be great if i can get any help

  • receive mp3 url's from API
  • download them using the following code. (ISSUE)
  • get the path of all and store them in the Database
  • fetch the path and play one by one continuously.

     private class DownloadTask extends AsyncTask<SongDetails, Integer, String> { Context context; public DownloadTask(Context context) { this.context = context; } @Override protected void onPreExecute() { super.onPreExecute(); Utilities.showProgressDialog(getActivity(), getFragmentManager()); } @Override protected String doInBackground(SongDetails... songDetails) { for (SongDetails songs : songDetails ) { donwloadsong(songs); } return null; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Utilities.dismissProgressDialog(); } } for (SongDetails songs : songDetails ) { downloadTask(songs); } public void downloadTask(SongDetails songs){ HttpURLConnection httpURLConnection = null; try { URL url = new URL(songs.getUrl()); httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("GET"); httpURLConnection.connect(); int fileLength = httpURLConnection.getContentLength(); InputStream inputStream = new BufferedInputStream(url.openStream(), 8192); OutputStream outputStream1; outputStream1 = new FileOutputStream(Environment.getExternalStorageDirectory() + "/gbeduwarssongs/" + songDetails.getTitle().trim() + ".mp3"); int count = 0; byte data[] = new byte[1024]; int buffer_length; while ((buffer_length = inputStream.read(data)) != -1) { outputStream1.write(data, 0, count); count += buffer_length; } outputStream1.flush(); outputStream1.close(); inputStream.close(); } catch (Exception e) { e.printStackTrace(); Log.e("Error", "Download Error Exception " + e.getMessage()); } finally { if (httpURLConnection != null) httpURLConnection.disconnect(); } } 

Rather calling iteration inside a new thread, use iteration first and then start new thread in every iteration. I hope it will help you. I am posting code for your help.

for (SongDetails songs : songDetails) {
        new DownloadTask(pass your song object in constructor).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }

and in doInBackground call your downloading method stuff.

I offer to use DownloadManager or workmanager for it. AsyncTask has used a bad idea. It has a problem with a memory leak . The asyncTask use only one thread

The same question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM