• VideoView获取本地视频播放


    主布局:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout
     3     xmlns:android="http://schemas.android.com/apk/res/android"
     4     xmlns:app="http://schemas.android.com/apk/res-auto"
     5     xmlns:tools="http://schemas.android.com/tools"
     6     android:layout_width="match_parent"
     7     android:layout_height="match_parent"
     8     android:orientation="vertical"
     9     tools:context="net.bwie.videoview.activity.MainActivity">
    10 
    11     <Button
    12         android:id="@+id/next_btn"
    13         android:layout_width="wrap_content"
    14         android:layout_height="wrap_content"
    15         android:text="下一页"/>
    16 
    17     <Button
    18         android:id="@+id/play_btn"
    19         android:layout_width="wrap_content"
    20         android:layout_height="wrap_content"
    21         android:text="播放"/>
    22 
    23     <VideoView
    24         android:id="@+id/video_view"
    25         android:layout_width="match_parent"
    26         android:layout_height="match_parent"/>
    27 
    28 </LinearLayout>

    主函数:

     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     @Override
    15     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 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout
     3     xmlns:android="http://schemas.android.com/apk/res/android"
     4     xmlns:app="http://schemas.android.com/apk/res-auto"
     5     xmlns:tools="http://schemas.android.com/tools"
     6     android:orientation="vertical"
     7     android:layout_width="match_parent"
     8     android:layout_height="match_parent"
     9     tools:context="net.bwie.videoview.activity.VideoListActivity">
    10 
    11     <android.support.v7.widget.RecyclerView
    12         android:id="@+id/recycler_view"
    13         android:layout_width="match_parent"
    14         android:layout_height="match_parent"
    15         app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>
    16 
    17 </LinearLayout>

    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     @Override
    11     protected void onCreate(Bundle savedInstanceState) {
    12         super.onCreate(savedInstanceState);
    13         super.setContentView(R.layout.activity_video_list);
    14         initView();
    15 
    16         List<String> pathList = 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 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3               android:layout_width="match_parent"
     4               android:layout_height="wrap_content"
     5               android:orientation="vertical">
     6 
     7     <TextView
     8         android:id="@+id/progress_tv"
     9         android:layout_width="wrap_content"
    10         android:layout_height="wrap_content"
    11         android:text="当前进度"/>
    12 
    13     <Button
    14         android:id="@+id/play_btn"
    15         android:layout_width="wrap_content"
    16         android:layout_height="wrap_content"
    17         android:text="播放"/>
    18 
    19     <VideoView
    20         android:id="@+id/video_view"
    21         android:layout_width="match_parent"
    22         android:layout_height="200dp"
    23         android:layout_gravity="center_horizontal"
    24         android:visibility="invisible"/>
    25     <!--android:visibility="invisible"代表视图不可见。点击播放时才可见-->
    26 
    27 </LinearLayout>

    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 List<String> pathList;
    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<String> getPathList() {
    22         return pathList;
    23     }
    24 
    25     public void setPathList(List<String> pathList) {
    26         this.pathList = pathList;
    27     }
    28 }

    Adapter:

      1 public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.ViewHolder> {
      2 
      3     private Context mContext;
      4     private List<String> 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<String> 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     @Override
    105     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"/> 

  • 相关阅读:
    ABP无法使用异步操作,但要调用异步方法
    ABP 领域服务层学习记录
    Postman 简单使用随笔记
    Abp 模板更换数据库 版本为V5.x,遇到的问题
    C# 求两个时间的差值
    Mysql 为现有数据生成Guid
    设计模式之单例模式
    iOS基础(十八)——调整图片尺寸和大小
    iOS基础(十七)——根据一个View生成一个image
    iOS基础(十六)——动态获取label的高度
  • 原文地址:https://www.cnblogs.com/SongYongQian/p/7861485.html
Copyright © 2020-2023  润新知