Android实现的视频背景

Android APP实现视频背景

步骤

1.导入Class
2.在需要使用的Activity中重写方法,加载本地资源
3.使用帧,布局中使用自定义Video控件并FULL全屏

第一步 导入重写的VideoView class

package com.scxx.wangli_qq.UserControl;

import android.content.Context;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.widget.VideoView;

public class UserVideoView extends VideoView {


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

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

    public UserVideoView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //我们重新计算高度
        int width = getDefaultSize(0, widthMeasureSpec);
        int height = getDefaultSize(0, heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

    @Override
    public void setOnPreparedListener(MediaPlayer.OnPreparedListener l) {
        super.setOnPreparedListener(l);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.onKeyDown(keyCode, event);
    }


}


在需要使用的Activity中重写方法,加载本地资源

/**
     * 视频加载
     */
    //返回重启加载
    @Override
    protected void onRestart() {
        super.onRestart();
        InitView();
    }
    //防止锁屏或者切出的时候,音乐在播放
    @Override
    protected void onStop() {
        super.onStop();
        userVideoView.stopPlayback();
    }
    public void InitView()
    {
        userVideoView.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.video));
        userVideoView.start();

        userVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                userVideoView.start();
            }
        });
    }

InitView函数需要在onCreate方法中调用,才能生效
R.raw.video为视频资源

使用帧

使用帧布局,并把自定义控件放在第一位

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.scxx.wangli_qq.UserControl.UserVideoView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/Video_player"/>
</FrameLayout>

你可能感兴趣的:(Study,log,for,Android,.)