使用videoview连续自动播放网络视屏

Posted 小树苗、

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用videoview连续自动播放网络视屏相关的知识,希望对你有一定的参考价值。

需求:网络请求接口,实现自动依次播放视频

1:xml布局文件

<VideoView
android:id="@+id/video"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>

2.MainActivity页面,开启线程请求视屏接口

线程请求接口

public final static String URL="";//IP地址

public static String VIDEO=URL+"/JCPCManager/getvideo/getVideoList.do";//视屏接口
public static String PERPATH=URL+"/file/";//具体的每一个视屏的地址

HttpTools.getRequest请求方法

 

private static HttpURLConnection conn;
private static String result;
public static String getRequest(String strUrl){
// 1.定义请求url
URL url;
HttpURLConnection conn = null;
try {
url = new URL(strUrl);
conn = (HttpURLConnection) url
.openConnection();
// 3.设置一些请求的参数
conn.setRequestMethod("GET");
// 设置字符集
conn.setRequestProperty("Charset", "UTF-8");
// 设置文件类型
conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
// 设置请求参数,可通过Servlet的getHeader()获取
conn.setRequestProperty("Cookie", "AppName=" + URLEncoder.encode("你好", "UTF-8"));
// 设置自定义参数
conn.setRequestProperty("MyProperty", "this is me!");
try {
int code = conn.getResponseCode(); // 服务器的响应码 200 OK //404 页面找不到
// // 503服务器内部错误
Log.e("code===========", code+"");
if (code == 200) {
InputStream is = conn.getInputStream();
// 把is的内容转换为字符串
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = is.read(buffer)) != -1) {
bos.write(buffer, 0, len);
result = new String(bos.toByteArray());
/*
* 这个result就是你请求完了后台的返回值
*/
System.out.println("result"+result);
Log.e("result==========", result);
}
bos.close();
is.close();
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 2.建立一个http的连接
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}

3.handler线程

private static int currentVideo=0;
public Handler handler = new Handler() {

private MediaController mc;

public void handleMessage(Message msg) {
switch (msg.what) {
case 0://请求到视屏接口,然后做操作
videoPaths = msg.getData().getString("requestVideoPaths");
Gson gson= new Gson();
videoPathBean=gson.fromJson(videoPaths,VideoPathBean.class);
String code=videoPathBean.getCode();
if ("100".equals(code)) {
mList = new ArrayList<VideoBean>();
mc = new MediaController(MainActivity.this);
for (int i = 0; i <videoPathBean.getData().size(); i++) {
String videoPath=videoPathBean.getData().get(i).getVideoPath();
//视屏播放地址 HttpTools.PERPATH+videoPath;
mList.add(new VideoBean(HttpTools.PERPATH+videoPath));
}


videoview.setVideoPath(mList.get(currentVideo).getUrl());
videoview.setMediaController(mc);
videoview.requestFocus();
try {

videoview.start();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
videoview.setOnCompletionListener(new OnCompletionListener() {

@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
if (mList.size()==1) {
videoview.start();
}else{
nextVideo();//播放下一个视屏
}
}


private void nextVideo() {
// TODO Auto-generated method stub
currentVideo++;
if (currentVideo==mList.size()) {
currentVideo=0;
}
videoview.setVideoPath(mList.get(currentVideo).getUrl());
videoview.setMediaController(mc);
videoview.requestFocus();
videoview.start();
}

});

}


break;

}
}
};

4.VideoBean实体类

public class VideoBean implements Serializable {

private static final long serialVersionUID = 1L;
private String url;

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public VideoBean(String url) {
super();
this.url = url;
}

}

5.VideoPathBean实体类


public class VideoPathBean {

/**
* code : 100
* msg : 请求成功
* data : [{"videoPath":"/video//123.mp4","videoId":153,"videoName":"123.mp4"},{"videoPath":"/video//11.mp4","videoId":154,"videoName":"11.mp4"},{"videoPath":"/video//12.mp4","videoId":155,"videoName":"12.mp4"}]
*/

private String code;
private String msg;
private List<DataBean> data;

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

public List<DataBean> getData() {
return data;
}

public void setData(List<DataBean> data) {
this.data = data;
}

public static class DataBean {
/**
* videoPath : /video//123.mp4
* videoId : 153
* videoName : 123.mp4
*/

private String videoPath;
private int videoId;
private String videoName;

public String getVideoPath() {
return videoPath;
}

public void setVideoPath(String videoPath) {
this.videoPath = videoPath;
}

public int getVideoId() {
return videoId;
}

public void setVideoId(int videoId) {
this.videoId = videoId;
}

public String getVideoName() {
return videoName;
}

public void setVideoName(String videoName) {
this.videoName = videoName;
}
}


}

 

以上是关于使用videoview连续自动播放网络视屏的主要内容,如果未能解决你的问题,请参考以下文章

MediaPlayer音乐播放器上一首下一首播放停止自动下一首进度条

Android 视频播放器 :使用VideoView播放视频

Android 视频播放器 VideoView 的使用,播放本地视频 和 网络 视频

谷歌浏览器视频怎么带声音自动播放?

有趣的安卓视频播放器(VideoView)

Android VideoView播放网络视频简介(转)