使用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連續自動播放網絡視屏