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

简介:

一、目标

1.1 使用Fragment、ViewPager搭建主界面

主功能区分为:本地视频、在线视频,允许滑动切换模块 

1.2 用ListView显示sdcard所有视频 

效果截图:

 

(图标均来自网络,仅供学习研究之用!)

二、实现代码

2.1 xml

<? xml version="1.0" encoding="utf-8" ?>
< LinearLayout  xmlns:android ="http://schemas.android.com/apk/res/android"
    android:orientation
="vertical"  android:gravity ="center_horizontal"
    android:layout_width
="match_parent"  android:layout_height ="match_parent" >
     < RadioGroup  android:gravity ="center_vertical"
        android:layout_width
="fill_parent"  android:layout_height ="wrap_content"
        android:orientation
="horizontal" >
         < RadioButton  android:id ="@+id/radio_file"  android:checked ="true"
            style
="@style/main_tab_bottom"  android:drawableTop ="@drawable/video_file"
            android:text
="@string/title_file"   />
         < RadioButton  android:id ="@+id/radio_online"
            android:drawableTop
="@drawable/video_online"  style ="@style/main_tab_bottom"
            android:text
="@string/title_online"   />
     </ RadioGroup >
     < android.support.v4.view.ViewPager
        
android:background ="@color/background"  android:id ="@+id/pager"
        android:layout_width
="match_parent"  android:layout_height ="match_parent" >
     </ android.support.v4.view.ViewPager >
</ LinearLayout >

  这是整体布局,使用RadioButton切换本地视频和在线视频功能,具体样式请下载项目。ViewPager支持左右侧滑切换功能。

2.2 class 

MainFragmentActivity 

public  class MainFragmentActivity  extends FragmentActivity {

     private ViewPager mPager;

    @Override
     protected  void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_pager);
        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);
    }

     private FragmentPagerAdapter mAdapter =  new FragmentPagerAdapter(getSupportFragmentManager()) {

         /**  仅执行一次  */
        @Override
         public Fragment getItem( int position) {
            Fragment result =  null;
             switch (position) {
             case 1:
                result =  new FragmentOnline(); // 在线视频
                 break;
             case 0:
             default:
                result =  new FragmentFile(); // 本地视频
                 break;
            }
             return result;
        }

        @Override
         public  int getCount() {
             return 2;
        }
    };
}

这里是Fragment与ViewPager结合使用的简单例子。 

FragmentFile

public  class FragmentFile  extends FragmentBase  implements OnItemClickListener {

     private FileAdapter mAdapter;

    @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v =  super.onCreateView(inflater, container, savedInstanceState);
        mAdapter =  new FileAdapter(getActivity(),  null);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener( this);
         new ScanVideoTask().execute();
         return v;
    }

     /**  单击启动播放  */
    @Override
     public  void onItemClick(AdapterView<?> parent, View view,  int position,  long id) {
         final File f = mAdapter.getItem(position);
        Intent intent =  new Intent(getActivity(), VideoViewDemo. class);
        intent.putExtra("path", f.getPath());
        startActivity(intent);
    }

     /**  扫描SD卡  */
     private  class ScanVideoTask  extends AsyncTask<Void, File, Void> {

        @Override
         protected Void doInBackground(Void... params) {
            eachAllMedias(Environment.getExternalStorageDirectory());
             return  null;
        }

        @Override
         protected  void onProgressUpdate(File... values) {
            mAdapter.add(values[0]);
            mAdapter.notifyDataSetChanged();
        }

         /**  遍历所有文件夹,查找出视频文件  */
         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);
                        }
                    }
                }
            }
        }
    }

     private  class FileAdapter  extends ArrayAdapter<File> {

         public FileAdapter(Context ctx, ArrayList<File> l) {
             super(ctx, l);
        }

        @Override
         public View getView( int position, View convertView, ViewGroup parent) {
             final File 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.getName());
             return convertView;
        }
    }

代码说明:

a).  这里是本章的主要功能,扫描所有视音频文件,并显示出来。

b).  ArrayAdapter和FileUtils这里不一一贴代码,主要是工具和辅助类,请下载项目查看。 

c).  注意mAdapter.add操作应放到主线程中,否则可能出错。 

三、 代码下载

 Vitamio-Demo2012-5-30.zip

