RazaCodeRepo
Lurker
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 or any better approach of handling downloads.
Below I am also providing the code that I am using for downloading each url.
My questions is that is there any way I can quickly download audio files from these 6000 urls or any better approach of handling downloads.
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.
Thanks
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.
Thanks