首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我们可以在Android中按流链接下载音乐吗?

我们可以在Android中按流链接下载音乐吗?
EN

Stack Overflow用户
提问于 2014-10-14 04:48:27
回答 1查看 1.6K关注 0票数 2

我正在开发一个在线音乐android应用程序,我需要在第三方服务器上下载一些音乐,但它们只给我流链接,所以我正在寻找一种方法,通过流链接下载音乐。

我的朋友在iOS工作,他可以在iOS中将流包合并到一个音乐文件中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-14 05:11:00

如果您想从任何url中播放和下载.mp3文件,请按照建议的代码执行。

但是,如果您想从服务器下载文件并将其存储在sdcard或内部存储设备上的任何位置,请按照以下代码操作,

代码语言:javascript
运行
复制
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;
        }

编辑:清单权限:

代码语言:javascript
运行
复制
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26352818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档