Android:VideoView&MediaPlayer+SurfaceView——視頻播放器

一,VideoView

無法手動控制視頻,只能任其播放到結束。

   <VideoView
       android:id="@+id/videoview"
       android:layout_width="match_parent"
       android:layout_height="240dp"
       app:layout_constraintBottom_toBottomOf="parent"
       app:layout_constraintTop_toTopOf="parent"
       tools:layout_editor_absoluteX="42dp" />
VideoView videoView =findViewById(R.id.videoview);
videoView.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.a));
// videoView.setVideoPath("/mnt/sdcard/oppo.3gp");
videoView.start();

二,MediaController+ViewVideo

有簡單的控制按鈕和進度條

MediaController mediaController = new MediaController(this);
VideoView videoView = (VideoView) findViewById(R.id.videoview);

mediaController.setAnchorView(videoView);//互相關聯
videoView.setMediaController(mediaController);

videoView.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.a));
        //videoView.start(); //開始播放
videoView.requestFocus();//獲取焦點,要手動點擊播放纔可以播放

三,自定義播放類

public class FullScreenVideoView extends VideoView {
    public FullScreenVideoView(Context context) {
        super(context);
    }
    public FullScreenVideoView(Context context, AttributeSet attributeSet) {
        super(context,attributeSet);
    }
    public FullScreenVideoView(Context context, AttributeSet attributeSet,int def) {
        super(context,attributeSet,def);
    }
    protected void onMeasure(int widthMessageSpec,int heightMeasureSpec){
        super.onMeasure(widthMessageSpec,heightMeasureSpec);
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        Point size = new Point();
        wm.getDefaultDisplay().getSize(size);
        int width = size.x;
        int height = size.y;
        setMeasuredDimension(width,height);
    }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

   <com.zzu.sunshaoqi.a1050.FullScreenVideoView
       android:id="@+id/videoview"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />

</LinearLayout>

四,MediaPlayer+SurfaceView

  • MediaPlayer主要用用來播放音頻,沒有提供輸出界面。
  • SurfaceView來顯示MediaPlayer播放圖像輸出。
MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.a);
SurfaceView surfaceView = findViewById(R.id.surfaceView);
 //設置橫屏播放
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//設置視頻輸出到SurfaceView
mediaPlayer.setDisplay(surfaceView.getHolder());
//獲取窗口管理器
WindowManager manager = getWindowManager();
//獲取屏幕大小
DisplayMetrics metrics = new DisplayMetrics();
manager.getDefaultDisplay().getMetrics(metrics);
//設置視頻保持縱橫比例縮放佔滿 整個屏幕
surfaceView.setLayoutParams(new RelativeLayout.LayoutParams(metrics.widthPixels, mediaPlayer.getVideoHeight() * metrics.widthPixels / mediaPlayer.getVideoHeight()));
mediaPlayer.start();

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章