我尝试使用AsyncTask下载远程音频文件,该版本适用于this,但不适用于this网址。
当我尝试调试它时说"断点到达线程主"当代码到达这一行时。
InputStream inputStream = connection.getInputStream();
当我使用我之前提到的第一个网址时,代码工作正常,这让我不确定到底出了什么问题。
这里是完整的代码:
@Override
protected String doInBackground(String... url) {
MediaPlayer mp = new MediaPlayer();
File newAudioFile = null;
File oldAudioFile = null;
try {
//String remoteURL = url[0];
Log.i(TAG, " url in AsyncTast= " + url[0]);
IdHasher idHasher = new IdHasher();
String fileName = idHasher.getHashedId(url[0]); // get hashed name of file from URL.
// define path for new directory in DIRECTORY_DOWNLOADS folder.
File localFileDirectory = new File(String.valueOf(downloadDirectoryPath), audioCacheDirectory);
if (!localFileDirectory.exists()) { // make directory if it doesn't exist.
localFileDirectory.mkdirs();
Log.i(TAG, " directories made");
}
oldAudioFile = new File(localFileDirectory, fileName + ".mp3");
if (!oldAudioFile.isFile()) { // if file doesn't exist download it.
Log.i(TAG, " new file downloaded");
// download remote file
URL remoteURL = new URL(url[0]);
connection = (HttpURLConnection) remoteURL.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("GET");
connection.connect();
double remoteFileSize = connection.getContentLength();
double freeMemoryOnPhone = checkFreeMemoryOnPhone();
Log.i(TAG, " remote file size = " + ((remoteFileSize / 1024) / 1024) + " mb");
Log.i(TAG, " free memory on phone = " + ((freeMemoryOnPhone / 1024) / 1024) + " mb");
try {
newAudioFile = new File(localFileDirectory, fileName + ".mp3");// add extension mp3 to filename.
newAudioFile.getParentFile().mkdirs();
newAudioFile.createNewFile();
} catch (Exception e) {
Log.i(TAG, "error creating file" + e.getMessage());
}
if (remoteFileSize <= freeMemoryOnPhone) { // download remote file only if there is enough space on available on the phone.
if (!localFileDirectory.exists()) { // make directory if it doesn't exist.
localFileDirectory.mkdirs();
Log.i(TAG, " directories made");
}
InputStream inputStream = connection.getInputStream();
OutputStream outputStream = new FileOutputStream(newAudioFile);
byte[] buffer = new byte[1024];
int bytecount = 0;
while ((bytecount = inputStream.read(buffer)) > 0) { // read audio file from input stream and write it on phone memory.
outputStream.write(buffer, 0, bytecount);
}
outputStream.flush();
outputStream.close();
inputStream.close();
Log.i(TAG, " File " + fileName + ".mp3 downloaded");
Log.i(TAG, " playAudioFile called");
// mp = MediaPlayer.create(context, Uri.fromFile(newAudioFile));
} else {
Log.i(TAG, " not enough memory on phone to download remote audio file");
}
} else { // if file exists, play it
Log.i(TAG, "file " + fileName + ".mp3 already exits");
Log.i(TAG, "old file passed");
//mp = MediaPlayer.create(context, Uri.fromFile(oldAudioFile));
}
} catch (Exception e) {
Log.i(TAG, e.getMessage());
}
if (!(newAudioFile == null)) {
return String.valueOf(newAudioFile.getAbsoluteFile());
} else {
return String.valueOf(oldAudioFile.getAbsoluteFile());
}// mp.start();// play audio file
// return String.valueOf(mp);
}
注意:我从服务启动AsyncTask。该服务调用一些最终调用AsyncTask的函数。