-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathVideoController.java
More file actions
241 lines (204 loc) · 8.41 KB
/
VideoController.java
File metadata and controls
241 lines (204 loc) · 8.41 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package com.wiipu.drink.Utils;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.LinearLayout;
import com.wiipu.drink.R;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Created by changliliao on 23/12/2017.
*/
public class VideoController {
private final String TAG="VideoController";
private MediaPlayer firstPlayer;
private MediaPlayer nextPlayer;
private static MediaPlayer currentPlayer;
//资源文件列表
private ArrayList<String> videoList;
private SurfaceHolder surfaceHolder;
//所有player对象的缓存
private HashMap<String, MediaPlayer> playersCache = new HashMap<String, MediaPlayer>();
private int currentVideoIndex=0;
MediaPlayer.OnPreparedListener mediaPlayerOnPreparedListener=null;
private ExecutorService cachedThreadPool;
private static float volume = 1.0f;
public VideoController(SurfaceHolder surfaceHolder, final SurfaceView surfaceView, final LinearLayout linearLayout){
this.surfaceHolder=surfaceHolder;
videoList=FileUtil.getVedioList();
cachedThreadPool = Executors.newCachedThreadPool();
//自适应宽高
mediaPlayerOnPreparedListener = new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer arg0) {
// 首先取得video的宽和高
MediaPlayer mediaPlayer=playersCache.get(String.valueOf(currentVideoIndex%videoList.size()));
int vWidth = mediaPlayer.getVideoWidth();
int vHeight = mediaPlayer.getVideoHeight();
float vProportion=(float)vHeight/(float)vWidth;
// 该LinearLayout的父容器 android:orientation="vertical" 必须
int lw = linearLayout.getWidth();
int lh = linearLayout.getHeight();
float lProportion = (float) lh/(float) lw;
if (vWidth > lw || vHeight > lh) {
// 如果video的宽或者高超出了当前屏幕的大小,则要进行缩放
float wRatio = (float) vWidth / (float) lw;
float hRatio = (float) vHeight / (float) lh;
// 选择大的一个进行缩放
float ratio = Math.max(wRatio, hRatio);
vWidth = (int) Math.ceil((float) vWidth / ratio);
vHeight = (int) Math.ceil((float) vHeight / ratio);
// 设置surfaceView的布局参数
LinearLayout.LayoutParams lp= new LinearLayout.LayoutParams(vWidth, vHeight);
lp.gravity = Gravity.CENTER;
surfaceView.setLayoutParams(lp);
}else {
int weight;
int height;
if(vProportion<=lProportion){
weight=lw;
height=(int)Math.ceil(((float)lw/(float) vWidth)*vHeight);
}else {
height=lh;
weight=(int)Math.ceil(((float)lh/(float) vHeight)*vWidth);
}
LinearLayout.LayoutParams lp= new LinearLayout.LayoutParams(weight, height);
lp.gravity = Gravity.CENTER;
surfaceView.setLayoutParams(lp);
}
}
};
}
public void startPlay(){
initFirstPlayer();
}
/*
* 初始化播放首段视频的player
*/
private void initFirstPlayer() {
firstPlayer = new MediaPlayer();
firstPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
firstPlayer.setDisplay(surfaceHolder);
firstPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
onVideoPlayCompleted(mp);
}
});
firstPlayer.setOnPreparedListener(mediaPlayerOnPreparedListener);
//设置cachePlayer为该player对象
if(videoList.size()==1)//{
firstPlayer.setLooping(true);
initNextPlayer();
}
private void startPlayFirstVideo() {
try {
firstPlayer.setDataSource(videoList.get(0));
playersCache.put("0",firstPlayer);
firstPlayer.prepare();
currentPlayer = firstPlayer;
firstPlayer.start();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
/*
* 新开线程负责初始化负责播放剩余视频分段的player对象,避免UI线程做过多耗时操作
*/
private void initNextPlayer() {
new Thread(new Runnable() {
@Override
public void run() {
startPlayFirstVideo();
for (int i = 1; i < videoList.size(); i++) {
nextPlayer = new MediaPlayer();
// nextPlayer.setVolume( 0f , 0f );
nextPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
nextPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
onVideoPlayCompleted(mp);
}
});
nextPlayer.setOnPreparedListener(mediaPlayerOnPreparedListener);
try {
nextPlayer.setDataSource(videoList.get(i));
nextPlayer.prepare();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
//put nextMediaPlayer in cache
playersCache.put(String.valueOf(i), nextPlayer);
}
}
}).start();
}
/*
* 负责处理一段视频播放过后,切换player播放下一段视频
*/
private void onVideoPlayCompleted(final MediaPlayer mp) {
cachedThreadPool.execute(new Runnable() {
@Override
public void run() {
mp.setDisplay(null);
mp.stop();
mp.reset();
try {
mp.setDataSource(videoList.get(currentVideoIndex));
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
onVideoPlayCompleted(mp);
}
});
mp.setOnPreparedListener(mediaPlayerOnPreparedListener);
if(videoList.size()==1)//{
mp.setLooping(true);
mp.prepare();
} catch (Exception e) {
e.printStackTrace();
Set<String> set = playersCache.keySet();
for(String str : set){
playersCache.get(str).release();
}
initFirstPlayer();
return;
}
++currentVideoIndex;
if(currentVideoIndex==videoList.size())
currentVideoIndex=0;
currentPlayer = playersCache.get(String.valueOf(currentVideoIndex%videoList.size()));
if (currentPlayer != null) {
currentPlayer.seekTo(0);
currentPlayer.setVolume(volume,volume);
currentPlayer.start();
currentPlayer.setDisplay(surfaceHolder);
} else {
Log.d(TAG, "循环错误");
}
}
});
}
public void setVedioVolume(){
currentPlayer.setVolume(volume,volume);
}
private static void turnDownVolume(){
volume = 0.1f;
currentPlayer.setVolume(volume,volume);
}
private static void turnUpVolume(){
volume = 1.0f;
currentPlayer.setVolume(volume,volume);
}
}