2014-02-05 63 views
1

我可以從我的SD卡上的Sound Cloud中下載聲音文件。我可以將其設置爲我的鈴聲,並且正常工作。但是當我試圖從SD卡上播放它時,我收到「文件不支持」消息,並且音樂沒有播放。我想我沒有得到文件擴展名,這就是爲什麼音樂不播放請幫助我。下載的歌曲不是從SD卡播放?

case PARSE_GET_TRACK: 
    ... 
    case PARSE_DOWNLOAD_TRACK: 
       String fileName = mTrack.getTrackTitle() 
         + mTrack.getTrackId(); 
       String path = mTrack.getDownloadUrl() 
         +"?client_id=xxx"; 


       URL u = new URL(path); 

       httpRequest = new HttpGet(u.toURI()); 
       httpclient = new DefaultHttpClient(); 
       response = (HttpResponse) httpclient.execute(httpRequest); 

       HttpURLConnection c = (HttpURLConnection) u 
         .openConnection(); 
       c.setRequestMethod("GET"); 
       c.setDoInput(true); 
       c.setDoOutput(true); 
       c.connect(); 


       File sdCard = Environment.getExternalStorageDirectory(); 
       File dir = new File(sdCard.getAbsolutePath() + "/dir1/dir2"); 
       dir.mkdirs(); 
       File file = new File(dir, fileName); 

       mTrack.setAboluteFilePath(file.getAbsolutePath()); 

       boolean redirect = false; 

       int status = c.getResponseCode(); 
       if (status != HttpURLConnection.HTTP_OK) { 
        if (status == HttpURLConnection.HTTP_MOVED_TEMP 
          || status == HttpURLConnection.HTTP_MOVED_PERM 
          || status == HttpURLConnection.HTTP_SEE_OTHER) 
         redirect = true; 

       } 

       if (redirect) { 


        String newUrl = c.getHeaderField("Location"); 

        c = (HttpURLConnection) new URL(newUrl) 
          .openConnection(); 

       } 

       InputStream inputStream = response.getEntity().getContent(); 
       InputStreamReader inputStreamReader = new InputStreamReader(
         inputStream); 

       BufferedReader bufferedReader = new BufferedReader(
         inputStreamReader); 

       BufferedInputStream bis = new BufferedInputStream(
         inputStream, 1024 * 50); 
       FileOutputStream fos = new FileOutputStream(file); 
       byte[] buffer = new byte[1024 * 50]; 

       int current = 0; 
       while ((current = bis.read(buffer)) != -1) { 
        fos.write(buffer, 0, current); 
       } 
       fos.flush(); 
       StringBuilder stringBuilder = new StringBuilder(); 

       String bufferedStrChunk = null; 

       while ((bufferedStrChunk = bufferedReader.readLine()) != null) { 
        stringBuilder.append(bufferedStrChunk); 
       } 

       fos.close(); 
       bis.close(); 
       return stringBuilder.toString(); 

      case PARSE_GET_VID_THUMB: 
    .... 
+0

你的文件名應該是擴展名爲.mp3 –

回答

0

是的嘗試了一些不同的方式後,我解決了它。您只需要在「曲目ID」字符串中進行更改。

String fileName = mTrack.getTrackTitle() 
        + mTrack.getTrackId()+ ".3gp";