VideoView的使用
来源:互联网 发布:张学友唱功 知乎 编辑:程序博客网 时间:2024/05/18 02:16
主布局:
<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 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); }}bean类:
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; }}适配器:
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)); } }}
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="播放"/> <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>
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); }}
<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>
阅读全文
0 0
- VideoView的使用
- VideoView的使用
- VideoView的使用
- VideoView的使用示例
- VideoView的简单使用
- 转载:VideoView的使用
- videoview使用的坑
- android videoview的使用
- VideoView的使用
- VideoView的使用
- VideoVIew的简单使用
- android VideoView的使用例程
- android中videoView的使用
- android VideoView的使用例程
- 视频播放,VideoView的使用。
- Android--VideoView的简单使用
- Android VideoView的简单使用
- VideoView与SurfaceView的使用
- cinepic怎么用 cinepic使用教程
- 二级列表购物车的全选反选
- 购物车基本功能(添加,删除)
- 基础练习 特殊的数字
- 新闻的头部与底部不变
- VideoView的使用
- Java 自定义类加载器实现插件式开发
- 集合LinkedList 的特有功能
- HashMap源码和常见问题分析以及多线程开发时的问题
- angularjs的增删改查
- Fluent NHibernate入门
- 洛谷P1466 集合 Subset Sums
- SSH人脸检测算法创新点
- codeforces 888E Maximum Subsequence (折半枚举 双向搜索)