简单的视频控件

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/spkj"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <VideoView
        android:id="@+id/kj_videoView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <FrameLayout
        android:id="@+id/fl"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:visibility="gone" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <RelativeLayout
                android:id="@+id/pauseOrPlay"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="10dp" >

                <ImageView
                    android:id="@+id/pause"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:src="@android:drawable/ic_media_pause"
                    android:visibility="gone" />

                <ImageView
                    android:id="@+id/play"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:src="@android:drawable/ic_media_play" />
            </RelativeLayout>

            <ImageView
                android:id="@+id/previous"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="10dp"
              android:layout_toRightOf="@+id/pauseOrPlay"
             android:src="@android:drawable/ic_media_previous" />

            <ImageView
                android:id="@+id/next"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="10dp"
                android:layout_toRightOf="@+id/previous"                           android:src="@android:drawable/ic_media_next" />

            <TextView
                android:id="@+id/view_time"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="10dp"
                android:layout_toRightOf="@+id/next"
                android:gravity="center_vertical"
                android:text="19:20" />

            <ImageView
                android:id="@+id/share"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
              android:layout_alignParentRight="true"
                android:layout_marginRight="10dp"              android:src="@android:drawable/ic_menu_share" />

            <ImageView
                android:id="@+id/screen_all"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginRight="10dp"
                android:layout_toLeftOf="@+id/share"
                android:src="@android:drawable/btn_plus" />
        </RelativeLayout>
    </FrameLayout>

    <ImageView
        android:id="@+id/start_medio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:padding="10dp"
        android:src="@android:drawable/ic_menu_slideshow" />

</RelativeLayout>
package com.haiqing.kongjian;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.MediaController.MediaPlayerControl;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.VideoView;

/**
 * Android支持播放网络上的视频。在播放网络上的视频时,牵涉到视频流的传输,
 * 往往有两种协议,一种是HTTP,一种是RTSP。
 * 这两种协议最大的不同是,HTTP协议,不支持实时流媒体的播放,
 * 而RTSP协议就支持
 * @author wm
 *
 */
public class KjVideoView {

    private Context context;
    private RelativeLayout mRelativeLayout;
    private VideoView kj_videoView;
    private FrameLayout fl;
    private ImageView  start_medio;
    private ImageView  pause;
    private ImageView  play;
    private ImageView  previous;
    private ImageView  next;
    private ImageView  screen_all;
    private ImageView  share;
    private TextView  view_time;
    private final static int NO_USE_CREEN = 1002;

    private Handler handler = new Handler(){
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
            case NO_USE_CREEN:
                fl.setVisibility(View.GONE);
                break;

            default:
                break;
            }
        }
    };

    public KjVideoView(Context context) {
        this.context = context;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mRelativeLayout = (RelativeLayout) inflater.inflate(R.layout.video_qinghai, null);
        initView(mRelativeLayout);
        setListener();
    }

    public KjVideoView(Context context, AttributeSet attrs) {
        this.context = context;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mRelativeLayout = (RelativeLayout) inflater.inflate(R.layout.video_qinghai, null);
        initView(mRelativeLayout);
        setListener();
    }
    public KjVideoView(Context context, AttributeSet attrs, int defStyle) {
        this.context = context;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mRelativeLayout = (RelativeLayout) inflater.inflate(R.layout.video_qinghai, null);
        initView(mRelativeLayout);
        setListener();
    }

    /**
     * 添加控件
     * @param mRelativeLayout
     */
    public void initView(RelativeLayout mRelativeLayout){
        kj_videoView = (VideoView) mRelativeLayout.findViewById(R.id.kj_videoView);
        fl = (FrameLayout) mRelativeLayout.findViewById(R.id.fl);
        start_medio = (ImageView) mRelativeLayout.findViewById(R.id.start_medio);
        pause = (ImageView) mRelativeLayout.findViewById(R.id.pause);
        play = (ImageView) mRelativeLayout.findViewById(R.id.play);
        previous = (ImageView) mRelativeLayout.findViewById(R.id.previous);
        next = (ImageView) mRelativeLayout.findViewById(R.id.next);
        screen_all = (ImageView) mRelativeLayout.findViewById(R.id.screen_all);
        share = (ImageView) mRelativeLayout.findViewById(R.id.share);
        view_time = (TextView) mRelativeLayout.findViewById(R.id.view_time);
    }



    public void setOnClickListener(OnClickListener onClickListener){
        start_medio.setOnClickListener(onClickListener);
        pause.setOnClickListener(onClickListener);
        play.setOnClickListener(onClickListener);
        previous.setOnClickListener(onClickListener);
        next.setOnClickListener(onClickListener);
        screen_all.setOnClickListener(onClickListener);
        share.setOnClickListener(onClickListener);
    }

    public void startPlay(String path,MediaPlayerControl mediaPlayerControl){
        kj_videoView.setKeepScreenOn(true);
        kj_videoView.setVideoPath(path);
        MediaController controller = new MediaController(context);
        kj_videoView.setMediaController(controller);
        controller.setMediaPlayer(mediaPlayerControl);
    }



    public TextView getTextView(){
        return view_time;
    }

    public VideoView getVideoView(){
        return kj_videoView;
    }


    public void setListener(){
        kj_videoView.setOnClickListener(new OnClickListener() {

            public void onClick(View view) {
                //点击显示
                if(fl.isShown()){
                    fl.setVisibility(View.GONE);
                }else{
                    fl.setVisibility(View.VISIBLE);
                }
            }
        });

        mRelativeLayout.setOnKeyListener(new OnKeyListener() {

            public boolean onKey(View view, int keyCode, KeyEvent event) {
                if(fl.isShown()){
                    handler.removeMessages(NO_USE_CREEN);
                    Message msg = new Message();
                    msg.what = NO_USE_CREEN;
                    handler.sendMessageDelayed(msg, 5000);
                    return true;
                }
                return false;
            }
        });
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值