SufaceViewVideoList播放URL视频

compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'

compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.okhttp3:okhttp:3.9.0'

compile 'com.github.bumptech.glide:glide:3.8.0'

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
主类布局:
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="net.bwie.surfaceviewvideolist.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>

</RelativeLayout>
主类:
/**
 * 1、封装播放视频的SurfaceView
 * 2、网络请求数据(手写bean):bean、httpservice
 * 3、放在RecyclerView中展示
 * 4、item中放入SurfaceView
 * 5、点击播放弹出通知
 * 6、进度
 */
public class MainActivity extends AppCompatActivity implements Callback<KaiyanBean> {

    protected RecyclerView mRecyclerView;
    private Object mData;

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

        initData();
    }

    private void initData() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://baobab.kaiyanapp.com/")
                .addConverterFactory(GsonConverterFactory.create()).build();

        retrofit.create(KaiyanHttpService.class)
                .getKaiyanCall()
                .enqueue(this);
    }

    private void initView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    }

    @Override
    public void onResponse(Call<KaiyanBean> call, Response<KaiyanBean> response) {
        KaiyanBean kaiyanBean = response.body();

        List<VideoBean> videoList = new ArrayList<>();

        for (KaiyanBean.ItemBean itemBean : kaiyanBean.getItemList()) {
            Log.d("1507","type: " + itemBean.getType()+ ", title : " + itemBean.getData().getTitle() + "url : " + itemBean.getData().getPlayUrl());

            if ("video".equals(itemBean.getType())) {
                // 过滤掉type不为video的数据
                VideoBean videoBean = new VideoBean(itemBean.getData().getTitle(), itemBean.getData().getPlayUrl());
                videoList.add(videoBean);
            }

        }

        VideoListAdapter adapter = new VideoListAdapter(this, videoList);
        mRecyclerView.setAdapter(adapter);
    }

    @Override
    public void onFailure(Call<KaiyanBean> call, Throwable t) {

    }
}
适配器:
public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.ViewHolder> {

    private Context mContext;
    private List<VideoBean> mDatas;

    // 记录上一次播放的VideoView
    private VideoSurfaceView mPlayingView;

    public VideoListAdapter(Context context, List<VideoBean> datas) {
        mContext = context;
        mDatas = datas;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext)
                .inflate(R.layout.item_video, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final VideoBean videoBean = mDatas.get(position);

        holder.mTitleTextView.setText(videoBean.getTitle());

        // item刚复用进入屏幕时,无论是否播放都停止并隐藏
        holder.mSurfaceView.stop();
        holder.mSurfaceView.setVisibility(View.INVISIBLE);

        holder.mPlayBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // 下一次播放时,停止上一次播放
                if (mPlayingView != null) {
                    mPlayingView.stop();
                    mPlayingView.setVisibility(View.INVISIBLE);
                }

                // 播放当前位置的视频
                holder.mSurfaceView.setVisibility(View.VISIBLE);// 显示
                holder.mSurfaceView.playVideo(videoBean.getPlayUrl());

                mPlayingView = holder.mSurfaceView;
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        Button mPlayBtn;
        TextView mTitleTextView;
        TextView mProgressTextView;
        VideoSurfaceView mSurfaceView;

        public ViewHolder(View itemView) {
            super(itemView);

            mPlayBtn = ((Button) itemView.findViewById(R.id.play_btn));
            mTitleTextView = ((TextView) itemView.findViewById(R.id.title_tv));
            mProgressTextView = ((TextView) itemView.findViewById(R.id.progress_tv));
            mSurfaceView = ((VideoSurfaceView) itemView.findViewById(R.id.video_view));
        }
    }

}

适配器item类:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">

    <TextView
        android:id="@+id/progress_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="当前进度"/>

    <Button
        android:id="@+id/play_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="播放"/>

    <TextView
        android:id="@+id/title_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="标题"/>

    <net.bwie.surfaceviewvideolist.widget.VideoSurfaceView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_gravity="center_horizontal"/>

</LinearLayout>

自定义控件类:
/**
 * 能播放视频的SurfaceView,封装MediaPlayer
 * 提供一个public方法用于设置播放路径和播放的方法
 */
public class VideoSurfaceView extends SurfaceView implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener {
    private SurfaceHolder mHolder;
    private MediaPlayer mMediaPlayer;

    public VideoSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);

        init();
    }

    private void init() {
        // 获得缓冲区持有者
        mHolder = getHolder();
        // 设置生命周期的回调
        mHolder.addCallback(this);
    }

    // 设置播放路径并播放
    public void playVideo(String path) {
        if (mMediaPlayer == null) {
            mMediaPlayer = new MediaPlayer();
            // 准备完毕监听器
            mMediaPlayer.setOnPreparedListener(this);
        }

        try {
            // 重置MediaPlayer
            mMediaPlayer.reset();
            // 设置画面播放源
            mMediaPlayer.setDisplay(mHolder);
            // 设置播放源
            mMediaPlayer.setDataSource(path);
            // 准备播放
            mMediaPlayer.prepareAsync();
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("1507", e.getMessage());
        }

    }

    // 停止播放
    public void stop() {
        if (mMediaPlayer != null) {
            mMediaPlayer.stop();
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        if (mMediaPlayer != null) {
            mMediaPlayer.release();// 释放资源
            mMediaPlayer = null;
        }
    }

       @Override
    public void onPrepared(MediaPlayer mp) {
        mMediaPlayer.start();
    }
}

Bean类:
public class KaiyanBean {

    private List<ItemBean> itemList;

    public List<ItemBean> getItemList() {
        return itemList;
    }

    public void setItemList(List<ItemBean> itemList) {
        this.itemList = itemList;
    }

    public static class ItemBean {
        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        private String type;
        private DataBean data;

        public DataBean getData() {
            return data;
        }

        public void setData(DataBean data) {
            this.data = data;
        }

        public static class DataBean {

            private String title;
            private String playUrl;

            public String getTitle() {
                return title;
            }

            public void setTitle(String title) {
                this.title = title;
            }

            public String getPlayUrl() {
                return playUrl;
            }

            public void setPlayUrl(String playUrl) {
                this.playUrl = playUrl;
            }
        }

    }

}

public class VideoBean {

    private String title;
    private String playUrl;

    public VideoBean(String title, String playUrl) {
        this.title = title;
        this.playUrl = playUrl;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getPlayUrl() {
        return playUrl;
    }

    public void setPlayUrl(String playUrl) {
        this.playUrl = playUrl;
    }
}



public interface KaiyanHttpService {

    @GET("api/v4/tabs/selected?udid=11111&vc=168&vn=3.3.1&deviceModel=Huawei%36&first_channel=eyepetizer_baidu_market&last_channel=eyepetizer_baidu_market&system_version_code=20")
    Call<KaiyanBean> getKaiyanCall();

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值