• SurfaceViewVideoList 列表视频(SurfaceView+Mediaplayer实现播放功能 网络)



    //适配布局

    package
    com.example.lenovo.surfaceview.adapter; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import com.example.lenovo.surfaceview.R; import com.example.lenovo.surfaceview.bean.VideoBean; import com.example.lenovo.surfaceview.widget.VideoSurfaceView; import java.util.List; 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)); } } }






    package com.example.lenovo.surfaceview.bean;
    
    import java.util.List;
    
    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;
                }
            }
    
        }
    
    }



     1 package com.example.lenovo.surfaceview.bean;
     2 
     3 public class VideoBean {
     4 
     5     private String title;
     6     private String playUrl;
     7 
     8     public VideoBean(String title, String playUrl) {
     9         this.title = title;
    10         this.playUrl = playUrl;
    11     }
    12 
    13     public String getTitle() {
    14         return title;
    15     }
    16 
    17     public void setTitle(String title) {
    18         this.title = title;
    19     }
    20 
    21     public String getPlayUrl() {
    22         return playUrl;
    23     }
    24 
    25     public void setPlayUrl(String playUrl) {
    26         this.playUrl = playUrl;
    27     }
    28 }
    package com.example.lenovo.surfaceview.httpservice;
    
    
    import com.example.lenovo.surfaceview.bean.KaiyanBean;
    
    import retrofit2.Call;
    import retrofit2.http.GET;
    
    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();
    
    }
    
    
    
    
    //http
    

      

    package com.example.lenovo.surfaceview.widget;
    
    import android.content.Context;
    import android.media.MediaPlayer;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    
    /**
     * 能播放视频的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();
        }
    }
    package com.example.lenovo.surfaceview;
    
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.RecyclerView;
    import android.util.Log;
    
    import com.example.lenovo.surfaceview.adapter.VideoListAdapter;
    import com.example.lenovo.surfaceview.bean.KaiyanBean;
    import com.example.lenovo.surfaceview.bean.VideoBean;
    import com.example.lenovo.surfaceview.httpservice.KaiyanHttpService;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import retrofit2.Call;
    import retrofit2.Callback;
    import retrofit2.Response;
    import retrofit2.Retrofit;
    import retrofit2.converter.gson.GsonConverterFactory;
    
    /**
     * 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) {
    
        }
    }
     <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"/>
    

      

    <?xml version="1.0" encoding="utf-8"?>
    <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="标题"/>
    
    
    //重写
        <com.example.lenovo.surfaceview.widget.VideoSurfaceView
            android:id="@+id/video_view"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_gravity="center_horizontal"/>
    
    </LinearLayout>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>




    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    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'
  • 相关阅读:
    只要我跑的够快,内卷它就卷不到我,一名高中生是如何做到在疫情下涨薪70%的?
    一个@Transaction哪里来这么多坑?
    基于netty实现一个完整的TFTP服务器
    DBeaver安装和注意要点
    Logstash将ES服务器A数据迁移ES服务器B
    Logstash迁移ES5.6.1一个索引多type类型迁移到ES7.6.2无type类型
    windows下使用filezilla上传文件权限问题
    logstash7.6.2更新已存在的elasticsearch记录
    mysql磁盘空间不足报错
    Kibana中DevTools命令集锦
  • 原文地址:https://www.cnblogs.com/yudada/p/7861086.html
Copyright © 2020-2023  润新知