我使用以下代码从我的服务器下载mp3文件到android
public class DownloadService extends IntentService {
private int result = Activity.RESULT_CANCELED;
public static final String RESULT = "result";
public static final String NOTIFICATION = "!@#$%%^";
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();
我从“mediaPlayer.prepare()”
中抛出IOException我尝试通过Android默认音乐播放器播放下载的音乐文件,并显示“无法播放此媒体”。
我尝试将其复制到计算机上尝试播放它,我注意到原始曲目和下载曲目之间存在几KB的大小差异。
请帮我找到错误。
答案 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);
}