主布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
tools:context="net.bwie.videoview.activity.MainActivity"> <Button
android:id="@+id/next_btn"
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="播放"/> <VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/> </LinearLayout>
主函数:
public class MainActivity extends AppCompatActivity implements View.OnClickListener { protected Button mNextBtn;
protected Button mPlayBtn;
protected VideoView mVideoView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
initView();
} @Override
public void onClick(View view) {
if (view.getId() == R.id.next_btn) { VideoListActivity.startActivity(this); } else if (view.getId() == R.id.play_btn) {
String videoPath = "http://172.29.23.6:8080/video/aa.3gp";
playVideo(videoPath);
}
} private void playVideo(String videoPath) {
mVideoView.setVideoPath(videoPath);
// 设置多媒体控制器
mVideoView.setMediaController(new MediaController(this));
mVideoView.start();
} private void initView() {
mNextBtn = (Button) findViewById(R.id.next_btn);
mNextBtn.setOnClickListener(MainActivity.this);
mPlayBtn = (Button) findViewById(R.id.play_btn);
mPlayBtn.setOnClickListener(MainActivity.this);
mVideoView = (VideoView) findViewById(R.id.video_view);
}
}
activity_video_list:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="net.bwie.videoview.activity.VideoListActivity"> <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"/> </LinearLayout>
VideoListActivity:
public class VideoListActivity extends AppCompatActivity { protected RecyclerView mRecyclerView; public static void startActivity(Context context) {
Intent intent = new Intent(context, VideoListActivity.class);
context.startActivity(intent);
} @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_video_list);
initView(); List<String> pathList = new VideoBean().getPathList();
VideoListAdapter adapter = new VideoListAdapter(this, pathList);
mRecyclerView.setAdapter(adapter);
} private void initView() {
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
}
}
item_video:
<?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="播放"/> <VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:visibility="invisible"/>
<!--android:visibility="invisible"代表视图不可见。点击播放时才可见--> </LinearLayout>
Bean类:
package net.bwie.videoview.bean; import android.os.Environment; import java.util.ArrayList;
import java.util.List; public class VideoBean { private List<String> pathList; public VideoBean() {
pathList = new ArrayList<>();
for (int i = 0; i < 20; i++) {
String path = Environment.getExternalStorageDirectory()
+ "/VID_20171117_144736.3gp";
pathList.add(path);
}
} public List<String> getPathList() {
return pathList;
} public void setPathList(List<String> pathList) {
this.pathList = pathList;
}
}
Adapter:
public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.ViewHolder> { private Context mContext;
private List<String> mPathList; // 当前正在播放的VideoView
private VideoView mPlayingVideoView;
//记录正在播放的进度TextView
private TextView mPlayingProgressTextiew; private Handler mHandler; public VideoListAdapter(Context context, List<String> pathList) {
mContext = context;
mPathList = pathList;
} @Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(mContext)
.inflate(R.layout.item_video, parent, false);
return new ViewHolder(itemView);
} @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
// 获取播放地址
final String path = mPathList.get(position); // 如果VideoView滑出去时正在播放,下次复用进入的item应该停止播放并隐藏
holder.mVideoView.stopPlayback();// 停止播放
holder.mVideoView.setVisibility(View.INVISIBLE);// 设置不可见
holder.mProgressTextView.setText("0%"); // 播放按钮绑定监听器
holder.mPlayBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { // 停止上一个VideoView的播放并隐藏,才能继续播放当前的视频
if (mPlayingVideoView != null) {
mPlayingVideoView.stopPlayback();
mPlayingVideoView.setVisibility(View.INVISIBLE);
} // 播放时让VideoView可见
holder.mVideoView.setVisibility(View.VISIBLE);// 设置可见性
holder.mVideoView.setVideoPath(path);
holder.mVideoView.start(); // 记录当前正在播放的VideoView
mPlayingVideoView = holder.mVideoView;
mPlayingProgressTextiew = holder.mProgressTextView; // 提示开始播放的通知
showPlayingNotification(); // 点击播放按钮,使用Handler每间隔1s更新一次进度
mHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) { // 获取VideoView当前进度和总进度
int currentPosition = mPlayingVideoView.getCurrentPosition();
int duration = mPlayingVideoView.getDuration();
float progress = currentPosition / ((float) duration) * 100; // 展示进度百分比
mPlayingProgressTextiew.setText("0%");
holder.mProgressTextView.setText(progress + "%");
mHandler.sendEmptyMessageDelayed(1, 1000);
return true;
}
}); // 第一次让Handler更新进度
mHandler.sendEmptyMessageDelayed(1, 1000);
}
}); } // 展示开始播放的通知
private void showPlayingNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);
// 必须设置3项:小图标,标题,内容
builder.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("视频开始播放")
.setContentText("正在播放"); Notification notification = builder.build(); // 使用通知管理者发布通知
NotificationManager nm = (NotificationManager)
mContext.getSystemService(Context.NOTIFICATION_SERVICE);
; // 发布通知
nm.notify(1, notification);
} @Override
public int getItemCount() {
return mPathList == null ? 0 : mPathList.size();
} static class ViewHolder extends RecyclerView.ViewHolder { Button mPlayBtn;
VideoView mVideoView;
TextView mProgressTextView; public ViewHolder(View itemView) {
super(itemView); mPlayBtn = ((Button) itemView.findViewById(R.id.play_btn));
mVideoView = ((VideoView) itemView.findViewById(R.id.video_view));
mProgressTextView = ((TextView) itemView.findViewById(R.id.progress_tv));
}
} }
被忘了加权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>