Android - 由于数据源错误而无法从Url下载后播放音频。怎么解决这个?

时间:2016-07-14 09:58:32

标签: android audio

由于Giridharan在以下链接中的回答,我已经从Url下载了一个音频文件:

Android - Save image from URL onto SD card

问题是我无法播放,错误如下:

java.io.IOException: setDataSourceFD failed.: status=0x80000000

我确信互联网上的音频网址工作正常,因为我可以直接从该网址播放音频而无需下载,但下载后无法播放,可能是数据源在下载时输入错误。< / p>

那么如何解决这个问题呢?任何帮助将不胜感激!谢谢你的阅读。

2 个答案:

答案 0 :(得分:0)

使用以下代码从网上下载音频。

private void startDownload() {
    String url = "http://farm1.static.flickr.com/114/298125983_0e4bf66782_b.jpg";
    // Smaple url String url = "http://farm1.static.flickr.com/114/298125983_0e4bf66782_b.jpg";
    new DownloadFileAsync().execute(url);
}

class DownloadFileAsync extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
       // create dialog if you want 
    }

    @Override
    protected String doInBackground(String... aurl) {
        int count;
        try {
            URL url = new URL(aurl[0]);
            URLConnection conexion = url.openConnection();
            conexion.connect();
            int lenghtOfFile = conexion.getContentLength();
            Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream("/sdcard/myAudio.mp3");
            byte data[] = new byte[1024];
            long total = 0;
            while ((count = input.read(data)) != -1) {
            total += count;
            publishProgress(""+(int)((total*100)/lenghtOfFile));
            output.write(data, 0, count);
        }

        output.flush();
        output.close();
        input.close();
    } catch (Exception e) {}
        return null;
    }


    @Override
    protected void onPostExecute(String unused) {
        // hide/dismiss dialog if you have any
   }
}

然后使用媒体播放器中的/sdcard/myAudio.mp3路径播放。

如果有任何问题,请参阅此thread.

答案 1 :(得分:0)

最后我找到了我的问题的解决方案,现在我发布在这里帮助其他人面临同样的问题可以克服它。

public String downloadAudioFromUrl(String url) {
            int count;
            File file = null;
            try {
                URL urls = new URL(url);
                URLConnection connection = urls.openConnection();
                connection.connect();
                // this will be useful to show the percentage 0-100% in progress bar
                int lengthOfFile = connection.getContentLength();

                File storageDir = new File(Environment.getExternalStorageDirectory().toString() + "/Photo_Quiz/Audio");
                if (!storageDir.exists()) {
                    storageDir.mkdirs();
                }
                String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmssSSS").format(new Date());
                String filename = "audio_" + timeStamp + ".3gp";
                file = new File(storageDir, filename);

                InputStream input = new BufferedInputStream(urls.openStream());
                OutputStream output = new FileOutputStream(file.getAbsolutePath());
                byte data[] = new byte[1024];
                long total = 0;

                while ((count = input.read(data)) != -1) {
                    total += count;
                    // publishing the progress...
//                    publishProgress((int) (total * 100 / lengthOfFile));
                    output.write(data, 0, count);
                }

                output.flush();
                output.close();
                input.close();
                notifyNewMediaFile(file);
            } catch (Exception e) {
                e.printStackTrace();
            }

            return file.getAbsolutePath();
        }