使用下面的代碼從網絡下載音頻。
private void startDownload() {
String url = "http://farm1.static.flickr.com/114/298125983_0e4bf66782_b.jpg";
// Smaple url String url = "http://farm1.static.flickr.com/114/298125983_0e4bf66782_b.jpg";
new DownloadFileAsync().execute(url);
}
class DownloadFileAsync extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// create dialog if you want
}
@Override
protected String doInBackground(String... aurl) {
int count;
try {
URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/myAudio.mp3");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {}
return null;
}
@Override
protected void onPostExecute(String unused) {
// hide/dismiss dialog if you have any
}
}
然後使用您的媒體播放器/sdcard/myAudio.mp3
路徑播放。
如果有任何問題,請參閱本thread.
你可以直接從網頁播放,但你如何將音頻保存在SD卡上面的鏈接是保存圖像從網絡到SD卡。 –
對不起,這個愚蠢的問題,我認爲它也適用於下載音頻。所以你知道如何從url下載音頻,我嘗試了很多從互聯網共享的方法,但都失敗了。謝謝。 –