how to get soundcloud audio download url by using audio file id
I fix it myself, i was using android and the stream url is also a download url. the stream url is also download url for downloading but it won't affect on download count. you can try like that
String file_url = "https://api.soundcloud.com/tracks/93216523/stream?client_id=4346c8125f4f5c40ad666bacd8e96498";
pass this url to asyntack and manage you download there, you can pass it like that
new DownloadFileFromURL().execute(file_url);
here is DownloadFileFromUR class using asyntask
class DownloadFileFromURL extends AsyncTask<String, Integer, String> { @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected String doInBackground(String... f_url) { URL u = null; InputStream is = null; try { u = new URL(f_url[0]); is = u.openStream(); HttpURLConnection huc = (HttpURLConnection)u.openConnection();//to know the size of video int size = huc.getContentLength(); if(huc != null){ String fileName = "FILE2.mp3"; String storagePath = Environment.getExternalStorageDirectory().toString(); File f = new File(storagePath,fileName); FileOutputStream fos = new FileOutputStream(f); byte[] buffer = new byte[1024]; long total = 0; int len1 = 0; if(is != null){ while ((len1 = is.read(buffer)) > 0) { total+=len1; publishProgress((int)((total*100)/size)); fos.write(buffer,0, len1); } } if(fos != null){ fos.close(); } } }catch (MalformedURLException mue) { mue.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } finally { try { if(is != null){ is.close(); } }catch (IOException ioe) { // just going to ignore this one } } return ""; } @Override protected void onPostExecute(String file_url) { } }
String file_url = "https://api.soundcloud.com/tracks/93216523/stream?client_id=4346c8125f4f5c40ad666bacd8e96498";
DownLoad Class:
private class DownloadFile extends AsyncTask<String, Integer, String> { @Override protected String doInBackground(String... params) { int count; try { URL url = new URL(file_url); URLConnection conexion = url.openConnection(); conexion.connect(); // this will be useful so that you can show a tipical 0-100% progress bar int lenghtOfFile = conexion.getContentLength(); // download the file InputStream input = new BufferedInputStream(url.openStream()); OutputStream output = new FileOutputStream(getOutputMediaFile()); byte data[] = new byte[1024]; long total = 0; while ((count = input.read(data)) != -1) { total += count; // publishing the progress.... publishProgress((int) (total * 100 / lenghtOfFile)); output.write(data, 0, count); } output.flush(); output.close(); input.close(); } catch (Exception e) { } return null; } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); }}