国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

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

2019-12-12 05:54:06
字體:
來源:轉載
供稿:網友

前言

 打造一款完整可用的Android播放器有許多功能和細節需要完成,也涉及到各種豐富的知識和內容,本章將結合Fragment、ViewPager來搭建播放器的主界面,并實現本地播放基本功能。系列文章提供截圖、代碼說明、源碼下載,歡迎交流!

系列

 1、Android 使用Vitamio打造自己的萬能播放器(1)――準備

 2、Android 使用Vitamio打造自己的萬能播放器(2)―― 手勢控制亮度、音量、縮放

 正文

 一、目標

  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操作應放到主線程中,否則可能出錯。

 以上就是對Android Vitamio本地播放的資料整理,后續繼續補充謝謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 镇原县| 施秉县| 平顺县| 新泰市| 永修县| 栾川县| 威信县| 平定县| 定襄县| 乌恰县| 定结县| 聂荣县| 武邑县| 石渠县| 桑植县| 平利县| 海丰县| 当涂县| 宁河县| 汝州市| 蕉岭县| 杨浦区| 大厂| 萨嘎县| 泉州市| 崇文区| 北流市| 门头沟区| 海丰县| 绍兴市| 苍山县| 红桥区| 洪江市| 涞水县| 菏泽市| 永和县| 彩票| 汝阳县| 镇江市| 旺苍县| 衡南县|