主布局:
1 210 11 16 17 22 23 27 28
主函数:
1 public class MainActivity extends AppCompatActivity implements View.OnClickListener { 2 3 protected Button mNextBtn; 4 protected Button mPlayBtn; 5 protected VideoView mVideoView; 6 7 @Override 8 protected void onCreate(Bundle savedInstanceState) { 9 super.onCreate(savedInstanceState);10 super.setContentView(R.layout.activity_main);11 initView();12 }13 14 @Override15 public void onClick(View view) {16 if (view.getId() == R.id.next_btn) {17 18 VideoListActivity.startActivity(this);19 20 } else if (view.getId() == R.id.play_btn) {21 String videoPath = "http://172.29.23.6:8080/video/aa.3gp";22 playVideo(videoPath);23 }24 }25 26 private void playVideo(String videoPath) {27 mVideoView.setVideoPath(videoPath);28 // 设置多媒体控制器29 mVideoView.setMediaController(new MediaController(this));30 mVideoView.start();31 }32 33 private void initView() {34 mNextBtn = (Button) findViewById(R.id.next_btn);35 mNextBtn.setOnClickListener(MainActivity.this);36 mPlayBtn = (Button) findViewById(R.id.play_btn);37 mPlayBtn.setOnClickListener(MainActivity.this);38 mVideoView = (VideoView) findViewById(R.id.video_view);39 }40 }
activity_video_list:
1 210 11 16 17
VideoListActivity:
1 public class VideoListActivity extends AppCompatActivity { 2 3 protected RecyclerView mRecyclerView; 4 5 public static void startActivity(Context context) { 6 Intent intent = new Intent(context, VideoListActivity.class); 7 context.startActivity(intent); 8 } 9 10 @Override11 protected void onCreate(Bundle savedInstanceState) {12 super.onCreate(savedInstanceState);13 super.setContentView(R.layout.activity_video_list);14 initView();15 16 ListpathList = new VideoBean().getPathList();17 VideoListAdapter adapter = new VideoListAdapter(this, pathList);18 mRecyclerView.setAdapter(adapter);19 }20 21 private void initView() {22 mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);23 }24 }
item_video:
1 26 7 12 13 18 19 25 26 27
Bean类:
1 package net.bwie.videoview.bean; 2 3 import android.os.Environment; 4 5 import java.util.ArrayList; 6 import java.util.List; 7 8 public class VideoBean { 9 10 private ListpathList;11 12 public VideoBean() {13 pathList = new ArrayList<>();14 for (int i = 0; i < 20; i++) {15 String path = Environment.getExternalStorageDirectory()16 + "/VID_20171117_144736.3gp";17 pathList.add(path);18 }19 }20 21 public List getPathList() {22 return pathList;23 }24 25 public void setPathList(List pathList) {26 this.pathList = pathList;27 }28 }
Adapter:
1 public class VideoListAdapter extends RecyclerView.Adapter{ 2 3 private Context mContext; 4 private List mPathList; 5 6 // 当前正在播放的VideoView 7 private VideoView mPlayingVideoView; 8 //记录正在播放的进度TextView 9 private TextView mPlayingProgressTextiew; 10 11 private Handler mHandler; 12 13 public VideoListAdapter(Context context, List pathList) { 14 mContext = context; 15 mPathList = pathList; 16 } 17 18 @Override 19 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 20 View itemView = LayoutInflater.from(mContext) 21 .inflate(R.layout.item_video, parent, false); 22 return new ViewHolder(itemView); 23 } 24 25 @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1) 26 @Override 27 public void onBindViewHolder(final ViewHolder holder, int position) { 28 // 获取播放地址 29 final String path = mPathList.get(position); 30 31 // 如果VideoView滑出去时正在播放,下次复用进入的item应该停止播放并隐藏 32 holder.mVideoView.stopPlayback();// 停止播放 33 holder.mVideoView.setVisibility(View.INVISIBLE);// 设置不可见 34 holder.mProgressTextView.setText("0%"); 35 36 37 // 播放按钮绑定监听器 38 holder.mPlayBtn.setOnClickListener(new View.OnClickListener() { 39 @Override 40 public void onClick(View v) { 41 42 // 停止上一个VideoView的播放并隐藏,才能继续播放当前的视频 43 if (mPlayingVideoView != null) { 44 mPlayingVideoView.stopPlayback(); 45 mPlayingVideoView.setVisibility(View.INVISIBLE); 46 } 47 48 // 播放时让VideoView可见 49 holder.mVideoView.setVisibility(View.VISIBLE);// 设置可见性 50 holder.mVideoView.setVideoPath(path); 51 holder.mVideoView.start(); 52 53 // 记录当前正在播放的VideoView 54 mPlayingVideoView = holder.mVideoView; 55 mPlayingProgressTextiew = holder.mProgressTextView; 56 57 // 提示开始播放的通知 58 showPlayingNotification(); 59 60 // 点击播放按钮,使用Handler每间隔1s更新一次进度 61 mHandler = new Handler(new Handler.Callback() { 62 @Override 63 public boolean handleMessage(Message msg) { 64 65 // 获取VideoView当前进度和总进度 66 int currentPosition = mPlayingVideoView.getCurrentPosition(); 67 int duration = mPlayingVideoView.getDuration(); 68 float progress = currentPosition / ((float) duration) * 100; 69 70 // 展示进度百分比 71 mPlayingProgressTextiew.setText("0%"); 72 holder.mProgressTextView.setText(progress + "%"); 73 mHandler.sendEmptyMessageDelayed(1, 1000); 74 return true; 75 } 76 }); 77 78 // 第一次让Handler更新进度 79 mHandler.sendEmptyMessageDelayed(1, 1000); 80 } 81 }); 82 83 } 84 85 // 展示开始播放的通知 86 private void showPlayingNotification() { 87 NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext); 88 // 必须设置3项:小图标,标题,内容 89 builder.setSmallIcon(R.mipmap.ic_launcher) 90 .setContentTitle("视频开始播放") 91 .setContentText("正在播放"); 92 93 Notification notification = builder.build(); 94 95 // 使用通知管理者发布通知 96 NotificationManager nm = (NotificationManager) 97 mContext.getSystemService(Context.NOTIFICATION_SERVICE); 98 ; 99 100 // 发布通知101 nm.notify(1, notification);102 }103 104 @Override105 public int getItemCount() {106 return mPathList == null ? 0 : mPathList.size();107 }108 109 static class ViewHolder extends RecyclerView.ViewHolder {110 111 Button mPlayBtn;112 VideoView mVideoView;113 TextView mProgressTextView;114 115 public ViewHolder(View itemView) {116 super(itemView);117 118 mPlayBtn = ((Button) itemView.findViewById(R.id.play_btn));119 mVideoView = ((VideoView) itemView.findViewById(R.id.video_view));120 mProgressTextView = ((TextView) itemView.findViewById(R.id.progress_tv));121 }122 }123 124 }
被忘了加权限:
1 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2 <uses-permission android:name="android.permission.INTERNET"/>