本文转自博客园农民伯伯的博客,原文链接:使用Vitamio打造自己的Android万能播放器(3)——本地播放(主界面、播放列表),如需转载请自行联系原博主。

 

目录
相关文章
|
7月前
|
缓存 网络协议 开发工具
庖丁解牛之-Android平台RTSP|RTMP播放器设计
我们在做Android平台RTSP或者RTMP播放器开发的时候,需要注意的点非常多,以下,以大牛直播SDK(官方)的接口为例,大概介绍下相关接口设计:
108 0
|
7月前
|
编解码 Android开发 数据安全/隐私保护
Android平台外部编码数据(H264/H265/AAC/PCMA/PCMU)实时预览播放技术实现
好多开发者可能疑惑,外部数据实时预览播放,到底有什么用? 是的,一般场景是用不到的,我们在开发这块前几年已经开发了非常稳定的RTMP、RTSP直播播放模块,不过也遇到这样的场景,部分设备输出编码后(视频:H.264/H.265,音频:AAC/PCMA/PCMU)的数据,比如无人机或部分智能硬件设备,回调出来的H.264/H.265数据,除了想转推到RTMP、轻量级RTSP服务或GB28181外,还需要本地预览甚至对数据做二次处理(视频分析、实时水印字符叠加等,然后二次编码),基于这样的场景诉求,我们开发了Android平台外部编码数据实时预览播放模块。
|
4月前
|
XML Java Android开发
Android Studio App开发中使用录音机、MediaRecorder录制音频和MediaPlayer播放音频讲解及实战(附源码)
Android Studio App开发中使用录音机、MediaRecorder录制音频和MediaPlayer播放音频讲解及实战(附源码)
72 0
|
4月前
|
XML Java 调度
Android开发音效增强中铃声播放Ringtone及声音池调度SoundPool的讲解及实战(超详细 附源码)
Android开发音效增强中铃声播放Ringtone及声音池调度SoundPool的讲解及实战(超详细 附源码)
47 0
|
4月前
|
XML 存储 Java
Android 开发音频录播中媒体录制器MediaRecorder和媒体播放器MediaPlayer的讲解及实战(超详细 附源码)
Android 开发音频录播中媒体录制器MediaRecorder和媒体播放器MediaPlayer的讲解及实战(超详细 附源码)
42 0
|
4月前
|
XML Java Android开发
Android App开发实战项目之仿手机QQ动感影集动画播放(附源码和演示视频 可直接使用)
Android App开发实战项目之仿手机QQ动感影集动画播放(附源码和演示视频 可直接使用)
28 0
|
4月前
|
XML 算法 Java
Android Studio App开发之利用图像解码器ImageDecoder播放GIF动图、Webp、HEIF图片(附源码 简单实用)
Android Studio App开发之利用图像解码器ImageDecoder播放GIF动图、Webp、HEIF图片(附源码 简单实用)
104 0
|
7月前
|
编解码 网络协议 Android开发
Android平台RTMP|RTSP直播播放器功能进阶探讨
很多开发者在跟我聊天的时候,经常问我,为什么一个RTMP或RTSP播放器,你们需要设计那么多的接口,真的有必要吗?带着这样的疑惑,我们今天聊聊Android平台RTMP、RTSP播放器常规功能,如软硬解码设置、实时音量调节、实时快照、实时录像、视频view翻转和旋转、画面填充模式设定、解码后YUV、RGB数据回调等:
103 0
|
7月前
|
编解码 Android开发 数据安全/隐私保护
Android平台如何实现外部编码后(H.264/H.265)数据实时预览播放
我们在对接开发者的时候,遇到这样的诉求:除了正常的RTMP、RTSP直播播放外,有些硬件设备输出编码后(H.264/H.265)的数据,比如无人机或类似硬件产品,回调出来的H.264/H.265数据,除了正常转推到RTMP、轻量级RTSP服务或GB28181外,还需要本地预览甚至重新对数据做二次处理,基于这样的场景诉求,我们开发了外部编码后数据实时预览播放模块。
|
7月前
|
前端开发 Android开发 开发者
Android平台RTSP、RTMP播放端如何实现YUV或ARGB数据按设定角度旋转
做音视频RTSP或RTMP直播播放器的时候,不免会遇到这样的诉求,实时播放或快照的时候,由于前端摄像头安装角度不一定是正向,导致播放或快照的时候,视频view显示的画面是呈90° 180°甚至270°旋转的。
133 0