前言

  关键字:Vitamio、VPlayer、Android播放器、Android影音、Android开源播放器

  本章节把Android万能播放器本地播放的主要功能(缓存播放列表和A-Z快速查询功能)完成,和播放组件关系不大,但用到一些实用的技术,欢迎交流!

声明
  欢迎转载,但请保留文章原始出处:) 
    博客园:http://www.cnblogs.com

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

系列

  1、使用Vitamio打造自己的Android万能播放器(1)——准备

  2、使用Vitamio打造自己的Android万能播放器(2)—— 手势控制亮度、音量、缩放

  3、使用Vitamio打造自己的Android万能播放器(3)——本地播放(主界面、播放列表)

正文

  一、目标

    1.1  A-Z快速切换查找影片

把手机上的联系人上的A-Z快速查找运用到了这里,查找文件更便捷。这也是"学"的米聊的 :)

    1.2  缓存扫描视频列表

首次使用扫描SD卡一遍,以后就从数据库读取了,下篇文章再加一个监听即可。

    1.3      截图

         

  二、实现

核心代码:


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;
    }     /** 单击启动播放 */
    @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());             // ~~~ 入库
            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();
            }             // ~~~ 查询数据
            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++;
            }
        }
    }

}

代码说明:

代码是基于上篇文章,新增了播放列表缓存功能以及快速查找功能。

a).  使用了pinyin4j开源项目,用于提取文件名中的汉字的拼音,以便能够。

b).  A-Z这部分的代码也是通过反编译参考米聊的,比较有实用价值

c).  入库部分使用了事务

其他代码请参见项目代码。

注意:由于是示例代码,考虑不尽周全,可能在后续章节中补充,请大家注意不要直接使用代码!例如应该检查一下SD卡是否可用等问题。

  三、项目下载

Vitamio-Demo2012-6-8.zip

  四、Vtamio与VPlayer

Vitamio:http://vov.io 
  VPlayer:http://vplayer.net(使用Vitamio最成功的产品,用户超过500万)

结束

这周发布新的版本稍忙,拖到周五才写这篇文章,总算没有食言 :) 功能越做越细,也会尽量往一个正式的产品方向靠,尽可能的提供有用的代码。

使用Vitamio打造自己的Android万能播放器(4)——本地播放(快捷搜索、数据存储)的更多相关文章

  1. 使用Vitamio打造自己的Android万能播放器(6)——在线播放(播放列表)

    前言 新版本的VPlayer由设计转入开发阶段,预计开发周期为一个月,这也意味着新版本的Vitamio将随之发布,开发者们可以和本系列文章一样,先开发其他功能.本章内容为"在线视频播放列表& ...

  2. 使用Vitamio打造自己的Android万能播放器(5)——在线播放(播放优酷视频)

    前言 为了保证每周一篇的进度,又由于Vitamio新版本没有发布, 决定推迟本地播放的一些功能(截图.视频时间.尺寸等),跳过直接写在线播放部分的章节.从Vitamio的介绍可以看得出,其支持http ...

  3. 使用Vitamio打造自己的Android万能播放器(3)——本地播放(主界面、播放列表)

    前言 打造一款完整可用的Android播放器有许多功能和细节需要完成,也涉及到各种丰富的知识和内容,本章将结合Fragment.ViewPager来搭建播放器的主界面,并实现本地播放基本功能.系列文章 ...

  4. 使用Vitamio打造自己的Android万能播放器(2)—— 手势控制亮度、音量、缩放

    前言 本章继续完善播放相关播放器的核心功能,为后续扩展打好基础.   声明 欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com 农民伯伯: http://ove ...

  5. 使用Vitamio打造自己的Android万能播放器(7)——在线播放(下载视频)

    前言 本章将实现非常实用的功能——下载在线视频.涉及到多线程.线程更新UI等技术,还需思考产品的设计,如何将新加的功能更好的融入到现有的产品中,并不是简单的加一个界面就行了,欢迎大家交流产品设计和技术 ...

  6. 使用Vitamio打造自己的Android万能播放器(1)——准备

    前言 虽然Android已经内置了VideoView组件和MediaPlayer类来支持开发视频播放器,但支持格式.性能等各方面都十分有限,这里与大家一起利用免费的Vitamio来打造属于自己的And ...

  7. H5播放器内置播放视频(兼容绝大多数安卓和ios)

    关于H5播放器内置播放视频,这个问题一直困扰我很长一段时间,qq以前提供白名单已经关闭,后来提供了同层属性的控制,或多或少也有点差强人意. 后来一次偶然发现一个非常简单的方法可以实现. 只需要给vid ...

  8. (1)H5实现音乐播放器【正在播放-歌词篇】

    近期闲来无事,就想着复习一下前端的东西,然后正好跟朋友搞了一个公共开放的音乐api接口,就想着写一个音乐播放器玩玩! 话不多说,直接上图,然后上代码 [播放器显示正在播放] 实现功能: 1:歌词随着歌 ...

  9. android音乐播放器开发 SweetMusicPlayer 播放本地音乐

    上一篇写了载入歌曲列表,http://blog.csdn.net/huweigoodboy/article/details/39856411,如今来总结下播放本地音乐. 一,MediaPlayer 首 ...

随机推荐

  1. html5实现烟花绽放效果

    来源地址:http://codepen.io/whqet/pen/Auzch 1.HTML5 你懂的,先看效果: 2.Html代码 <!-- setup our canvas element - ...

  2. hdu 2157 How many ways_ 矩阵快速幂

    题意:略 直接矩阵乘法就行了 #include <iostream> #include<cstdio> #include<cstring> using namesp ...

  3. Python-求助 SAE 如何使用第三方库? - 德问:编程社交问答

    Python-求助 SAE 如何使用第三方库? - 德问:编程社交问答 求助 SAE 如何使用第三方库?

  4. 格而知之1:UIButton中imageView和titleLabel的位置调整

    在使用UIButton时,有时候需要调整按钮内部的imageView和titleLabel的位置和尺寸.在默认情况下,按钮内部的imageView和titleLabel的显示效果是图片在左文字在右,然 ...

  5. OC基础7:变量和数据类型

    "OC基础"这个分类的文章是我在自学Stephen G.Kochan的<Objective-C程序设计第6版>过程中的笔记. 1.有时候初始化需要让对象带有初始值,那么 ...

  6. python中pip的使用和安装

    Ubuntu下安装pip的方法   安装pip的方法: Install pip and virtualenv for Ubuntu 10.10 Maverick and newer   $ sudo ...

  7. 【设计模式】学习笔记13:组合模式(Composite)

    本文出自   http://blog.csdn.net/shuangde800 认识组合模式 上一篇中,我们可以用迭代器来实现遍历一个集合(数组,ArrayList, Vector, HashTabl ...

  8. 编写javascript的基本技巧

    第一.编写可维护的代码 什么叫着编写可维护的代码呢?就是当我的做出来的项目,拿给其它编码团队能很快的看懂 你编写的代码,你的整个项目的逻辑等等.一个项目的修改维护是要比开发一个项目的成本 是要高的.例 ...

  9. C#中按指定质量保存图片的实例代码 24位深度

     /// <summary>        /// 按指定的压缩质量及格式保存图片(微软的Image.Save方法保存到图片压缩质量为75)        /// </summary ...

  10. 利用DreamweaverCS5制作一个含有动态标题的教程

    DreamweaverCS5怎么制作一个含有动态标题?做一个网页就先要做一个标题,一个好标题会让网页让人印象深刻,有动态的标题会让网页更生动,下面我就介绍一下怎么制作一个含有动态的标题   做一个网页 ...