最近在重構個人視頻播放器,項目有點點複雜,不可能全面的記錄
接下來,挑一些要點來記錄一下,上下文鋪設比較繁瑣,有興趣的能夠本系列源碼:githubgit
一些播放信息的記錄感受仍是放在數據庫裏好一些,否則感受很生硬
之前的SQLite介紹文章有點無病呻吟的感受,此次來實際用一下,相信感觸會更深
1.解決視頻播放量的記錄問題
2.解決視頻進入時恢復到上次播放進度
3.解決查詢最近播放的n條記錄的問題
4.解決查詢播放最多的n條記錄的問題
複製代碼
表字段
id 標識 主鍵,自增
path 視頻名稱 varchar(120) 惟一 非空
current_pos 當前播放進度 TINYINT 默認爲0
last_play_time 上次播放時間 CHAR(24) 2019-3-1 16:20:00
play_count 播放次數 INT 默認爲0
|--- 建表語句 -------------------------------
CREATE TABLE video_player (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
path VARCHAR(120) UNIQUE NOT NULL,
current_pos TINYINT NOT NULL DEFAULT 0,
last_play_time CHAR(24) NOT NULL,
play_count INT NOT NULL DEFAULT 0
);
複製代碼
/**
* 做者:張風捷特烈<br/>
* 時間:2019/4/4/004:13:19<br/>
* 郵箱:1981462002@qq.com<br/>
* 說明:數據庫輔助類
*/
public class VideoDatabaseHelper extends SQLiteOpenHelper {
private static String DATABASE_NAME = "i_video.db";//數據庫名
private static int DATABASE_VERSION = 1;//數據庫版本
public VideoDatabaseHelper(@Nullable Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
createSwordTable(db);
}
private void createSwordTable(SQLiteDatabase db) {
db.execSQL("CREATE TABLE video_player (\n" +
"id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n" +
"path VARCHAR(120) UNIQUE NOT NULL,\n" +
"current_pos TINYINT NOT NULL DEFAULT 0,\n" +
"last_play_time CHAR(24) NOT NULL,\n" +
"play_count INT NOT NULL DEFAULT 0\n" +
"); ");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
複製代碼
/**
* 做者:張風捷特烈<br></br>
* 時間:2019/4/4/004:13:29<br></br>
* 郵箱:1981462002@qq.com<br></br>
* 說明:視屏播放實體類
*/
class VideoBean {
var id: Int = 0
var path: String = ""
var current_pos: Int = 0
var last_play_time: String = ""
var play_count: Int = 1
}
複製代碼
用個單例來獲取VideoDao方便操做github
/**
* 做者:張風捷特烈<br/>
* 時間:2019/4/4/004:13:26<br/>
* 郵箱:1981462002@qq.com<br/>
* 說明:數據庫操做層
*/
public class VideoDao {
private static VideoDao sVideoDao;
private SQLiteOpenHelper mHelper;
public void setHelper(SQLiteOpenHelper helper) {
mHelper = helper;
}
private VideoDao() {
}
public static VideoDao newInstance() {
if (sVideoDao == null) {
synchronized (VideoDao.class) {
if (sVideoDao == null) {
sVideoDao = new VideoDao();
}
}
}
return sVideoDao;
}
/**
* 插入
*
* @param video
*/
public void insert(VideoBean video) {
if (contains(video.getPath())) {
addPlayCount(video.getPath());
} else {
mHelper.getWritableDatabase().execSQL(
"INSERT INTO video_player(path,current_pos,last_play_time,play_count) VALUES(?,?,?,?)",
new String[]{
video.getPath(),
video.getCurrent_pos() + "",
video.getLast_play_time(),
video.getPlay_count() + ""});
}
}
/**
* 將某視頻播放量+1,並更新時間
*
* @param path 視頻路徑
*/
private void addPlayCount(String path) {
int count = getPlayCount(path);
count++;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
String now = format.format(System.currentTimeMillis());
mHelper.getWritableDatabase().execSQL(
"UPDATE video_player SET play_count=? , last_play_time=?",
new String[]{count + "", now});
}
/**
* 根據路徑獲取播放量
*
* @param path 視頻路徑
* @return 播放量
*/
private int getPlayCount(String path) {
int result = 0;
Cursor cursor = mHelper.getReadableDatabase().
rawQuery("SELECT play_count FROM video_player WHERE path=?", new String[]{path});
if (cursor.moveToNext()) {
result = cursor.getInt(cursor.getColumnIndex("play_count"));
}
cursor.close();
return result;
}
/**
* 檢測是否包含某視頻
*
* @param path 視頻路徑
* @return 否包含某視頻
*/
public boolean contains(String path) {
Cursor cursor = mHelper.getReadableDatabase().
rawQuery("SELECT path FROM video_player WHERE path=?", new String[]{path});
boolean has = cursor.moveToNext();
cursor.close();
return has;
}
}
複製代碼
視屏播放器功能由VideoView實現,我上面封了一層VideoPlayerManager用來管理
在每次設置播放資源時插入數據,上面的插入方法在已經有值時,播放次數會 + 1數據庫
|--- 在每次設置播放資源時插入 -------------------------------
VideoBean videoBean = new VideoBean();
videoBean.setPath(info.getDataUrl());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA);
videoBean.setLast_play_time(format.format(System.currentTimeMillis()));
VideoDao.newInstance().insert(videoBean);
複製代碼
注意點擊先後的播放量數字bash
核心在於暫停時保存進度,在恰當的時機進行 seekTo 和界面數據回顯及渲染
使用MVP來解耦很方便,Presenter中獲取數據庫進度,順便seekTo,
再將進度數據設置給Model,調用View的render() 方法進行渲染ide
---->[VideoView#pause]------------------------------
@Override
public void pause() {
saveProgress();//保存進度
if (canPlay() && mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
}
}
private void saveProgress() {
int per = (int) (getCurrentPosition() * 1.f / getDuration() * 100);
VideoDao.newInstance().saveProgress(mUri.getPath(), per);
}
---->[VideoDao#saveProgress]------------------------------
/**
* 保存播放進度
*/
public void saveProgress(String path, int per) {
if (contains(path)) {
mHelper.getWritableDatabase().execSQL(
"UPDATE video_player SET current_pos=? WHERE path =?",
new String[]{per + "", path});
}
}
---->[VideoDao#getProgress]------------------------------
/**
* 根據路徑獲取播放進度
*
* @param path 視頻路徑
* @return 播放進度
*/
public int getProgress(String path) {
int result = 0;
Cursor cursor = mHelper.getReadableDatabase().
rawQuery("SELECT current_pos FROM video_player WHERE path=?", new String[]{path});
if (cursor.moveToNext()) {
result = cursor.getInt(cursor.getColumnIndex("current_pos"));
}
cursor.close();
return result;
}
複製代碼
/**
* 獲取最近播放的記錄
*
* @param count 條數
* @return 最近播放的count條記錄
*/
public String[] getRecent(int count) {
String[] strings = new String[count];
Cursor cursor = mHelper.getReadableDatabase().
rawQuery("SELECT path FROM video_player ORDER BY last_play_time DESC LIMIT ?",
new String[]{count + ""});
int i = 0;
while (cursor.moveToNext()) {
String path = cursor.getString(cursor.getColumnIndex("path"));
strings[i] = path;
i++;
}
cursor.close();
return strings;
}
|--- 使用 ---------------------------------
VideoDao.newInstance().getRecent(5);
複製代碼
/**
* 獲取播放最多的n條記錄
*
* @param count 條數
* @return 獲取播放最多的n條記錄
*/
public String[] getMost(int count) {
String[] strings = new String[count];
Cursor cursor = mHelper.getReadableDatabase().
rawQuery("SELECT path FROM video_player ORDER BY play_count DESC LIMIT ?",
new String[]{count + ""});
int i = 0;
while (cursor.moveToNext()) {
String path = cursor.getString(cursor.getColumnIndex("path"));
strings[i] = path;
i++;
}
cursor.close();
return strings;
}
複製代碼
小插曲:很詭異的一件事,我視圖將這兩個方法封裝成一個ui
|--- 一開始我是這樣的 ---------------------
/**
* 獲取最近播放的記錄
*
* @param count 條數
* @return 最近播放的count條記錄
*/
public String[] getLimit(String by, int count) {
String[] strings = new String[count];
Cursor cursor = mHelper.getReadableDatabase().
rawQuery("SELECT path FROM video_player ORDER BY ? DESC LIMIT ?",
new String[]{by,count + ""});
int i = 0;
while (cursor.moveToNext()) {
String path = cursor.getString(cursor.getColumnIndex("path"));
strings[i] = path;
i++;
}
cursor.close();
return strings;
}
|--- 而後怎麼都搞不出想要的結果,鬱悶... 這裏說一下,問號只能用來傳值,其餘的能夠拼接字符串
/**
* 獲取最近播放的記錄
*
* @param count 條數
* @return 最近播放的count條記錄
*/
public String[] getLimit(String by, int count) {
String[] strings = new String[count];
Cursor cursor = mHelper.getReadableDatabase().
rawQuery("SELECT path FROM video_player ORDER BY " + by + " DESC LIMIT ?",
new String[]{count + ""});
int i = 0;
while (cursor.moveToNext()) {
String path = cursor.getString(cursor.getColumnIndex("path"));
strings[i] = path;
i++;
}
cursor.close();
return strings;
}
/**
* 獲取播放最多的n條記錄
*
* @param count 條數
* @return 獲取播放最多的n條記錄
*/
public String[] getMost(int count) {
return getLimit("play_count", 3);
}
/**
* 獲取最近播放的記錄
*
* @param count 條數
* @return 最近播放的count條記錄
*/
public String[] getRecent(int count) {
return getLimit("last_play_time", count);
}
複製代碼
想增長其餘的記錄,能夠本身擴展。Over 本篇記錄 就這樣。spa