自定义视频播放(原生)

使用原生的VideoView、MadielView



自定义VideoView:

package com.whzg.zbjy.utils;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.VideoView;

public class CustomVideoView extends VideoView
{

    private PlayPauseListener mListener;

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

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

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

    public void setPlayPauseListener (PlayPauseListener listener)
    {
        mListener = listener;
    }

    @Override
    public void pause ()
    {
        super.pause ();
        if (mListener != null)
        {
            mListener.onPause ();
        }
    }

    @Override
    public void start ()
    {
        super.start ();
        if (mListener != null)
        {
            mListener.onPlay ();
        }
    }

    public interface PlayPauseListener
    {
        void onPlay();

        void onPause();
    }

}

自定义播放界面:

使用intent传url

Intent intent = new Intent(getActivity(), ShowVideoAct.class);

intent.putExtra(ShowVideoAct.URL, “url”);

startActivity(intent);


package com.whzg.zbjy.ui;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.TextView;

import com.whzg.zbjy.R;
import com.whzg.zbjy.utils.CustomVideoView;

public class ShowVideoAct extends Activity
{

    public static final String URL = "url";

    private Button btnBack;
    private CustomVideoView vv;
    private TextView tv;

    @Override
    protected void onCreate (Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView (R.layout.show_video);

        String url = getIntent().getStringExtra (URL);
        vv = (CustomVideoView)findViewById (R.id.video_show_video);
        tv = (TextView)findViewById (R.id.tv_show_video_loading_video);
        Uri uri = Uri.parse (url);
        vv.setMediaController (new MediaController (this));
        vv.setVideoURI (uri);
        vv.start ();
        vv.setPlayPauseListener (new CustomVideoView.PlayPauseListener ()
        {

            @Override
            public void onPlay ()
            {
                tv.setVisibility (View.GONE);
            }

            @Override
            public void onPause ()
            {
            }
        });
        // vv.requestFocus();//加载完之后等待,用户点击播放
    }
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <com.whzg.zbjy.utils.CustomVideoView
            android:id="@+id/video_show_video"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent" />
    </LinearLayout>

    <TextView
        android:id="@+id/tv_show_video_loading_video"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="44dp"
        android:background="#5000"
        android:gravity="center"
        android:text="加载中..."
        android:textColor="#fff"
        android:textSize="20sp" />

</FrameLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值