使用Vitamio打造自己的Android万能播放器(7)——在线播放(下载视频)
前言
4、使用Vitamio打造自己的Android万能播放器(4)——本地播放(快捷搜索、数据存储)
5、使用Vitamio打造自己的Android万能播放器(5)——在线播放(播放优酷视频)
本章实现视频下载的功能
使用说明:进入在线视频,点击播放时将弹出选择框询问播放还是下载,点击下载后进度条将在本地视频顶部显示。如果想边看便下载,请直接点击本地播放列表中正在下载的视频。
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));
}
});
}
}
代码说明:
b. 这里使用了Time定时来刷进度,而没有直接在write数据时更新进度,这样的原因时每秒write较高,更新UI过于频繁,可能导致超时等问题。
Handle
@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);
}
};
代码说明:
b. 下载流程:开始(显示mTempListView) -> 正在下载(更新进度图片和大小) -> 完成(入裤)
Dialog
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;
}
至本章节往后,代码均不再提供下载,请移步Google Code:
http://code.google.com/p/android-oplayer
四、Vitamio公告
正式建立Vitamio开发者联盟QQ群!群号为:246969281
注意:目前仅接受已经开发基于Vitamio产品的开发者申请加入,申请理由请填写产品的名词和链接,获取最新进展以及与Vitamio作者直接交流机会!
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。