天天看點

使用Vitamio打造自己的Android萬能播放器(4)——本地播放(快捷搜尋、資料存儲)

前言

  關鍵字:vitamio、vplayer、android播放器、android影音、android開源播放器

  本章節把android萬能播放器本地播放的主要功能(緩存播放清單和a-z快速查詢功能)完成,和播放元件關系不大,但用到一些實用的技術,歡迎交流!

聲明

  歡迎轉載,但請保留文章原始出處:) 

    部落格園:http://www.cnblogs.com

    農民伯伯: http://over140.cnblogs.com 

系列

正文

  一、目标

    1.1  a-z快速切換查找影片

把手機上的聯系人上的a-z快速查找運用到了這裡,查找檔案更便捷。這也是"學"的米聊的 :)

    1.2  緩存掃描視訊清單

首次使用掃描sd卡一遍,以後就從資料庫讀取了,下篇文章再加一個監聽即可。

    1.3      截圖 

         

使用Vitamio打造自己的Android萬能播放器(4)——本地播放(快捷搜尋、資料存儲)

  二、實作

核心代碼:

使用Vitamio打造自己的Android萬能播放器(4)——本地播放(快捷搜尋、資料存儲)

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);

        // ~~~~~~~~~ 加載資料

        if (new sqlitehelper(getactivity()).isempty())

            new scanvideotask().execute();

        else

            new datatask().execute();

        return v;

    }

    /** 單擊啟動播放 */

    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);

        }

        protected arraylist<pfile> doinbackground(void... params) {

            return filebusiness.getallsortfiles(getactivity());

        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>();

            pd = new progressdialog(getactivity());

            pd.setmessage("正在掃描視訊檔案...");

            pd.show();

            // ~~~ 周遊檔案夾

            eachallmedias(environment.getexternalstoragedirectory());

            // ~~~ 入庫

            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) {

            } finally {

                db.endtransaction();

                db.close();

            }

            // ~~~ 查詢資料

        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);

                        }

                    }

            madapter = new fileadapter(getactivity(), result);

            pd.dismiss();

    private class fileadapter extends arrayadapter<pfile> {

        public fileadapter(context ctx, arraylist<pfile> l) {

            super(ctx, l);

        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() {

        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);

            case motionevent.action_move:

            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++;

使用Vitamio打造自己的Android萬能播放器(4)——本地播放(快捷搜尋、資料存儲)

 代碼說明:

        代碼是基于上篇文章,新增了播放清單緩存功能以及快速查找功能。

a).  使用了pinyin4j開源項目,用于提取檔案名中的漢字的拼音,以便能夠。

b).  a-z這部分的代碼也是通過反編譯參考米聊的,比較有實用價值

c).  入庫部分使用了事務

其他代碼請參見項目代碼。 

注意:由于是示例代碼,考慮不盡周全,可能在後續章節中補充,請大家注意不要直接使用代碼!例如應該檢查一下sd卡是否可用等問題。 

  三、項目下載下傳

  四、vtamio與vplayer

結束

這周釋出新的版本稍忙,拖到周五才寫這篇文章,總算沒有食言 :) 功能越做越細,也會盡量往一個正式的産品方向靠,盡可能的提供有用的代碼。

轉載:http://www.cnblogs.com/over140/archive/2012/06/08/2541452.html

繼續閱讀