Android:VideoView——视频播放器

一,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>
发布了360 篇原创文章 · 获赞 163 · 访问量 20万+

猜你喜欢

转载自blog.csdn.net/qq_42192693/article/details/105145781