SwitchVideoActivity.java
5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package com.cnlive.demo.video;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import com.cnlive.demo.video.model.DataSource;
import com.cnlive.libs.util.Config;
import com.cnlive.libs.video.video.SwitchVideoView;
import com.cnlive.libs.video.video.base.IDataSource;
import com.cnlive.libs.video.video.base.IMediaPlayer;
import com.cnlive.libs.video.video.base.ISwitchVideoView;
import java.util.ArrayList;
import java.util.List;
public class SwitchVideoActivity extends AppCompatActivity implements IMediaPlayer.OnPreparedListener,
IMediaPlayer.OnCompletionListener, IMediaPlayer.OnErrorListener, IMediaPlayer.OnInfoListener,
IMediaPlayer.OnPlayStateListener, IMediaPlayer.OnSeekCompleteListener,
ISwitchVideoView.OnSwitchVideoPreparedListener, ISwitchVideoView.OnSwitchVideoErrorListener{
private SwitchVideoView videoView;
private int curVideoIndex=0;
private List<String> videoUrls;
private List<IDataSource> videoIdUrls;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_switch_video);
initPlay();
// startPlay(getVideoList());
startPlayId(getVideoIdList());
}
public List<String> getVideoList() {
List<String> videoList = new ArrayList<>();
videoList.add("https://mobile.cnlive.com/CnliveMobile/web/7.mp4");
videoList.add("https://mobile.cnlive.com/CnliveMobile/web/7.mp4");
videoList.add("https://mobile.cnlive.com/CnliveMobile/web/7.mp4");
videoList.add("https://mobile.cnlive.com/CnliveMobile/web/7.mp4");
videoList.add("https://mobile.cnlive.com/CnliveMobile/web/7.mp4");
return videoList;
}
private void startPlay(List<String> videoList) {
videoUrls = videoList;
curVideoIndex = 0;
videoView.setDataSource(getNextVideoPath());
videoView.switchVideo(getNextVideoPath());
}
private String getNextVideoPath() {
if (curVideoIndex >= videoUrls.size()) return null;
return videoUrls.get(curVideoIndex++);
}
public List<IDataSource> getVideoIdList() {
List<IDataSource> videoList = new ArrayList<>();
videoList.add(new DataSource("20161126",Config.TYPE_VOD,Config.TYPE_PLAY_RATE_2));
videoList.add(new DataSource("20161126",Config.TYPE_VOD,Config.TYPE_PLAY_RATE_2));
videoList.add(new DataSource("20161126",Config.TYPE_VOD,Config.TYPE_PLAY_RATE_2));
videoList.add(new DataSource("20161126",Config.TYPE_VOD,Config.TYPE_PLAY_RATE_2));
videoList.add(new DataSource("20161126",Config.TYPE_VOD,Config.TYPE_PLAY_RATE_2));
return videoList;
}
private void startPlayId(List<IDataSource> videoList) {
videoIdUrls = videoList;
curVideoIndex = 0;
videoView.setDataSource(getNextVideoId());
videoView.switchVideo(getNextVideoId());
}
private IDataSource getNextVideoId() {
if (curVideoIndex >= videoIdUrls.size()) return null;
return videoIdUrls.get(curVideoIndex++);
}
private void initPlay() {
videoView = (SwitchVideoView) findViewById(R.id.videoview);
//认证APP播放权限(使用视讯中国资源)
videoView.setOnPreparedListener(this);
videoView.setOnCompletionListener(this);
videoView.setOnPlayStateListener(this);
videoView.setOnErrorListener(this);
videoView.setOnInfoListener(this);
videoView.setOnSeekCompleteListener(this);
videoView.setOnSwitchVideoPreparedListener(this);
videoView.setOnSwitchVideoErrorListener(this);
}
private void playComplete() {
//播放完成 (视频结束或错误结束都会被触发)
}
@Override
protected void onPause() {
super.onPause();
videoView.pause();
}
@Override
protected void onResume() {
super.onResume();
videoView.start();
}
@Override
protected void onDestroy() {
super.onDestroy();
videoView.release();
}
@Override
public void onCompletion(IMediaPlayer iMediaPlayer) {
boolean isLastMedia =
videoView.getSwitchMediaPlayer() == null || iMediaPlayer.equals(videoView.getSwitchMediaPlayer());
if(isLastMedia){
playComplete();
}else{
videoView.switchVideoStart();
// videoView.switchVideo(getNextVideoPath());
videoView.switchVideo(getNextVideoId());
}
}
@Override
public boolean onError(IMediaPlayer iMediaPlayer, int i, int i1) {
videoView.releaseSwitchVideo();
return false;
}
@Override
public boolean onInfo(IMediaPlayer iMediaPlayer, int i, int i1) {
return false;
}
@Override
public void onPlayState(boolean b) {
}
@Override
public void onPrepared(IMediaPlayer iMediaPlayer) {
iMediaPlayer.start();
}
@Override
public void onSeekComplete(IMediaPlayer iMediaPlayer) {
}
@Override
public void onSwitchVideoError(IMediaPlayer iMediaPlayer, int i, int i1) {
videoView.releaseSwitchVideo();
}
@Override
public void onSwitchVideoPrepared(IMediaPlayer iMediaPlayer) {
}
}