Android 使用Vitamio打造自己的萬(wàn)能播放器(4)——本地播放(快捷搜索、數(shù)據(jù)存儲(chǔ))
前言
關(guān)鍵字:Vitamio、VPlayer、Android播放器、Android影音、Android開(kāi)源播放器
本章節(jié)把Android萬(wàn)能播放器本地播放的主要功能(緩存播放列表和A-Z快速查詢(xún)功能)完成,和播放組件關(guān)系不大,但用到一些實(shí)用的技術(shù),歡迎交流!
系列
1、Android 使用Vitamio打造自己的萬(wàn)能播放器(1)——準(zhǔn)備
2、Android 使用Vitamio打造自己的萬(wàn)能播放器(2)—— 手勢(shì)控制亮度、音量、縮放
3、Android 使用Vitamio打造自己的萬(wàn)能播放器(3)——本地播放(主界面、播放列表)
正文
一、目標(biāo)
1.1 A-Z快速切換查找影片
把手機(jī)上的聯(lián)系人上的A-Z快速查找運(yùn)用到了這里,查找文件更便捷。這也是"學(xué)"的米聊的 :)
1.2 緩存掃描視頻列表
首次使用掃描SD卡一遍,以后就從數(shù)據(jù)庫(kù)讀取了,下篇文章再加一個(gè)監(jiān)聽(tīng)即可。
1.3 截圖

