編輯:關於 Android 程式設計
手邊的事情終於告一段落,可以回到日常的學習節奏上。上一篇《Android 開發學習之路——基於 Vitamio 的影片播放器(一)》裡,我們完成了播放器的基本版面,也做好了影片檔案的掃描與基礎資訊擷取,但還沒真正動手使用 Vitamio 這個庫。這一篇就從最基礎的用法講起。
先看一下 layout 檔的寫法:
<layout>
<data class="LocalPlayerBinding"></data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<io.vov.vitamio.widget.CenterLayout
android:id="@+id/dd"
android:layout_width="match_parent"
android:layout_height="match_parent">
<io.vov.vitamio.widget.VideoView
android:id="@+id/surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
</io.vov.vitamio.widget.CenterLayout>
<TextView
android:id="@+id/subtitle_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</layout>
io.vov.vitamio.widget.CenterLayout 是 Vitamio 附帶的居中容器,而 io.vov.vitamio.widget.VideoView 才是真正的播放畫面,本質上它還是一個 SurfaceView。值得注意的是,Vitamio 的 VideoView 並不是 android.widget.VideoView,import 時別挑錯。
接著看 Activity 的部分:
public class LocalVideoPlayer extends BaseActivity {
private String subtitle_path = "";
private long mPosition = 0;
private LocalPlayerBinding binding;
public static void launch(Context context, String url) {
Intent intent = new Intent(context, LocalVideoPlayer.class);
intent.putExtra("url", url);
context.startActivity(intent);
}
@Override
protected void initParams() {
Vitamio.isInitialized(getActivity());
binding = DataBindingUtil.setContentView(getActivity(), R.layout.activity_local_player);
}
@Override
protected void initViews() {
super.initViews();
String url = getActivity().getIntent().getStringExtra("url");
initVideo("file://" + url);
}
@Override
protected void onPause() {
mPosition = binding.surfaceView.getCurrentPosition();
binding.surfaceView.stopPlayback();
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
if (mPosition > 0) {
binding.surfaceView.seekTo(mPosition);
mPosition = 0;
}
binding.surfaceView.start();
}
private void initVideo(String path) {
if (path.isEmpty()) {
Toast.makeText(getActivity(), "請先設定媒體檔案的路徑", Toast.LENGTH_LONG).show();
return;
}
binding.surfaceView.setVideoPath(path);
binding.surfaceView.setMediaController(new MyMediaController(getActivity()));
binding.surfaceView.requestFocus();
binding.surfaceView.setOnPreparedListener(mp -> {
mp.setPlaybackSpeed(1.0f);
binding.surfaceView.addTimedTextSource(subtitle_path);
binding.surfaceView.setTimedTextShown(true);
});
binding.surfaceView.setOnTimedTextListener(new MediaPlayer.OnTimedTextListener() {
@Override
public void onTimedText(String text) {
binding.subtitleView.setText(text);
}
@Override
public void onTimedTextUpdate(byte[] pixels, int width, int height) {
}
});
}
}
幾個關鍵點:
setVideoPath(path) 決定播放來源,這裡的路徑來自前一篇掃描出來的播放清單,透過 launch() 的 Intent 傳進來。接著掛上自訂的 controller,播放、暫停、拖曳進度就都交給它處理。
生命週期這邊做了一個常見的處理:在 onPause() 記下目前的播放位置並停止播放,回到前台時再用 seekTo() 續播,使用者切出去再回來不會從頭開始。
字幕的部分透過 addTimedTextSource() 載入字幕檔,並在 OnTimedTextListener 中把文字塞進 TextView;如果你的字幕是內嵌在容器裡的圖形格式,則改用 onTimedTextUpdate() 的像素資料自行繪製。
Vitamio 的 controller 只要繼承並覆寫一個方法就能換皮:
public class MyMediaController extends MediaController {
private Context mContext;
public MyMediaController(Context context) {
super(context);
mContext = context;
}
public MyMediaController(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
}
@Override
protected View makeControllerView() {
return LayoutInflater.from(mContext)
.inflate(R.layout.mymediacontroller, this);
}
}
也就是說,把原本 super.makeControllerView() 換成自己的配置即可。自訂的 controller 版面大致如下:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="74dp"
android:background="@color/mediacontroller_bg"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/mediacontroller_play_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="@drawable/mediacontroller_button"
android:src="@drawable/mediacontroller_pause" />
<TextView
android:id="@+id/mediacontroller_time_current"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/mediacontroller_play_pause" />
<TextView
android:id="@+id/mediacontroller_time_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
<SeekBar
android:id="@+id/mediacontroller_seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/mediacontroller_time_total"
android:layout_toRightOf="@id/mediacontroller_time_current"
android:max="1000"
android:maxHeight="4dp"
android:minHeight="4dp"
android:progressDrawable="@drawable/seekbar_define_color_style"
android:thumb="@drawable/seekbar_thumb" />
</RelativeLayout>
<TextView
android:id="@+id/mediacontroller_file_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true" />
</LinearLayout>
這裡最容易踩的雷:上述那些 id 名稱不能任意更改。Vitamio 內部的 MediaController 是靠 id 去 findViewById 並綁定行為的,改名之後播放鍵、進度條、時間文字都會失效。想改的只有樣式,不是名稱。
整體來說,Vitamio 的 API 相當精簡,該替你處理的細節(解碼、渲染、字幕、進度控制)大多包好了。它底層是以 FFmpeg 為基礎,若之後想深入理解音訊解碼與播放時序,從 FFmpeg 下手會是必經之路。
Android Xfermode 入門:用圖層概念做出圓角、遮罩與圖形裁切
寫這篇的起點,是參考了一篇講解 Paint.setXfermode() 的文章所得到的靈感。 不過讀了一圈下來,也有些忍不住想吐槽的地方:網路上關於 Xfermode
動手高仿京東篩選:Android PopupWindow 的兩種經典玩法
京東的篩選功能煥然一新之後,那種流暢又炫酷的交互著實讓人眼前一亮。既然別人做得這麼漂亮,我們自己動手復刻一個又有何不可?於是便有了這篇實戰記錄。廢話不多說,先看效果,只想
Activity 啟動模式之 standard:每次啟動都是新實例
standard 是系統默認的啟動模式,規則很簡單:每次激活 Activity 都會創建新實例,並放入 Activity 棧中。 布局 放一個按鈕,點擊後用顯式 Int
Android 6.0 執行期授權機制與權限管理實作
6.0 之後,授權邏輯整個換了 Android 6.0(API 23)對權限系統做了一次結構性的改造。只有當你的 targetSdkVersion 設為 23 以上,且應