我试图从Dropbox下载一些音频文件供用户下次没有互联网时使用,所以代码实际上下载文件,但我有麻烦播放该音频我不知道如果我要解析下载的文件什么的,希望你能帮助Android下载mp3文件并播放它
下载文件,并发挥它的类,它发挥它在后执行,或至少它试图
class DownloadFileFromURL extends AsyncTask {
/**
* 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所以肯定有什么不对劲的下载
+0
你面临什么麻烦? pl发布日志,如果你有任何错误。 –