从服务器下载的android MP3文件服务器、文件、android

2023-09-04 06:08:31 作者:有酒就喝有爱就做

我想创建一个应用程序可以下载音乐文件,MP3播放为precise,从server.As我在这个Android开发领域的新秀,所以我会AP preciate任何帮助你们。 我需要的东西开始就和我真的AP preciate如果ü可以给我一些链接,有用的资源。 谢谢

I am trying to create an app that can download music files, .mp3 to be precise, from the server.As I am a rookie in this Android Development field so I will appreciate any help from you guys. I need something to start on and I will really appreciate if u can give me some links for useful resources. Thanks

推荐答案

如果你想从任何URL播放.MP3文件,然后按照code。通过NIK建议。

If you want to play the .mp3 file from any url then follow the code suggested by nik.

但是,如果你想下载一个文件形式传递到服务器,并将其存储在SD卡或内部存储设备的任何地方,那么按照这个code,

But if you want to download a file form the server and store it in any place on sdcard or internal storage device then follow this code,

private class DownloadFile extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... urlParams) {
    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;
}

编辑:清单权限:

manifest permission:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

 <uses-permission android:name="android.permission.INTERNET"></uses-permission>