VideoView获取本地视频播放
主布局:
<?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获取本地视频播放的更多相关文章
- SurfaceView获取本地视频播放
1.定义 可以直接从内存或者DMA等硬件接口取得图像数据,是个非常重要的绘图容器. 它的特性是:可以在主线程之外的线程中向屏幕绘图上.这样可以避免画图任务繁重的时候造成主线程阻塞,从而提高了程序的反应 ...
- Android开发之获取本地视频和获取自拍视频
1.获取本地所有视频 public void getLoadMedia() { Cursor cursor = UILApplication.instance.getApplicationContex ...
- Android使用VideoView播放本地视频及网络视频Demo
1.xm文件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:and ...
- Win10《芒果TV》更新v3.5.0夏至版:会员尊享蓝光画质,关联本地视频播放
在Win10秋季创意者更新前夕,Win10版<芒果TV>全平台同步更新夏至版v3.5.0,新增会员蓝光画质,关联本地视频播放,进一步提升使用体验. Win10版<芒果TV>V3 ...
- android获取本地图片并显示图片
import java.io.FileNotFoundException; import android.content.ContentResolver; import android.content ...
- Linux编程获取本地IP
#include <stdio.h> #include <sys/types.h> #include <ifaddrs.h> #include <netine ...
- Java获取本地IP地址
import java.net.InetAddress; import java.net.UnknownHostException; public class IpTest { public stat ...
- 获取本地IP地址信息
2012-06-05 /// <summary> /// 获取本地IP地址信息 /// </summary> void G ...
- 用angular实时获取本地localStorage数据,实现一个模拟后台数据登入的效果
研究了一上午,终于做出了,实时获取本地localStorage来模拟注册登入~~~ <!DOCTYPE html><html><head lang="en&qu ...
随机推荐
- AndroidStudio -- AndroidStuido中找不到cache.properties文件
AndroidStuido中找不到cache.properties文件 报错信息: 16:32:10 Gradle sync failed: C:\Users\***\.gradle\caches\2 ...
- Mysql 和 Postgresql 抛开性能的对比
MySQL/MariaDB的当前版本是5.7.6(MariaDB为MySQL创建者Monty Widenius创建的一个MySQL分支),PostgreSQL的版本是9.4.1. 以下几个方面对比两者 ...
- logstash笔记(一)——redis&es
下载地址: https://www.elastic.co/downloads 版本:logstash-2.2.2 两台linux虚拟机,一台windows宿主机 shipper: 192.168.22 ...
- android的电话监听
android的电话监听 新建一个项目,结构图如下: PhoneService: package com.demo.tingdianhua; import android.app.Service; i ...
- ASP.NET MVC网站使用新浪微博账号登录
首先到http://open.weibo.com/development 注册一个开发者账号. 然后可以点微连接--网站接入 会分配App Key 和App Secret 然后点高级信息 在这里设置回 ...
- 微信小程序网络封装-简单高效
废话引言 小程序虽然出世很久了,但一直没怎么接触到小程序开发.吉他兴趣班老师想弄一个小程序发布课程信息和打卡功能,作为IT一员就自愿加入了这个小程序开发小组中.虽然小程序面向的是前端工程师,但作为移动 ...
- Docker Swarm 日常运维命令笔记
之前介绍了Docker管理工具-Swarm部署记录,这里简单总结下Docker Swarm的日常维护命令,以作为平时运维笔记. Swarm作为一个管理Docker集群的工具,首先需要将其部署起来,可以 ...
- [转]【mysql监控】查看mysql库大小,表大小,索引大小
本文转自:http://blog.sina.com.cn/s/blog_4c197d420101fbl9.html 查看所有库的大小 mysql> use information_schema; ...
- 15天学习MVC后的小结(分享经历与想法)
学习MVC已经有半个月,看了看日历,刚好半个月.分享了好几篇练习的博文:一,<创建第一个MVC应用程序> http://www.cnblogs.com/insus/p/3358560.ht ...
- ABB机器人---PCSDK简介
BB机器人为用户提供了大量便捷的二次开发及应用工具,PCSDK就是其中一项. 1) 首先,机器人使用PCSDK,必须要有pc interface选项. 2)此处举例使用C#编写简单界面,实现与机器人数 ...