自定义VideoView全屏显示播放

来源:互联网 发布:asp微信扫码支付源码 编辑:程序博客网 时间:2024/06/05 03:47
一、通过VideoView播放视频的步骤:
 1、在界面布局文件中定义VideoView组件
 2、调用VideoView的如下两个方法来加载指定的视频
         setVidePath(String path):加载path文件代表的视频
         setVideoURI(Uri uri):加载uri所对应的视频

3、调用VideoView的start()、stop()、psuse()方法来控制视频的播放

二、实例:

1、xml布局

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".ui.WelcomeActivity">    <com.cnnews.cyh.appnews.widget.CustomVideoView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/welcome_videoview"        />    <Button        android:layout_width="150dp"        android:layout_height="35dp"        android:layout_marginBottom="20dp"        android:layout_centerHorizontal="true"        android:layout_alignParentBottom="true"        android:background="@drawable/head_bt_bg"        android:onClick="btn_login"        android:text="进入主页"        android:textColor="#ffffff"        android:textSize="16sp"/></RelativeLayout>
2、自定义VideoView类--CustomVideoView

因为在VideoView源码的OnMeasure方法里对这个长宽比进行了处理。这就是为什么长宽比不能改变的原因了。我们把其中处理的代码屏蔽掉,视频大小就可以随着VideoView的长宽改变而改变了。

package com.cnnews.cyh.appnews.widget;import android.content.Context;import android.media.MediaFormat;import android.media.MediaPlayer;import android.util.AttributeSet;import android.view.KeyEvent;import android.widget.VideoView;import java.io.InputStream;/** * Created by cyh on 2016/7/22. */public class CustomVideoView extends VideoView {    public CustomVideoView(Context context) {        super(context);    }    public CustomVideoView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public CustomVideoView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {//重写onMeasure方法,改变长宽        super.onMeasure(widthMeasureSpec, 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);    }}

3、在类中使用

        welcome_videoview = (CustomVideoView)findViewById(R.id.welcome_videoview);        welcome_videoview.setVideoURI(Uri.parse("android.resource://"+this.getPackageName()+"/"+R.raw.video));//获取视频        welcome_videoview.start();        welcome_videoview.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {//设置监听事件,播放完后再次启动播放            @Override            public void onCompletion(MediaPlayer mp) {                welcome_videoview.start();            }        });


                                             
0 0
原创粉丝点击