【问题标题】:How to download an MP3 file如何下载 MP3 文件
【发布时间】:2011-04-27 12:03:21
【问题描述】:

我想使用AsyncTask 或线程下载一个mp3 文件。

我该怎么做?

【问题讨论】:

    标签: android multithreading asynchronous download mp3


    【解决方案1】:

    你可以做这样的事情......

    当您决定开始下载时:

    new Thread(new Runnable()
      {
      @Override
      public void run()
           {
           File out;
           Downloader DDL;
           DDL=new Downloader();
           out=new File(Environment.getExternalStorageDirectory() + "/DestFileName.txt");
           DDL.DownloadFile("SourceURL",out);
    
           }
        }).start();
    

    下载器类在哪里

    public class Downloader {
    
    public void Downloader() 
            {
        // TODO Auto-generated method stub
        }
    
    public boolean DownloadFile(String url, File outputFile) 
        {
        try {
          URL u = new URL(url);
          URLConnection conn = u.openConnection();
          int contentLength = conn.getContentLength();
    
          DataInputStream stream = new DataInputStream(u.openStream());
    
          byte[] buffer = new byte[contentLength];
          stream.readFully(buffer);
          stream.close();
    
          DataOutputStream fos = new DataOutputStream(new FileOutputStream(outputFile));
          fos.write(buffer);
          fos.flush();
          fos.close();
          } 
        catch(FileNotFoundException e) 
          {
          return false; 
          } 
        catch (IOException e) 
          {
          return false; 
          }
    
        return true;
        }
    }
    

    【讨论】:

      【解决方案2】:

      参考这个帖子:

      android: file download in background

      【讨论】:

        【解决方案3】:

        使用此功能在 SDCARD 上下载文件

        public void downloadNauhe(String url) {
        
            class DownloadFile extends AsyncTask<String, Integer, String> {
                @Override
                protected String doInBackground(String... url) {
                    int count;
                    try {
                        URL url1 = new URL("http://downloads.hussainiat.com/nauhey/arsalan_haider/vol_2011_-_12/01_tum_jawab_e_zulm_dogay_-_arsalan_haider_2011_-_2012.mp3");
                        URLConnection conexion = url1.openConnection();
                        conexion.connect();
                        int lenghtOfFile = conexion.getContentLength();
                        InputStream input = new BufferedInputStream(url1.openStream());
                        OutputStream output = new FileOutputStream("/sdcard/01_manum_abbas_as_-_mesum_abbas_2012.mp3");
                        byte data[] = new byte[1024];
                        long total = 0;
                        System.out.println("downloading.............");
                        while ((count = input.read(data)) != -1) {
                            total += count;
                            publishProgress((int)((total/(float)lenghtOfFile)*100));
                            output.write(data, 0, count);                   
                        }
                        output.flush();
                        output.close();
                        input.close();
                    } catch (Exception e) {
                    }
        
                    return null;
                }
        
                @Override
                protected void onProgressUpdate(Integer... values) {
                    super.onProgressUpdate(values);
                    mprBar.setProgress(values[0]);
                }
            }
            DownloadFile downloadFile = new DownloadFile();
            downloadFile.execute(url);
        }
        

        别忘了添加权限

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

        【讨论】:

          猜你喜欢
          • 2017-07-04
          • 2021-12-11
          • 1970-01-01
          • 2013-01-09
          • 2014-08-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-04-08
          相关资源
          最近更新 更多