二、實(shí)現(xiàn)
核心代碼:
public class FragmentFile extends FragmentBase implements OnItemClickListener {
private FileAdapter mAdapter;
private TextView first_letter_overlay;
private ImageView alphabet_scroller;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = super.onCreateView(inflater, container, savedInstanceState);
// ~~~~~~~~~ 綁定控件
first_letter_overlay = (TextView) v.findViewById(R.id.first_letter_overlay);
alphabet_scroller = (ImageView) v.findViewById(R.id.alphabet_scroller);
// ~~~~~~~~~ 綁定事件
alphabet_scroller.setClickable(true);
alphabet_scroller.setOnTouchListener(asOnTouch);
mListView.setOnItemClickListener(this);
// ~~~~~~~~~ 加載數(shù)據(jù)
if (new SQLiteHelper(getActivity()).isEmpty())
new ScanVideoTask().execute();
else
new DataTask().execute();
return v;
}
/** 單擊啟動(dòng)播放 */
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final PFile f = mAdapter.getItem(position);
Intent intent = new Intent(getActivity(), VideoViewDemo.class);
intent.putExtra("path", f.path);
startActivity(intent);
}
private class DataTask extends AsyncTask<Void, Void, ArrayList<PFile>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mLoadingLayout.setVisibility(View.VISIBLE);
mListView.setVisibility(View.GONE);
}
@Override
protected ArrayList<PFile> doInBackground(Void... params) {
return FileBusiness.getAllSortFiles(getActivity());
}
@Override
protected void onPostExecute(ArrayList<PFile> result) {
super.onPostExecute(result);
mAdapter = new FileAdapter(getActivity(), FileBusiness.getAllSortFiles(getActivity()));
mListView.setAdapter(mAdapter);
mLoadingLayout.setVisibility(View.GONE);
mListView.setVisibility(View.VISIBLE);
}
}
/** 掃描SD卡 */
private class ScanVideoTask extends AsyncTask<Void, File, ArrayList<PFile>> {
private ProgressDialog pd;
private ArrayList<File> files = new ArrayList<File>();
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(getActivity());
pd.setMessage("正在掃描視頻文件...");
pd.show();
}
@Override
protected ArrayList<PFile> doInBackground(Void... params) {
// ~~~ 遍歷文件夾
eachAllMedias(Environment.getExternalStorageDirectory());
// ~~~ 入庫(kù)
SQLiteHelper sqlite = new SQLiteHelper(getActivity());
SQLiteDatabase db = sqlite.getWritableDatabase();
try {
db.beginTransaction();
SQLiteStatement stat = db.compileStatement("INSERT INTO files(" + FilesColumns.COL_TITLE + "," + FilesColumns.COL_TITLE_PINYIN + "," + FilesColumns.COL_PATH + "," + FilesColumns.COL_LAST_ACCESS_TIME + ") VALUES(?,?,?,?)");
for (File f : files) {
String name = FileUtils.getFileNameNoEx(f.getName());
int index = 1;
stat.bindString(index++, name);//title
stat.bindString(index++, PinyinUtils.chineneToSpell(name));//title_pinyin
stat.bindString(index++, f.getPath());//path
stat.bindLong(index++, System.currentTimeMillis());//last_access_time
stat.execute();
}
db.setTransactionSuccessful();
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
db.endTransaction();
db.close();
}
// ~~~ 查詢(xún)數(shù)據(jù)
return FileBusiness.getAllSortFiles(getActivity());
}
@Override
protected void onProgressUpdate(final File... values) {
File f = values[0];
files.add(f);
pd.setMessage(f.getName());
}
/** 遍歷所有文件夾,查找出視頻文件 */
public void eachAllMedias(File f) {
if (f != null && f.exists() && f.isDirectory()) {
File[] files = f.listFiles();
if (files != null) {
for (File file : f.listFiles()) {
if (file.isDirectory()) {
eachAllMedias(file);
} else if (file.exists() && file.canRead() && FileUtils.isVideoOrAudio(file)) {
publishProgress(file);
}
}
}
}
}
@Override
protected void onPostExecute(ArrayList<PFile> result) {
super.onPostExecute(result);
mAdapter = new FileAdapter(getActivity(), result);
mListView.setAdapter(mAdapter);
pd.dismiss();
}
}
private class FileAdapter extends ArrayAdapter<PFile> {
public FileAdapter(Context ctx, ArrayList<PFile> l) {
super(ctx, l);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final PFile f = getItem(position);
if (convertView == null) {
final LayoutInflater mInflater = getActivity().getLayoutInflater();
convertView = mInflater.inflate(R.layout.fragment_file_item, null);
}
((TextView) convertView.findViewById(R.id.title)).setText(f.title);
return convertView;
}
}
/**
* A-Z
*/
private OnTouchListener asOnTouch = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:// 0
alphabet_scroller.setPressed(true);
first_letter_overlay.setVisibility(View.VISIBLE);
mathScrollerPosition(event.getY());
break;
case MotionEvent.ACTION_UP:// 1
alphabet_scroller.setPressed(false);
first_letter_overlay.setVisibility(View.GONE);
break;
case MotionEvent.ACTION_MOVE:
mathScrollerPosition(event.getY());
break;
}
return false;
}
};
/**
* 顯示字符
*
* @param y
*/
private void mathScrollerPosition(float y) {
int height = alphabet_scroller.getHeight();
float charHeight = height / 28.0f;
char c = 'A';
if (y < 0)
y = 0;
else if (y > height)
y = height;
int index = (int) (y / charHeight) - 1;
if (index < 0)
index = 0;
else if (index > 25)
index = 25;
String key = String.valueOf((char) (c + index));
first_letter_overlay.setText(key);
int position = 0;
if (index == 0)
mListView.setSelection(0);
else if (index == 25)
mListView.setSelection(mAdapter.getCount() - 1);
else {
for (PFile item : mAdapter.getAll()) {
if (item.title_pinyin.startsWith(key)) {
mListView.setSelection(position);
break;
}
position++;
}
}
}
代碼說(shuō)明:
代碼是基于上篇文章,新增了播放列表緩存功能以及快速查找功能。
a). 使用了pinyin4j開(kāi)源項(xiàng)目,用于提取文件名中的漢字的拼音,以便能夠。
b). A-Z這部分的代碼也是通過(guò)反編譯參考米聊的,比較有實(shí)用價(jià)值
c). 入庫(kù)部分使用了事務(wù)
其他代碼請(qǐng)參見(jiàn)項(xiàng)目代碼。
注意:
由于是示例代碼,考慮不盡周全,可能在后續(xù)章節(jié)中補(bǔ)充,請(qǐng)大家注意不要直接使用代碼!例如應(yīng)該檢查一下SD卡是否可用等問(wèn)題。
三、Vtamio與VPlayer
Vitamio:http://vov.io
VPlayer:http://vplayer.net (使用Vitamio最成功的產(chǎn)品,用戶(hù)超過(guò)500萬(wàn))
以上就是對(duì)Android Vitamio 本地播放功能(快捷搜索,數(shù)據(jù)存儲(chǔ))資料整理,有開(kāi)發(fā)Android播放器的朋友可以參考下。
- Android 使用Vitamio打造自己的萬(wàn)能播放器(10)—— 本地播放 (縮略圖、視頻信息、視頻掃描服務(wù))
- Android 使用Vitamio打造自己的萬(wàn)能播放器(9)—— 在線播放 (在線電視)
- Android 使用Vitamio打造自己的萬(wàn)能播放器(8)——細(xì)節(jié)優(yōu)化
- Android 使用Vitamio打造自己的萬(wàn)能播放器(7)——在線播放(下載視頻)
- Android 使用Vitamio打造自己的萬(wàn)能播放器(6)——在線播放(播放列表)
- Android 使用Vitamio打造自己的萬(wàn)能播放器(5)——在線播放(播放優(yōu)酷視頻)
- Android 使用Vitamio打造自己的萬(wàn)能播放器(3)——本地播放(主界面、播放列表)
- Android 使用Vitamio打造自己的萬(wàn)能播放器(2)—— 手勢(shì)控制亮度、音量、縮放
- Android 使用Vitamio打造自己的萬(wàn)能播放器(1)——準(zhǔn)備
- Android使用vitamio插件實(shí)現(xiàn)視頻播放器
相關(guān)文章
Flutter進(jìn)階之實(shí)現(xiàn)動(dòng)畫(huà)效果(四)
這篇文章主要為大家詳細(xì)介紹了Flutter進(jìn)階之實(shí)現(xiàn)動(dòng)畫(huà)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Android RecyclerView添加頭部和底部的方法
這篇文章主要為大家詳細(xì)介紹了Android RecyclerView添加頭部和底部的方法,感興趣的小伙伴們可以參考一下2016-05-05
Android簡(jiǎn)易圖片瀏覽器的實(shí)現(xiàn)
最近做了一個(gè)圖片瀏覽小程序,本文主要介紹了Android簡(jiǎn)易圖片瀏覽器的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03
Android進(jìn)階Handler應(yīng)用線上卡頓監(jiān)控詳解
這篇文章主要為大家介紹了Android進(jìn)階Handler應(yīng)用線上卡頓監(jiān)控詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Android UI設(shè)計(jì)系列之自定義Dialog實(shí)現(xiàn)各種風(fēng)格的對(duì)話(huà)框效果(7)
這篇文章主要介紹了Android UI設(shè)計(jì)系列之自定義Dialog實(shí)現(xiàn)各種風(fēng)格的對(duì)話(huà)框效果,具有一定的實(shí)用性和參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06

