2016-04-09 188 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