2014-10-30 103 views
3

正在使用聲音雲搜索api。當我點擊搜索網址時,它會給我搜索結果。每個音頻都有流url,但不會下載url,因爲它取決於上傳器的設置。每一個沒有下載的文件也下載次數超過1例如,當你打這個網址如何通過使用音頻文件ID獲取soundcloud音頻下載網址

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

這會給像

[{"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"}] 

這是關於搜索結果只有一個音頻和該音頻不可下載,但它的下載次數爲100.這怎麼可能? 任何人都可以告訴我如何下載那些不可下載的音頻? 任何幫助將不勝感激。謝謝:)

+0

也許它最初發布爲可下載並且設置稍後由上傳器更改? – 2Dee 2014-10-30 09:18:49

+0

有什麼方法可以下載不可下載的音頻文件? – 2014-10-30 09:54:53

+0

我當然希望它不是:)如果有人在使用API​​時仍然可以下載文件,那麼用戶將該文件設置爲不可下載有什麼意義? – 2Dee 2014-10-30 09:59:01

回答

4

我自己修復它,我使用android和流url也是一個下載url。流url也可以下載url進行下載,但不會影響下載次數。你可以嘗試這樣的

String file_url = "https://api.soundcloud.com/tracks/93216523/stream?client_id=4346c8125f4f5c40ad666bacd8e96498"; 

通過這個網址來asyntack和管理您下載那裏,你可以通過它像

new DownloadFileFromURL().execute(file_url); 

這裏使用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) { 


     } 

    } 
+0

它適合我thanx – 2015-01-02 13:18:02

0
String file_url = "https://api.soundcloud.com/tracks/93216523/stream?client_id=4346c8125f4f5c40ad666bacd8e96498"; 
DownloadFileFromUR類

下載類:

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