Android 使用Vitamio打造自己的萬(wàn)能播放器(7)——在線播放(下載視頻)
前言
本章將實(shí)現(xiàn)非常實(shí)用的功能——下載在線視頻。涉及到多線程、線程更新UI等技術(shù),還需思考產(chǎn)品的設(shè)計(jì),如何將新加的功能更好的融入到現(xiàn)有的產(chǎn)品中,并不是簡(jiǎn)單的加一個(gè)界面就行了,歡迎大家交流產(chǎn)品設(shè)計(jì)和技術(shù)細(xì)節(jié)實(shí)現(xiàn)!
系列
1、Android 使用Vitamio打造自己的萬(wàn)能播放器(1)——準(zhǔn)備
2、Android 使用Vitamio打造自己的萬(wàn)能播放器(2)—— 手勢(shì)控制亮度、音量、縮放
3、Android 使用Vitamio打造自己的萬(wàn)能播放器(3)——本地播放(主界面、視頻列表)
4、Android 使用Vitamio打造自己的萬(wàn)能播放器(4)——本地播放(快捷搜索、數(shù)據(jù)存儲(chǔ))
5、Android 使用Vitamio打造自己的萬(wàn)能播放器(5)——在線播放(播放優(yōu)酷視頻)
6、Android 使用Vitamio打造自己的萬(wàn)能播放器(6)——在線播放(播放列表)
正文
一、目標(biāo)
本章實(shí)現(xiàn)視頻下載的功能


使用說(shuō)明:進(jìn)入在線視頻,點(diǎn)擊播放時(shí)將彈出選擇框詢問(wèn)播放還是下載,點(diǎn)擊下載后進(jìn)度條將在本地視頻頂部顯示。如果想邊看便下載,請(qǐng)直接點(diǎn)擊本地播放列表中正在下載的視頻。
二、實(shí)現(xiàn)(部分主要實(shí)現(xiàn)代碼)
FileDownloadHelper
public class FileDownloadHelper {
private static final String TAG = "FileDownloadHelper";
/** 線程池 */
private ThreadPool mPool = new ThreadPool();
/** 開始下載 */
public static final int MESSAGE_START = 0;
/** 更新進(jìn)度 */
public static final int MESSAGE_PROGRESS = 1;
/** 下載結(jié)束 */
public static final int MESSAGE_STOP = 2;
/** 下載出錯(cuò) */
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));
}
/**
* 下載一個(gè)新的文件
*
* @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];
//每秒更新一次進(jìn)度
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));
}
});
}
}
代碼說(shuō)明:
a. ThreadPool是線程池,請(qǐng)參照項(xiàng)目代碼。
b. 這里使用了Time定時(shí)來(lái)刷進(jìn)度,而沒(méi)有直接在write數(shù)據(jù)時(shí)更新進(jìn)度,這樣的原因時(shí)每秒write較高,更新UI過(guò)于頻繁,可能導(dǎo)致超時(shí)等問(wèn)題?! ?nbsp;
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://下載結(jié)束
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);
}
};
代碼說(shuō)明:
a. mTempListView是新增的,默認(rèn)是隱藏,請(qǐng)參見項(xiàng)目代碼layout部分。
b. 下載流程:開始(顯示mTempListView) -> 正在下載(更新進(jìn)度圖片和大小) -> 完成(入褲)
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) + " ,可從本地視頻查看進(jìn)度!", Toast.LENGTH_LONG).show();
}
}).setNegativeButton("取消", null).create();
dialog.show();
return true;
}
三、下載
至本章節(jié)往后,代碼均不再提供下載,請(qǐng)移步Google Code:
http://code.google.com/p/android-oplayer
以上就是對(duì)Android Vitamio 開發(fā)播放器下載視頻播放器的資料整理,有需要開發(fā)Android播放器的朋友可以參考下。
- Android GSYVideoPlayer視頻播放器功能的實(shí)現(xiàn)
- Android項(xiàng)目實(shí)現(xiàn)視頻播放器
- Android使用vitamio插件實(shí)現(xiàn)視頻播放器
- Android基于IJKPlayer視頻播放器簡(jiǎn)單封裝設(shè)計(jì)
- Android視頻播放器屏幕左側(cè)邊隨手指上下滑動(dòng)亮度調(diào)節(jié)功能的原理實(shí)現(xiàn)
- Android多媒體之VideoView視頻播放器
- android webvie指定視頻播放器播放網(wǎng)站視頻
- Android 使用Vitamio打造自己的萬(wàn)能播放器(10)—— 本地播放 (縮略圖、視頻信息、視頻掃描服務(wù))
- Android 使用Vitamio打造自己的萬(wàn)能播放器(5)——在線播放(播放優(yōu)酷視頻)
- Android UniversalVideoView實(shí)現(xiàn)視頻播放器
相關(guān)文章
Android Studio實(shí)現(xiàn)帶邊框的圓形頭像
這篇文章主要為大家詳細(xì)介紹了Android Studio實(shí)現(xiàn)帶邊框的圓形頭像,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Flutter 包管理器和資源管理使用學(xué)習(xí)
這篇文章主要為大家介紹了Flutter 包管理器和資源管理使用學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android定時(shí)器實(shí)現(xiàn)的幾種方式整理及removeCallbacks失效問(wèn)題解決
本文為大家詳細(xì)介紹下Android 定時(shí)器實(shí)現(xiàn)的幾種方式:Handler + Runnable、Timer的方式、Handle與線程的sleep(long )方法和removeCallbacks失效問(wèn)題如何解決2013-06-06
通過(guò)WIFI(不用數(shù)據(jù)線)連接Android手機(jī)調(diào)試
本文主要介紹WIFI 鏈接手機(jī)調(diào)試,這里詳細(xì)介紹了WIFI 鏈接Android手機(jī)實(shí)現(xiàn)調(diào)試的過(guò)程,有需要的小伙伴可以參考下2016-08-08
Android BroadcastReceiver傳輸機(jī)制詳解
Android開發(fā)的四大組件分別是:活動(dòng)(activity),用于表現(xiàn)功能;服務(wù)(service),后臺(tái)運(yùn)行服務(wù),不提供界面呈現(xiàn);廣播接受者(Broadcast Receive),勇于接收廣播;內(nèi)容提供者(Content Provider),支持多個(gè)應(yīng)用中存儲(chǔ)和讀取數(shù)據(jù),相當(dāng)于數(shù)據(jù)庫(kù),本篇著重介紹廣播組件2023-01-01
Android將內(nèi)容分享到QQ和微信實(shí)例代碼
這篇文章主要介紹了Android將內(nèi)容分享到QQ和微信實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06
android編程判斷應(yīng)用是否具有某個(gè)權(quán)限的方法
這篇文章主要介紹了android編程判斷應(yīng)用是否具有某個(gè)權(quán)限的方法,涉及Android進(jìn)程操作及權(quán)限控制的相關(guān)使用技巧,需要的朋友可以參考下2015-10-10

