0
우리는 백그라운드 서비스에서 내부 저장소에 MP3 파일을 다운로드해야하는 android 응용 프로그램을 만들고 있습니다. 아래에 언급 된 코드 스 니펫을 intent service
과 함께 사용하여 구현했습니다.하지만 플레이어가이 오디오 파일을 지원하지 않는 것처럼 오류가 발생하는 mp3를 재생하려고 할 때. 어떤 사람이이 문제에 대한 해결책을 가지고 있다면, 같은 것을 알려주십시오.mp3 파일 다운로드 및 재생
코드 조각 :
String urlPath = intent.getStringExtra(URL);
String fileName = intent.getStringExtra(FILENAME);
// String fileName = Environment.getExternalStorageDirectory().toString() + "/trail.mp3";
File output = new File(Environment.getExternalStorageDirectory(),
fileName);
File dir = new File(output.getAbsolutePath()
+ "/TRIALS/");
dir.mkdirs();
String path = dir + "/" +"trail" + ".mp3";
File f = new File(path);
if (f.exists()) {
f.delete();
}
InputStream stream = null;
FileOutputStream fos = null;
try {
URL url = new URL(urlPath);
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
//URL url = new URL(urlPath);
InputStream input = new BufferedInputStream(url.openStream());
stream = url.openConnection().getInputStream();
InputStreamReader reader = new InputStreamReader(stream);
fos = new FileOutputStream(f.getPath());
byte data[] = new byte[lenghtOfFile];
long total = 0;
int times = -1;
while ((times = reader.read()) != -1) {
fos.write(times);
}
// Successful finished
result = Activity.RESULT_OK;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
publishResults(output.getAbsolutePath(), result);
}