Closed. This question needs to be more focused。它当前不接受答案。
想改善这个问题吗?更新问题,使其仅通过editing this post专注于一个问题。
12个月前关闭。
我正在开发一个在线音乐android应用程序,我需要在第三方服务器上下载一些音乐,但它们只给我流链接,因此我正在寻找一种仅通过流链接下载音乐的方法。
我的朋友在iOS上工作,他可以通过将流包合并到音乐文件中来在iOS中进行操作。
编辑:清单权限:
想改善这个问题吗?更新问题,使其仅通过editing this post专注于一个问题。
12个月前关闭。
我正在开发一个在线音乐android应用程序,我需要在第三方服务器上下载一些音乐,但它们只给我流链接,因此我正在寻找一种仅通过流链接下载音乐的方法。
我的朋友在iOS上工作,他可以通过将流包合并到音乐文件中来在iOS中进行操作。
最佳答案
如果要从任何URL播放和下载.mp3文件,请遵循建议的代码。
但是,如果您要从服务器下载文件并将其存储在sdcard或内部存储设备上的任何位置,请遵循以下代码,
private class DownloadFile extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... url) {
int count;
try {
URL url = new URL("url of your .mp3 file");
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();
// downlod the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/somewhere/nameofthefile.mp3");
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;
}
编辑:清单权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>