2016-04-09 153 views
1

我使用下面的代碼從我的服務器上下載一個MP3文件到Android下載MP3文件拋出IOException異常

public class DownloadService extends IntentService { 

    private int result = Activity.RESULT_CANCELED; 
    public static final String RESULT = "result"; 
    public static final String NOTIFICATION = "[email protected]#$%%^"; 

    public DownloadService() { 
     super("DownloadService"); 
    } 

    // will be called asynchronously by Android 
    @Override 
    protected void onHandleIntent(Intent intent) { 
     Integer serverTrackId=intent.getIntExtra(Constants.INTENT_PARAM_SERVER_TRACK_ID, 0); 
     String serverUrl=intent.getStringExtra(Constants.INTENT_PARAM_SERVER_TRACK_URL); 
     String trackName=intent.getStringExtra(Constants.INTENT_PARAM_SERVER_TRACK_NAME); 
     String filePath=intent.getStringExtra(Constants.INTENT_PARAM_ROOT_FILE_PATH); 
     Integer localTrackId=intent.getIntExtra(Constants.INTENT_PARAM_LOCAL_TRACK_ID, 0); 

     File output = new File(filePath+"/"+trackName); 
     if (output.exists()) { 
      result = Activity.RESULT_OK; 
      publishResults(output.getAbsolutePath(), result); 
     } 
     else { 

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

       URL url = new URL(serverUrl); 
       stream = url.openConnection().getInputStream(); 
       InputStreamReader reader = new InputStreamReader(stream); 
       fos = new FileOutputStream(output.getPath()); 
       int next = -1; 
       while ((next = reader.read()) != -1) { 
        fos.write(next); 
       } 
       // successfully finished 
       result = Activity.RESULT_OK; 


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

    private void publishResults(String outputPath, int result) { 
try { 
    FileInputStream fileInputStream = new FileInputStream(outputPath); 

     Intent intent = new Intent(NOTIFICATION); 
     intent.putExtra(FILEPATH, outputPath); 
     intent.putExtra(RESULT, result); 
     sendBroadcast(intent); 
}catch(Exception e){ 
    e.printStackTrace(); 
} 
    } 
} 

下載的廣播之後製作的,我嘗試下面的播放MP3文件代碼

if (trackPath != null) { 
       FileInputStream fileInputStream = new FileInputStream(trackPath); 
       mediaPlayer.setDataSource(fileInputStream.getFD()); 
      } else { 
       AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.spacer_audio); 
       mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); 
      } 
      mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); 
      mediaPlayer.setLooping(false); 
      mediaPlayer.prepare(); 
      mediaPlayer.setVolume(1f, 1f); 
      mediaPlayer.start(); 

我收到IOException異常從「mediaPlayer.prepare()」拋出

我試圖通過Android的默認音樂播放器播放下載的音樂文件,並顯示「C註釋播放此媒體「。

我試着將它複製到電腦上試試播放它,我發現原始音軌和下載的音軌有幾個KB的大小差異。

請幫我看看這個bug。

+0

絕對大小是問題...確保您已經下載完整的副本...我面臨同樣的問題... –

回答

2

您使用InputStreamReader來讀取二進制文件,它可能會產生一些意想不到的問題。我建議你改用BufferedInputStream

BufferedInputStream reader = new BufferedInputStream(stream); 
fos = new FileOutputStream(output.getPath()); 
int length = -1; 
byte[] buffer = new byte[1024 * 8]; 
while ((length = reader.read(buffer)) != -1) { 
    fos.write(buffer, 0, length); 
} 
+0

謝謝。你是對的。我在一個小時前找到了它。我會接受你的回答 – hybrid