自定义视频播放(原生)

来源:互联网 发布:网络空间安全考研排名 编辑:程序博客网 时间:2024/05/18 23:57

使用原生的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>


0 0
原创粉丝点击