主布局:

 <?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"/>

VideoView获取本地视频播放的更多相关文章

  1. SurfaceView获取本地视频播放

    1.定义 可以直接从内存或者DMA等硬件接口取得图像数据,是个非常重要的绘图容器. 它的特性是:可以在主线程之外的线程中向屏幕绘图上.这样可以避免画图任务繁重的时候造成主线程阻塞,从而提高了程序的反应 ...

  2. Android开发之获取本地视频和获取自拍视频

    1.获取本地所有视频 public void getLoadMedia() { Cursor cursor = UILApplication.instance.getApplicationContex ...

  3. Android使用VideoView播放本地视频及网络视频Demo

    1.xm文件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:and ...

  4. Win10《芒果TV》更新v3.5.0夏至版:会员尊享蓝光画质,关联本地视频播放

    在Win10秋季创意者更新前夕,Win10版<芒果TV>全平台同步更新夏至版v3.5.0,新增会员蓝光画质,关联本地视频播放,进一步提升使用体验. Win10版<芒果TV>V3 ...

  5. android获取本地图片并显示图片

    import java.io.FileNotFoundException; import android.content.ContentResolver; import android.content ...

  6. Linux编程获取本地IP

    #include <stdio.h> #include <sys/types.h> #include <ifaddrs.h> #include <netine ...

  7. Java获取本地IP地址

    import java.net.InetAddress; import java.net.UnknownHostException; public class IpTest { public stat ...

  8. 获取本地IP地址信息

    2012-06-05    /// <summary>         /// 获取本地IP地址信息         /// </summary>         void G ...

  9. 用angular实时获取本地localStorage数据,实现一个模拟后台数据登入的效果

    研究了一上午,终于做出了,实时获取本地localStorage来模拟注册登入~~~ <!DOCTYPE html><html><head lang="en&qu ...

随机推荐

  1. webstorm无法显示左边文件夹目录的解决方法

    webstorm无法显示左边文件夹目录的解决方法 方法一 view-->Tool Windows-->Project 就可以显示或者关闭 方法二 1.删除webstorm的配置文件夹 2. ...

  2. Metasploit数据库问题汇总

    数据库在metaspoit中是相当重要的,当做一个大型渗透测试项目的时候,收集到的信息是相当大的,当和你的同伴一起协同作战的时候,你们可能 在不同的地方,所以数据共享很重要了!而且Metasploit ...

  3. 搭建Golang开发环境

    Go语言是谷歌 2009 年首次推出并在 2012 年正式发布的一种全新的编程语言,可以在不损失应用程序性能的情况下降低代码的复杂性.谷歌首席软件工程师罗布派克(Rob Pike)说:我们之所以开发 ...

  4. WPF绑定的ListBox获取ListBoxItem及GoToState应用

    现公司项目中需要制作一个扇形菜单,菜单项是用ListBox重写Style实现的,其数据是绑定的.菜单的每一项都有Normal,MouseOver和Selected三种状态,这三种状态当然可以通过鼠标移 ...

  5. Oracle SQL语句执行步骤

    转自:http://www.cnblogs.com/quanweiru/archive/2012/11/09/2762345.html Oracle中SQL语句执行过程中,Oracle内部解析原理如下 ...

  6. centos7之使用最新版的kubeadm体验k8s1.12.0

    1.环境准备 centos7 .docker-ce18.06.1-ce.kubeadm.kubelet.kubectl 2.安装 yum安装,准备repo文件 docker: [docker-ce-s ...

  7. 进程监控工具supervisor

    supervisor是一个python编写的进程管理工具, 可以方便的管理和监控进程. supervisor分为服务端supervisord和客户端supervisorctl. supervisor由 ...

  8. C#Redis 事务操作

    一.理论 还是抄前辈的理论知识. 和众多其它数据库一样,Redis作为NoSQL数据库也同样提供了事务机制.在Redis中,MULTI/EXEC/DISCARD/WATCH这四个命令是我们实现事务的基 ...

  9. .Net Core使用Socket与树莓派进行通信

    前言 去年买的树莓派一直放在抽屉里吃灰,前些阵子Debian 9发布,也不出意外的支持了树莓派. 于是重新拿出读卡器又重新了装上了Debian桌面版系统. 介绍 现在这个东西目前的程度只是了解一下Py ...

  10. JOffice中的权限管理--功能粒度的权限管理配置

    JOffice中的权限管理是基于角色的管理策略,采用Spring Security2的配置方式,同时能够结合EXT3来进行整个系统的权限管理,通过使用配置文件,进行整个系统的功能集中管理,包括系统左边 ...