|
1
2
|
package com.upc.pbe.upcnews;
|
|
3
|
import io.vov.vitamio.MediaPlayer;
|
|
4
|
import io.vov.vitamio.R;
|
|
5
6
7
|
import io.vov.vitamio.widget.MediaController;
import io.vov.vitamio.widget.VideoView;
|
|
8
9
|
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
|
|
10
|
import java.io.IOException;
|
|
11
12
13
|
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
|
|
14
|
|
|
15
|
import android.app.Activity;
|
|
16
|
import android.app.ProgressDialog;
|
|
17
|
import android.content.res.Configuration;
|
|
18
19
|
import android.net.TrafficStats;
import android.os.AsyncTask;
|
|
20
|
import android.os.Bundle;
|
|
21
|
import android.util.Log;
|
|
22
|
import android.view.View;
|
|
23
|
import android.widget.Toast;
|
|
24
|
|
|
25
26
27
28
|
//Tercera activitat principal, executa la reproduccio del video sencer
public class VideoActivity extends Activity {
private final static String TAG = "VideoActivity";
|
|
29
|
private VideoView video;
|
|
30
|
private HLS h = null;
|
|
31
|
private String filePath;
|
|
32
33
34
35
36
37
38
|
private BandwidthMeasurer bm;
private ProgressDialog pd;
ArrayList<String> queue;
ArrayList<String> playedQueue;
int currentPos;
boolean ended;
boolean buffering;
|
|
39
|
|
|
40
|
|
|
41
42
|
@Override
public void onCreate(Bundle savedInstanceState) {
|
|
43
|
|
|
44
|
//Creem el layout
|
|
45
|
|
|
46
|
super.onCreate(savedInstanceState);
|
|
47
48
49
50
51
|
//Necesario para las libs
if (!io.vov.vitamio.LibsChecker.checkVitamioLibs(this))
{
return;
}
|
|
52
|
setContentView(R.layout.activity_video);
|
|
53
54
55
56
57
58
|
currentPos = 0;
queue = new ArrayList<String>();
playedQueue = new ArrayList<String>();
buffering = true;
ended = false;
filePath = ((UpcApp)getApplication()).getLocalPath();
|
|
59
|
video = (VideoView) findViewById(R.id.VideoView1);
|
|
60
|
//Creem un listener associat al fi de l'activitat (el fi de cada ts)
|
|
61
|
io.vov.vitamio.widget.MediaController mc = new MediaController(this);
|
|
62
63
64
|
video.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
//Al acabar cada ts, reproduit el seguent
|
|
65
|
playNext();
|
|
66
67
|
}
});
|
|
68
69
70
|
mc.setOnFFLeftListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(TAG, "FFLEFT");
|
|
71
|
playPrevious();
|
|
72
73
74
75
76
77
78
79
80
|
}
});
mc.setOnFFRightListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(TAG, "FFRIGHT");
playNext();
}
});
video.setVideoQuality(MediaPlayer.VIDEOQUALITY_HIGH);
|
|
81
|
//Creem un gestor HLS, carreguem el video i iniciem la reproduccio
|
|
82
83
|
h = ((UpcApp)getApplication()).getHLS();
h.loadVideo();
|
|
84
|
video.setMediaController(mc);
|
|
85
86
|
bm = new BandwidthMeasurer();
downloadSegment(getNext());
|
|
87
|
}
|
|
88
|
|
|
89
90
|
public String getNext()
{
|
|
91
|
return h.next();
|
|
92
|
}
|
|
93
94
95
|
public void toggleBuffering(boolean b)
{
|
|
96
|
if(b)
|
|
97
|
{
|
|
98
99
100
101
102
103
104
|
buffering = true;
if(pd == null)
{
pd = new ProgressDialog(VideoActivity.this);
pd.setCancelable(false);
pd.setMessage(getString(R.string.buffering_text));
}
|
|
105
|
pd.show();
|
|
106
|
}
|
|
107
|
else
|
|
108
|
{
|
|
109
|
buffering = false;
|
|
110
111
112
113
114
115
116
117
118
119
120
121
122
|
pd.dismiss();
}
}
public void quitPlayer()
{
video.stopPlayback();
super.finish();
return;
}
public void buffer()
{
|
|
123
|
toggleBuffering(true);
|
|
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
video.suspend();
}
public void play(String path)
{
toggleBuffering(false);
video.setVideoPath(path);
video.start();
video.requestFocus();
}
public void playNext()
{
if(++currentPos > 0)
{
currentPos = 0;
}
if(currentPos == 0)
{
playedQueue.add(queue.get(0));
queue.remove(0);
if(ended && queue.isEmpty())
{
|
|
146
|
quitPlayer();
|
|
147
148
149
150
151
|
}
if(queue.isEmpty())
{
buffer();
}
|
|
152
153
154
155
|
else
{
play(queue.get(0));
}
|
|
156
157
158
159
160
161
162
163
164
165
166
167
|
}
else if(currentPos < 0)
{
play(playedQueue.get(playedQueue.size() + currentPos));
}
}
public void playPrevious()
{
if(playedQueue.isEmpty())
{
play(queue.get(0));
|
|
168
169
|
return;
}
|
|
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
else if(playedQueue.size() + (--currentPos) < 0)
{
currentPos++;
}
play(playedQueue.get(playedQueue.size() + currentPos));
}
public void downloadSegment(String url)
{
DescarregaSegment ds = new DescarregaSegment();
try
{
ds.execute(new URL(url));
}
catch (MalformedURLException e)
{
|
|
186
|
Toast.makeText(this, "URL malformada", Toast.LENGTH_LONG).show();
|
|
187
|
}
|
|
188
|
}
|
|
189
190
191
192
|
public void showNotFound(String url)
{
Toast.makeText(this, "No s'ha trobat el segment " + url, Toast.LENGTH_LONG).show();
}
|
|
193
|
|
|
194
|
@Override
|
|
195
196
197
198
199
|
public void onConfigurationChanged(Configuration newConfig) {
if (video != null)
video.setVideoLayout(VideoView.VIDEO_LAYOUT_SCALE, 0);
super.onConfigurationChanged(newConfig);
}
|
|
200
|
|
|
201
202
203
204
|
public class DescarregaSegment extends AsyncTask<URL, Void, Void>
{
final static String TAG = "DescarregaSegment";
private long bps;
|
|
205
206
|
boolean failed;
String url;
|
|
207
208
209
210
|
public DescarregaSegment()
{
bps = 0;
|
|
211
|
failed = false;
|
|
212
213
214
215
216
217
218
|
}
@Override
protected void onPreExecute()
{
if(queue.isEmpty() && !ended)
{
|
|
219
|
buffer();
|
|
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
}
}
@Override
protected Void doInBackground(URL... urls)
{
Long downloaded = Long.valueOf(0);
long startTime = System.currentTimeMillis();
long segmentBytes = TrafficStats.getTotalRxBytes();
String fileName = urls[0].toString().substring(urls[0].toString().lastIndexOf("/"));
// Iniciem la connexi� i creem els Streams
try
{
FileOutputStream out = new FileOutputStream(filePath + fileName,true);
BufferedInputStream in = new BufferedInputStream(urls[0].openStream());
Log.d(TAG, "\nDescarregant: \n");
Log.d(TAG, ">> URL: " + urls[0]);
byte data[] = new byte[102400];
int count;
while ((count = in.read(data)) != -1)
{
downloaded += count;
out.write(data, 0, count);
}
out.flush();
out.close();
in.close();
bps = (long) bm.Measure(segmentBytes, startTime);
Log.d(TAG, "Descarrega finalitzada");
queue.add(filePath + fileName);
}
catch(IOException e)
{
|
|
253
254
|
url = urls[0].toString();
failed = true;
|
|
255
256
257
258
259
260
261
262
263
264
265
|
this.cancel(true);
return null;
}
return null;
}
protected void onPostExecute(Void v)
{
if(ended)
{
return;
}
|
|
266
267
268
269
270
271
|
if(failed)
{
failed = false;
showNotFound(url);
}
else if(!queue.isEmpty() && buffering)
|
|
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
{
play(queue.get(0));
}
String newSegment = null;
h.updateQuality((long) bps);
newSegment = getNext();
if(newSegment == null)
{
ended = true;
}
else
{
downloadSegment(newSegment);
}
return;
}
}
|
|
290
|
}
|