我試圖從Dropbox下載一些音頻文件供用戶下次沒有互聯網時使用,所以代碼實際上下載文件,但我有麻煩播放該音頻我不知道如果我要解析下載的文件什麼的,希望你能幫助Android下載mp3文件並播放它
下載文件,併發揮它的類,它發揮它在後執行,或至少它試圖
class DownloadFileFromURL extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Bar Dialog
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
/**
* Downloading file in background thread
*/
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// this will be useful so that you can show a tipical 0-100%
// progress bar
int lenghtOfFile = conection.getContentLength();
// download the file
InputStream input = new BufferedInputStream(url.openStream(),
8192);
// Output stream
OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory().getPath()+"/workout.mp3");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress("" + (int) ((total * 100)/lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
/**
* Updating progress bar
*/
protected void onProgressUpdate(String... progress) {
// setting progress percentage
pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
* After completing background task Dismiss the progress dialog
**/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
dismissDialog(progress_bar_type);
Uri u = Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/workout.mp3");
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/workout.mp3");
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
}
}
IM實際上得到的日誌
05-31 11:46:11.605 4638-4650/com.example.project.calisthenic E/MediaPlayer: error (1, -2147483648)
這個錯誤
問題是試圖播放下載的文件,即使嘗試播放它與Android本地播放器它說「播放器不支持這種類型的音頻文件」,所以我不知道如果我' m以錯誤的方式下載文件o嘗試以錯誤的方式播放文件。
我會檢查和文件大小爲10KB和下載後是400B所以肯定有什麼不對勁的下載
你面臨什麼麻煩? pl發佈日誌,如果你有任何錯誤。 – RAAAAM