【问题标题】:Downloading and Playing mp3 file into internal storage下载和播放 mp3 文件到内部存储
【发布时间】:2016-09-28 06:17:38
【问题描述】:

我们正在开发 android 应用程序,其中 mp3 文件应下载到后台服务的内部存储中。我们已经在intent service 中使用下面提到的代码 sn-p 实现了这一点,但是当我们尝试播放 mp3 时出现错误,例如播放器不支持此音频文件。如果有人对此问题有解决方案,请告知我。

代码片段:

    String urlPath = intent.getStringExtra(URL);
    String fileName = intent.getStringExtra(FILENAME);
    //  String fileName = Environment.getExternalStorageDirectory().toString() + "/trail.mp3";
    File output = new File(Environment.getExternalStorageDirectory(),
            fileName);

    File dir = new File(output.getAbsolutePath()
            + "/TRIALS/");

    dir.mkdirs();
    String path = dir + "/" +"trail" + ".mp3";
    File f = new File(path);


    if (f.exists()) {
        f.delete();
    }

    InputStream stream = null;
    FileOutputStream fos = null;
    try {

        URL url = new URL(urlPath);
        URLConnection conection = url.openConnection();
        conection.connect();
        // getting file length
        int lenghtOfFile = conection.getContentLength();
        //URL url = new URL(urlPath);
        InputStream input = new BufferedInputStream(url.openStream());

        stream = url.openConnection().getInputStream();
        InputStreamReader reader = new InputStreamReader(stream);
        fos = new FileOutputStream(f.getPath());

        byte data[] = new byte[lenghtOfFile];

        long total = 0;

        int times = -1;
        while ((times = reader.read()) != -1) {
            fos.write(times);

        }
        // Successful finished
        result = Activity.RESULT_OK;

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    publishResults(output.getAbsolutePath(), result);
}

【问题讨论】:

    标签: android file storage internal download


    【解决方案1】:

    1.检查urlPath是否正确。可以用资源管理器打开,检查.mp3是否开始下载。

    2.将资源管理器获取的文件大小与通过代码下载的文件大小进行比较。

    3.如果大小不同,则表示下载失败。尝试像这样修改您的代码:

        String urlPath = intent.getStringExtra(URL);
        String fileName = intent.getStringExtra(FILENAME);
        File output = new File(Environment.getExternalStorageDirectory(),
                fileName);
        File dir = new File(output.getAbsolutePath()
                + "/TRIALS/");
        dir.mkdirs();
        String path = dir + "/" +"trail" + ".mp3";
        File f = new File(path);
    
        if (f.exists()) {
            f.delete();
        }
    
        InputStream stream = null;
        FileOutputStream fos = null;
        try {
    
            URL url = new URL(urlPath);
            URLConnection urlcon = url.openConnection();
            stream = urlcon.getInputStream();
    
            InputStreamReader reader = new InputStreamReader(stream);
            fos = new FileOutputStream(f.getPath());
    
            int times = -1;
            while ((times = reader.read()) != -1) {
                fos.write(times);
            }
    
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
            if (fos != null) {
                try {
                    fos.flush();
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    

    我删除了一些无用的代码,并在fos.close()之前添加了fos.flush()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 2011-05-31
      • 1970-01-01
      • 2019-07-31
      • 1970-01-01
      相关资源
      最近更新 更多