本文转自;http://www.cnblogs.com/over140/archive/2012/06/29/2561244.html
前言
本章实现视频下载的功能
使用说明:进入在线视频,点击播放时将弹出选择框询问播放还是下载,点击下载后进度条将在本地视频顶部显示。如果想边看便下载,请直接点击本地播放列表中正在下载的视频。
public class FileDownloadHelper { private static final String TAG = "FileDownloadHelper"; /** 线程池 */ private ThreadPool mPool = new ThreadPool(); /** 开始下载 */ public static final int MESSAGE_START = 0; /** 更新进度 */ public static final int MESSAGE_PROGRESS = 1; /** 下载结束 */ public static final int MESSAGE_STOP = 2; /** 下载出错 */ public static final int MESSAGE_ERROR = 3; /** 中途终止 */ private volatile boolean mIsStop = false; private Handler mHandler; public volatile HashMap<String, String> mDownloadUrls = new HashMap<String, String>(); public FileDownloadHelper(Handler handler) { if (handler == null) throw new IllegalArgumentException("handler不能为空!"); this.mHandler = handler; } public void stopALl() { mIsStop = true; mPool.stop(); } public void newDownloadFile(final String url) { newDownloadFile(url, Environment.getExternalStorageDirectory() + "/" + FileUtils.getUrlFileName(url)); } /** * 下载一个新的文件 * * @param url * @param savePath */ public void newDownloadFile(final String url, final String savePath) { if (mDownloadUrls.containsKey(url)) return; else mDownloadUrls.put(url, savePath); mPool.start(new Runnable() { @Override public void run() { mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_START, url)); HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(url); InputStream inputStream = null; FileOutputStream outputStream = null; try { HttpResponse response = client.execute(get); HttpEntity entity = response.getEntity(); final int size = (int) entity.getContentLength(); inputStream = entity.getContent(); if (size > 0 && inputStream != null) { outputStream = new FileOutputStream(savePath); int ch = -1; byte[] buf = new byte[1024]; //每秒更新一次进度 new Timer().schedule(new TimerTask() { @Override public void run() { try { FileInputStream fis = new FileInputStream(new File(savePath)); int downloadedSize = fis.available(); if (downloadedSize >= size) cancel(); mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_PROGRESS, downloadedSize, size, url)); } catch (Exception e) { } } }, 50, 1000); while ((ch = inputStream.read(buf)) != -1 && !mIsStop) { outputStream.write(buf, 0, ch); } outputStream.flush(); } } catch (Exception e) { Log.e(TAG, e.getMessage(), e); mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_ERROR, url + ":" + e.getMessage())); } finally { try { if (outputStream != null) outputStream.close(); } catch (IOException ex) { } try { if (inputStream != null) inputStream.close(); } catch (IOException ex) { } } mDownloadUrls.remove(url); mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_STOP, url)); } }); } }
代码说明:
Handle
public Handler mDownloadHandler = new Handler() { @Override public void handleMessage(Message msg) { PFile p; String url = msg.obj.toString(); switch (msg.what) { case FileDownloadHelper.MESSAGE_START://开始下载 p = new PFile(); p.path = mParent.mFileDownload.mDownloadUrls.get(url); p.title = new File(p.path).getName(); p.status = 0; p.file_size = 0; if (mDownloadAdapter == null) { mDownloadAdapter = new FileAdapter(getActivity(), new ArrayList<PFile>()); mDownloadAdapter.add(p, url); mTempListView.setAdapter(mDownloadAdapter); mTempListView.setVisibility(View.VISIBLE); } else { mDownloadAdapter.add(p, url); mDownloadAdapter.notifyDataSetChanged(); } break; case FileDownloadHelper.MESSAGE_PROGRESS://正在下载 p = mDownloadAdapter.getItem(url); p.temp_file_size = msg.arg1; p.file_size = msg.arg2; int status = (int) ((msg.arg1 * 1.0 / msg.arg2) * 10); if (status > 10) status = 10; p.status = status; mDownloadAdapter.notifyDataSetChanged(); break; case FileDownloadHelper.MESSAGE_STOP://下载结束 p = mDownloadAdapter.getItem(url); FileBusiness.insertFile(getActivity(), p); break; case FileDownloadHelper.MESSAGE_ERROR: Toast.makeText(getActivity(), url, Toast.LENGTH_LONG).show(); break; } super.handleMessage(msg); } }
代码说明:
Dialog
if (FileUtils.isVideoOrAudio(url)) { Dialog dialog = new AlertDialog.Builder(getActivity()).setIcon(android.R.drawable.btn_star).setTitle("播放/下载").setMessage(url).setPositiveButton("播放", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(getActivity(), VideoPlayerActivity.class); intent.putExtra("path", url); startActivity(intent); } }).setNeutralButton("下载", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { MainFragmentActivity activity = (MainFragmentActivity) getActivity(); activity.mFileDownload.newDownloadFile(url); Toast.makeText(getActivity(), "正在下载 .." + FileUtils.getUrlFileName(url) + " ,可从本地视频查看进度!", Toast.LENGTH_LONG).show(); } }).setNegativeButton("取消", null).create(); dialog.show(); return true; }
四、Vitamio公告
正式建立Vitamio开发者联盟QQ群!群号为:246969281
注意: 目前仅接受已经开发基于Vitamio产品的开发者申请加入 ,申请理由请填写产品的名词和链接,获取最新进展以及与Vitamio作者直接交流机会!