简体   繁体   English

如何通过使用音频文件ID获取soundcloud音频下载URL

[英]how to get soundcloud audio download url by using audio file id

am using sound cloud search api. 我正在使用声音云搜索API。 when i hit the search url it give me search results. 当我点击搜索网址时,它会给我搜索结果。 every audio has stream url but not download url because it depends on setting of the uploader. 每个音频都有流网址,但没有下载网址,因为它取决于上传者的设置。 Every none downloadable file also has download count more than 1. for example, when you'll hit this url 每个无可下载文件的下载次数也超过1.例如,当您点击此URL时

http://api.soundcloud.com/tracks.json?client_id=4346c8125f4f5c40ad666bacd8e96498&q=tere%20bin&limit=1 http://api.soundcloud.com/tracks.json?client_id=4346c8125f4f5c40ad666bacd8e96498&q=tere%20bin&limit=1

It'll give the search result like that 它会给出那样的搜索结果

[{"kind":"track","id":63225776,"created_at":"2012/10/13 01:52:38 +0000","user_id":26029726,"duration":206177,"commentable":true,"state":"finished","original_content_size":3298521,"last_modified":"2014/10/01 18:56:25 +0000","sharing":"public","tag_list":"","permalink":"tere-bin-uzair-jaswal-official","streamable":true,"embeddable_by":"all","downloadable":false,"purchase_url":null,"label_id":null,"purchase_title":null,"genre":"Musical","title":"Tere Bin - Uzair Jaswal [Official Music Audio]","description":"","label_name":"","release":"","track_type":"original","key_signature":"","isrc":"","video_url":null,"bpm":null,"release_year":null,"release_month":null,"release_day":null,"original_format":"mp3","license":"all-rights-reserved","uri":"https://api.soundcloud.com/tracks/63225776","user":{"id":26029726,"kind":"user","permalink":"uzair-jaswal-1","username":"Uzair Jaswal Music","last_modified":"2014/10/19 13:06:28 +0000","uri":"https://api.soundcloud.com/users/26029726","permalink_url":"http://soundcloud.com/uzair-jaswal-1","avatar_url":"https://i1.sndcdn.com/avatars-000110064166-2ts508-large.jpg"},"permalink_url":"http://soundcloud.com/uzair-jaswal-1/tere-bin-uzair-jaswal-official","artwork_url":"https://i1.sndcdn.com/artworks-000032079002-kup6vc-large.jpg","waveform_url":"https://w1.sndcdn.com/9bwAsZfGrxwN_m.png","stream_url":"https://api.soundcloud.com/tracks/63225776/stream","playback_count":359588,"download_count":100,"favoritings_count":7557,"comment_count":491,"attachments_uri":"https://api.soundcloud.com/tracks/63225776/attachments","policy":"ALLOW"}]

this is for only one audio and that audio is not downloadable but it has download count of 100. how is this possible ? 这只适用于一个音频而且音频不可下载,但下载次数为100.这怎么可能? can anybody tell me how i can download that audio which is not downloadable? 任何人都可以告诉我如何下载不可下载的音频? any help would be much appreciated. 任何帮助将非常感激。 Thanks :) 谢谢 :)

I fix it myself, i was using android and the stream url is also a download url. 我自己修复它,我使用的是android,流url也是一个下载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 将此URL传递给asyntack并管理您在那里下载,您可以像这样传递它

new DownloadFileFromURL().execute(file_url);

here is DownloadFileFromUR class using asyntask 这里是使用asyntask的DownloadFileFromUR类

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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