編輯:關於 Android 程式設計
前言
要做出一個真正好用的 Android 播放器,需要處理的功能與細節多到超出想像,牽涉的知識面也很廣。本章先用 Fragment 搭配 ViewPager 把主界面架子搭起來,再完成本地播放最基礎的一步:掃描並列出手機裡的影片。系列文章都附截圖、程式碼說明與源碼下載,歡迎交流。
系列
1、Android 使用 Vitamio 打造自己的萬能播放器(1)——前置準備
2、Android 使用 Vitamio 打造自己的萬能播放器(2)——手勢控制亮度、音量、縮放
正文
一、目標
1.1 以 Fragment + ViewPager 組出主界面
主功能切成「本地視頻」與「在線視頻」兩區,允許左右滑動切換。
1.2 用 ListView 列出 SD 卡中的所有影片

(聲明:圖標均來自網路,僅供學習研究之用。)
二、實作程式碼
2.1 版面配置
<LinearLayout xmlns:android="https://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"
android:drawableTop="@drawable/video_file"
android:text="@string/title_file" />
<RadioButton android:id="@+id/radio_online"
android:drawableTop="@drawable/video_online"
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 Activity 與 FragmentPagerAdapter
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) {
switch (position) {
case 1:
return new FragmentOnline(); // 在線視頻
case 0:
default:
return new FragmentFile(); // 本地視頻
}
}
@Override
public int getCount() {
return 2;
}
};
}
這裡就是 Fragment 與 ViewPager 結合最精簡的樣子,兩個頁面、兩個 Fragment,職責切得乾淨。
2.3 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);
}
/** 遞迴找出所有視音頻檔 */
public void eachAllMedias(File f) {
if (f != null && f.exists() && f.isDirectory()) {
File[] files = f.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
eachAllMedias(file);
} else if (file.exists() && file.canRead()
&& FileUtils.isVideoOrAudio(file)) {
publishProgress(file);
}
}
}
}
}
/** 掃描 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();
}
}
}
程式碼說明
a). 本章的核心就是這段掃描邏輯:遞迴走訪目錄樹,把視音頻檔挑出來塞進清單。
b). ArrayAdapter 與 FileUtils 這裡就不逐一貼了,它們屬於工具與輔助類,請下載專案查看。
c). 特別提醒:mAdapter.add 這類會改動 UI 資料的操作必須放在主線程執行,丟在背景執行緒裡很可能出問題。AsyncTask 的 onProgressUpdate 天生就在主線程,因此把 publishProgress 放在背景、add 放在 onProgressUpdate,是刻意的安排。
以上就是 Android 端以 Vitamio 做本地播放整理的入門部分,後續再繼續補充。
Android 瀑布流借鏡 ListView 的回收與更新機制
在 Android 開發中,瀑布流常用於展示大量圖片內容。若只使用 ScrollView 堆疊子 View,雖然畫面效果容易完成,但當圖片數量增加時,記憶體使用量也會快速
Android 的 android:windowSoftInputMode 屬性詳解
先前的文章提過如何讓軟鍵盤不自動彈出,做法是設定 android:windowSoftInputMode 屬性。那麼這個屬性究竟有何作用?本文就逐一測試它的九個取值。 顧
在 Android 中模擬實現 3D Touch 預覽效果
3D Touch 那種「長按彈出預覽、背景高斯模糊」的交互,看起來很酷,其實在 Android 上也能模擬出來。本文將一步步拆解實現思路,從截屏、高斯模糊到彈出動畫,帶你
Android 自定義控件入門:從自定義屬性到基本繪制流程
一、自定義控件(一)——自定義屬性 TextView1,定義屬性,製作 attrs.xml 文件。屬性值:string、color、attr、array、bool、dec