2016-07-14 18 views
0

我在下面的鏈接下載的網址謝謝音頻文件Giridharan的回答是:Android - 由於數據源錯誤而無法播放從Url下載的音頻。如何解決這個問題?

Android - Save image from URL onto SD card

的問題是,我不能打它,錯誤如下:

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

我確定互聯網上的音頻網址工作正常,因爲我可以直接從該Url播放音頻而無需下載,但下載後無法播放,也可能下載時數據源被錯誤地更改。

那麼如何解決這個問題呢?任何幫助將不勝感激!謝謝閱讀。

+0

你可以直接從網頁播放,但你如何將音頻保存在SD卡上面的鏈接是保存圖像從網絡到SD卡。 –

+0

對不起,這個愚蠢的問題,我認爲它也適用於下載音頻。所以你知道如何從url下載音頻,我嘗試了很多從互聯網共享的方法,但都失敗了。謝謝。 –

回答

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(); 
     } 
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.

相關問題