首先是主页面的布局
主页面直接播放从手机内存视频
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 rootPath = Environment.getExternalStorageDirectory().getPath();// 根目录
// String fileName = "VID_20171117_144736.3gp";
// String videoPath = rootPath + "/" + fileName;
//这里是从网络获取视频
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);
}
}
点击下一页进入RecyclerView,每个item是不显示的,当点击播放时播放视频
下面是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();
ListpathList = new VideoBean().getPathList();
VideoListAdapter adapter = new VideoListAdapter(this, pathList);
mRecyclerView.setAdapter(adapter);
}
private void initView() {
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
}
}
VideoListActivity布局
VideoListAdapter适配器Adapter
public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.ViewHolder>{
private Context mContext; private ListmPathList;
// 当前正在播放的VideoView private VideoView mPlayingVideoView;
public VideoListAdapter(Context context, ListpathList) {
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);
}
@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.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;
}
});
}
@Override
public int getItemCount() {
return mPathList == null ? 0 : mPathList.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
Button mPlayBtn;
VideoView mVideoView;
public ViewHolder(View itemView) {
super(itemView);
mPlayBtn = ((Button) itemView.findViewById(R.id.play_btn));
mVideoView = ((VideoView) itemView.findViewById(R.id.video_view));
}
}
}