0

I have downloaded an audio file from Url thanks to Giridharan's answer in the link below:

Android - Save image from URL onto SD card

The problem is that I cannot play it, the error is as follows:

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

I'm sure that the audio url on the Internet is working fine, because I can play audio directly from that Url without downloading, but after download it then cannot play anymore, maybe the data source was changed incorrectly while downloading.

So how to solve this problem? Any help will be appreciated! Thanks for reading.

Community
  • 1
  • 1
Dang Nguyen
  • 354
  • 2
  • 9
  • you can play directly from web fine but how you save audio on sd card above link is saving image from web to sd card. – Sohail Zahid Jul 14 '16 at 10:07
  • Sorry for this stupid question, I thinks that it can also apply to download audio too. So do you know how to download audio from url, I tried many methods shared from Internet but all failed. Thanks. – Dang Nguyen Jul 14 '16 at 10:14

2 Answers2

0

Download Audio from web using below code.

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
   }
}

then play it using /sdcard/myAudio.mp3 path in your media player.

if have any issue see this thread.

Community
  • 1
  • 1
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
0

Finally I found the solution to my question, and now I post here to help everyone else faces the same problem can overcome it.

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();
        }
Dang Nguyen
  • 354
  • 2
  • 9