2015-04-23 114 views
-1

在我的Android項目中,通過編程我需要從谷歌驅動器下載URL下載一個.mp3文件並存儲在應用程序沙箱中。然後,應用可以有播放選項在本地播放此音頻。下載mp3文件並存儲在應用程序目錄中

這怎麼可能實現從服務器下載.mp3文件並將其存儲在本地的應用程序?之後,它可以從本地存儲播放。對此非常感謝。

謝謝。

+0

檢查了這一點: [下載MP3文件] [1] [1]:http://stackoverflow.com/a/13133942/1404734 –

+0

@Stella你想下載歌曲在你的設備上或應用程序內(如資源文件夾)? – Signare

+0

應用程序文件夾或設備上的任何公共位置,而不是在SD卡等 – Stella

回答

4

您可以使用此方法:

static void downloadFile(String dwnload_file_path, String fileName, 
     String pathToSave) { 
    int downloadedSize = 0; 
    int totalSize = 0; 

    try { 
     URL url = new URL(dwnload_file_path); 
     HttpURLConnection urlConnection = (HttpURLConnection) url 
       .openConnection(); 

     urlConnection.setRequestMethod("POST"); 
     urlConnection.setDoOutput(true); 

     // connect 
     urlConnection.connect(); 

     File myDir; 
     myDir = new File(pathToSave); 
     myDir.mkdirs(); 

     // create a new file, to save the downloaded file 

     String mFileName = fileName; 
     File file = new File(myDir, mFileName); 

     FileOutputStream fileOutput = new FileOutputStream(file); 

     // Stream used for reading the data from the internet 
     InputStream inputStream = urlConnection.getInputStream(); 

     // this is the total size of the file which we are downloading 
     totalSize = urlConnection.getContentLength(); 

     // runOnUiThread(new Runnable() { 
     // public void run() { 
     // pb.setMax(totalSize); 
     // } 
     // }); 

     // create a buffer... 
     byte[] buffer = new byte[1024]; 
     int bufferLength = 0; 

     while ((bufferLength = inputStream.read(buffer)) > 0) { 
      fileOutput.write(buffer, 0, bufferLength); 
      downloadedSize += bufferLength; 
      // update the progressbar // 
      // runOnUiThread(new Runnable() { 
      // public void run() { 
      // pb.setProgress(downloadedSize); 
      // float per = ((float)downloadedSize/totalSize) * 100; 
      // cur_val.setText("Downloaded " + downloadedSize + "KB/" + 
      // totalSize + "KB (" + (int)per + "%)"); 
      // } 
      // }); 
     } 
     // close the output stream when complete // 
     fileOutput.close(); 
     // runOnUiThread(new Runnable() { 
     // public void run() { 
     // // pb.dismiss(); // if you want close it.. 
     // } 
     // }); 

    } catch (final MalformedURLException e) { 
     // showError("Error : MalformedURLException " + e); 
     e.printStackTrace(); 
    } catch (final IOException e) { 
     // showError("Error : IOException " + e); 
     e.printStackTrace(); 
    } catch (final Exception e) { 
     // showError("Error : Please check your internet connection " + e); 
    } 
} 

調用此方法是這樣的:

String SDCardRoot = Environment.getExternalStorageDirectory() 
          .toString(); 
        Utils.downloadFile("http://my_audio_url/my_file.mp3", "my_file.mp3", 
          SDCardRoot+"/MyAudioFolder"); 

回放:

String SDCardRoot = Environment.getExternalStorageDirectory() 
         .toString(); 
String audioFilePath = SDCardRoot + "/MyAudioFolder/my_file.mp3"; 
MediaPlayer mPlayer = new MediaPlayer(); 
try { 
       mPlayer.setDataSource(audioFilePath); 
       mPlayer.prepare(); 
       mPlayer.start(); 
      } catch (IOException e) { 
       Log.e("AUDIO PLAYBACK", "prepare() failed"); 
      } 
+0

我不應該存儲在SD卡等。我可以存儲在設備上的應用程序文件夾? – Stella

+0

是的,只需用** SDCardRoot +「/ AppFolder」**方法調用替換** SDCardRoot +「/ MyAudioFolder」**。 –

+0

在上述評論中將「AppFolder」更改爲您應用的文件夾名稱,請嘗試此操作。 –

相關問題