一、目标
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>
<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;
}
};
}
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;
}
}
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打造自己的Android万能播放器(3)——本地播放(主界面、播放列表),如需转载请自行联系原博主。