android 启动页VideoView 播放本地视频短暂黑屏解决


    在许多应用中,都加入了播放视频,有的在登录页,有的在启动页,今天就试试在启动页播放本地视频。

1,重写VideoView 

    由于 视频的尺寸问题导致VideoView 播放视频的时候不能全屏,这时候需要重写VideoView的onMeasure()的方法,代码如下:
  
public class FullScreenVideoView extends VideoView {


    public FullScreenVideoView(Context context) {
        super(context);
    }

    public FullScreenVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FullScreenVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(View.MeasureSpec.getSize(widthMeasureSpec), View.MeasureSpec.getSize(heightMeasureSpec));
    }
}

2,启动页布局






    <***.FullScreenVideoView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

    



以上的FullScreenVideoView  换成自己的包名,之所以加一个ImageView,是在视频没有播放开始前,先展示缩略图。

3,代码实现

   申请添加SD 权限
  

 启动页代码实现:
public class MainActivity extends FragmentActivity {

    private VideoView videoView ;
    private ImageView imageView ;
    private String videoPath ;
    private int playProgress ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        videoView = (VideoView)findViewById(R.id.video_view);
        imageView = (ImageView)findViewById(R.id.image_view);
        //申请权限
        if(ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED)
        {
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},0);
        }else
        {
            playVideo();
        }

    }

    private void playVideo()
    {
        videoPath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"intro.mp4";
        MediaMetadataRetriever media = new MediaMetadataRetriever();
        media.setDataSource(videoPath);
        //获取第一帧
        Bitmap bitmap  = media.getFrameAtTime(1, MediaMetadataRetriever.OPTION_CLOSEST_SYNC );
        imageView.setImageBitmap(bitmap);
        media.release();
        videoView.setVideoPath(videoPath);
        videoView.start();
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mediaPlayer) {
                        // imageView.setVisibility(View.GONE);
                        mediaPlayer.setLooping(true);
                        mediaPlayer.setOnInfoListener(new MediaPlayer.OnInfoListener() {
                            @Override
                            public boolean onInfo(MediaPlayer mediaPlayer, int what, int i1) {

                                //开始播放时,就把显示第一帧的ImageView gone 掉
                                if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
                                    // video started; hide the placeholder.
                                    imageView.setVisibility(View.GONE);
                                    //videoView.seekTo(0);
                                    return true;
                                }
                                return false;
                            }
                        });
                    }
                });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(grantResults[0] == PackageManager.PERMISSION_GRANTED )
        {
            playVideo();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        //记录播放的progress,避免黑屏
        videoView.pause();
        playProgress = videoView.getCurrentPosition();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(playProgress != 0) {
            videoView.seekTo(playProgress);
            videoView.start();
        }
       // playProgress = videoView.getCurrentPosition();
    }
}
这里的用到的intro.mp4 是从Keep 这个APP 的apk 包解压缩拿出来的,直接拷贝到你的SD目录里就行。

4,最后


   设置启动页主题,避免出现白屏,主题中增加两行:
        true
        true


  
   

你可能感兴趣的:(视频相关)