Commit 90ebde3da40b6eae4f569615c7d819d3a8bd8064

Authored by Imanol-Mikel Barba Sabariego
1 parent 117e467b

Improves varios

src/com/upc/pbe/upcnews/BandwidthMeasurer.java
@@ -3,8 +3,8 @@ package com.upc.pbe.upcnews; @@ -3,8 +3,8 @@ package com.upc.pbe.upcnews;
3 import android.net.TrafficStats; 3 import android.net.TrafficStats;
4 4
5 //Mesura la velocitat (Bandwidth) d'Internet 5 //Mesura la velocitat (Bandwidth) d'Internet
6 -public class BandwidthMeasurer{  
7 - 6 +public class BandwidthMeasurer
  7 +{
8 public double Measure(long rxBytes, long Time){ 8 public double Measure(long rxBytes, long Time){
9 long AfterTime = System.currentTimeMillis(); 9 long AfterTime = System.currentTimeMillis();
10 double bps; 10 double bps;
src/com/upc/pbe/upcnews/Directoris.java
@@ -4,9 +4,7 @@ import java.io.IOException; @@ -4,9 +4,7 @@ import java.io.IOException;
4 import java.util.ArrayList; 4 import java.util.ArrayList;
5 5
6 import android.app.Activity; 6 import android.app.Activity;
7 -import android.content.ComponentName;  
8 import android.content.Intent; 7 import android.content.Intent;
9 -import android.net.Uri;  
10 import android.os.Bundle; 8 import android.os.Bundle;
11 import android.util.Log; 9 import android.util.Log;
12 import android.view.KeyEvent; 10 import android.view.KeyEvent;
@@ -24,7 +22,6 @@ public class Directoris extends Activity implements OnItemClickListener { @@ -24,7 +22,6 @@ public class Directoris extends Activity implements OnItemClickListener {
24 private Descarrega d = new Descarrega(); 22 private Descarrega d = new Descarrega();
25 private String URL, currentFolder; 23 private String URL, currentFolder;
26 private HTMLParser parser; 24 private HTMLParser parser;
27 - private VideoActivity videoPlayer;  
28 25
29 public void onCreate(Bundle savedInstanceState){ 26 public void onCreate(Bundle savedInstanceState){
30 //Creem el layout 27 //Creem el layout
@@ -39,7 +36,6 @@ public class Directoris extends Activity implements OnItemClickListener { @@ -39,7 +36,6 @@ public class Directoris extends Activity implements OnItemClickListener {
39 parser = new HTMLParser(URL); 36 parser = new HTMLParser(URL);
40 //Fem una llista ListView i la omplim amb la informacio trobada 37 //Fem una llista ListView i la omplim amb la informacio trobada
41 ((ListView)findViewById(R.id.listView1)).setOnItemClickListener(this); 38 ((ListView)findViewById(R.id.listView1)).setOnItemClickListener(this);
42 - videoPlayer = new VideoActivity();  
43 this.showResources(); 39 this.showResources();
44 } 40 }
45 41
@@ -78,24 +74,16 @@ public class Directoris extends Activity implements OnItemClickListener { @@ -78,24 +74,16 @@ public class Directoris extends Activity implements OnItemClickListener {
78 ArrayList<ParentList> m3u8parsed = p.parseFile(playlist); 74 ArrayList<ParentList> m3u8parsed = p.parseFile(playlist);
79 Log.d(TAG, "parsing completed"); 75 Log.d(TAG, "parsing completed");
80 HLS h = new HLS(m3u8parsed,((UpcApp)getApplication()).getLocalPath()); 76 HLS h = new HLS(m3u8parsed,((UpcApp)getApplication()).getLocalPath());
81 - h.loadVideo();  
82 - /*  
83 - * while(true)  
84 - * {  
85 - * String[] segment = h.next();  
86 - * if(segment == null)  
87 - * {  
88 - * break;  
89 - * }  
90 - * Log.d(TAG,segment[0]);  
91 - * startPlayer(segment[0],segment[1]);  
92 - */  
93 - Bundle params = new Bundle();  
94 - params.putString("url", "/sdcard/a.ts"); 77 + ((UpcApp)getApplication()).setHLS(h);
95 Intent mIntent = new Intent(this, VideoActivity.class); 78 Intent mIntent = new Intent(this, VideoActivity.class);
96 - mIntent.putExtras(params);  
97 - startActivity(mIntent);  
98 - 79 + if(((UpcApp)getApplication()).getHLS() != null)
  80 + {
  81 + startActivity(mIntent);
  82 + }
  83 + else
  84 + {
  85 + Toast.makeText(this, "HLS engine error", Toast.LENGTH_LONG).show();
  86 + }
99 } 87 }
100 catch (ErrorException e){ 88 catch (ErrorException e){
101 Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show(); 89 Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
src/com/upc/pbe/upcnews/HLS.java
1 package com.upc.pbe.upcnews; 1 package com.upc.pbe.upcnews;
2 2
3 -import java.util.ArrayList;  
4 3
  4 +import java.io.File;
  5 +import java.util.ArrayList;
5 import android.util.Log; 6 import android.util.Log;
6 7
7 //Gestor del protocol HTTP Live Streaming 8 //Gestor del protocol HTTP Live Streaming
8 -public class HLS {  
9 - 9 +public class HLS
  10 +{
10 private static final String TAG = "HLS"; 11 private static final String TAG = "HLS";
11 private ArrayList<ParentList> videos; 12 private ArrayList<ParentList> videos;
12 private ArrayList<Segment> segments; 13 private ArrayList<Segment> segments;
@@ -16,15 +17,28 @@ public class HLS { @@ -16,15 +17,28 @@ public class HLS {
16 private int currentSegment; 17 private int currentSegment;
17 private boolean endReached; 18 private boolean endReached;
18 private String localFolder; 19 private String localFolder;
19 - //private fileDownloader fd; 20 + //private FileDownloader fd;
20 private BandwidthMeasurer bm; 21 private BandwidthMeasurer bm;
21 22
22 - public HLS(ArrayList<ParentList> parsed, String localFolder){ 23 + public HLS(ArrayList<ParentList> parsed, String localFolder)
  24 + {
  25 + /*
  26 + * CLEAN DIRECTORY
  27 + */
  28 + File dir = new File(localFolder);
  29 + String[] files = dir.list();
  30 + for(int i = 0; i < files.length; i++)
  31 + {
  32 + File deleteme = new File(dir,files[i]);
  33 + Log.d(TAG,"Deleted " + files[i].toString());
  34 + deleteme.delete();
  35 + }
23 currentVideo = currentQuality = currentSegment = 0; 36 currentVideo = currentQuality = currentSegment = 0;
24 this.videos = parsed; 37 this.videos = parsed;
25 endReached = false; 38 endReached = false;
26 this.localFolder = localFolder; 39 this.localFolder = localFolder;
27 bm = new BandwidthMeasurer(); 40 bm = new BandwidthMeasurer();
  41 + //fd = new FileDownloader();
28 } 42 }
29 43
30 public void loadVideo() 44 public void loadVideo()
@@ -37,7 +51,7 @@ public class HLS { @@ -37,7 +51,7 @@ public class HLS {
37 } 51 }
38 } 52 }
39 53
40 - public String[] next() 54 + public String next()
41 { 55 {
42 Segment seg = segments.get(currentSegment++); 56 Segment seg = segments.get(currentSegment++);
43 Log.d(TAG, seg.getName() + " " + seg.getURL()); 57 Log.d(TAG, seg.getName() + " " + seg.getURL());
@@ -49,9 +63,6 @@ public class HLS { @@ -49,9 +63,6 @@ public class HLS {
49 { 63 {
50 currentQuality++; 64 currentQuality++;
51 } 65 }
52 - String[] data = new String[2];  
53 - data[0] = seg.getURL().substring(seg.getURL().lastIndexOf("/")+2, seg.getURL().length());  
54 - data[1] = seg.getName();  
55 if(currentSegment == segments.size()) 66 if(currentSegment == segments.size())
56 { 67 {
57 if(endReached) 68 if(endReached)
@@ -60,6 +71,7 @@ public class HLS { @@ -60,6 +71,7 @@ public class HLS {
60 } 71 }
61 loadVideo(); 72 loadVideo();
62 } 73 }
63 - return data; 74 +
  75 + return localFolder + seg.getURL().substring(seg.getURL().lastIndexOf("/")+1, seg.getURL().length());
64 } 76 }
65 } 77 }
66 \ No newline at end of file 78 \ No newline at end of file
src/com/upc/pbe/upcnews/UpcApp.java
@@ -15,11 +15,22 @@ public class UpcApp extends Application implements OnSharedPreferenceChangeListe @@ -15,11 +15,22 @@ public class UpcApp extends Application implements OnSharedPreferenceChangeListe
15 private String url = null; 15 private String url = null;
16 private String desc; 16 private String desc;
17 private final static String localPath = Environment.getExternalStorageDirectory().getPath() + "/UPC NEWS/"; 17 private final static String localPath = Environment.getExternalStorageDirectory().getPath() + "/UPC NEWS/";
  18 + private HLS h;
18 19
19 public String getLocalPath(){ 20 public String getLocalPath(){
20 return localPath; 21 return localPath;
21 } 22 }
22 23
  24 + public HLS getHLS()
  25 + {
  26 + return h;
  27 + }
  28 +
  29 + public void setHLS(HLS h)
  30 + {
  31 + this.h = h;
  32 + }
  33 +
23 public String getUrl() { 34 public String getUrl() {
24 url = "http://" + prefs.getString("server", getString(R.string.defaultURL)); 35 url = "http://" + prefs.getString("server", getString(R.string.defaultURL));
25 return url; 36 return url;
src/com/upc/pbe/upcnews/VideoActivity.java
1 package com.upc.pbe.upcnews; 1 package com.upc.pbe.upcnews;
2 2
3 import android.app.Activity; 3 import android.app.Activity;
  4 +import android.media.MediaPlayer;
4 import android.net.Uri; 5 import android.net.Uri;
5 import android.os.Bundle; 6 import android.os.Bundle;
  7 +import android.util.Log;
6 import android.view.Menu; 8 import android.view.Menu;
7 import android.widget.MediaController; 9 import android.widget.MediaController;
8 import android.widget.VideoView; 10 import android.widget.VideoView;
9 11
10 -public class VideoActivity extends Activity {  
11 -  
12 - VideoView video;  
13 - String url;  
14 - 12 +public class VideoActivity extends Activity
  13 +{
  14 + private VideoView video;
  15 + private HLS h;
  16 + private final static String TAG = "VideoActivity";
15 17
16 @Override 18 @Override
17 public void onCreate(Bundle savedInstanceState) { 19 public void onCreate(Bundle savedInstanceState) {
18 super.onCreate(savedInstanceState); 20 super.onCreate(savedInstanceState);
19 setContentView(R.layout.activity_video); 21 setContentView(R.layout.activity_video);
20 video = (VideoView) findViewById(R.id.videoView1); 22 video = (VideoView) findViewById(R.id.videoView1);
21 - url = this.getIntent().getExtras().getString("url");  
22 - play(); 23 + video.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
  24 + {
  25 + public void onCompletion(MediaPlayer mp)
  26 + {
  27 + play(null);
  28 + //playNext();
  29 + }
  30 + });
  31 + h = ((UpcApp)getApplication()).getHLS();
  32 + h.loadVideo();
  33 + playNext();
23 } 34 }
24 35
25 - public void play() 36 + public void play(String url)
26 { 37 {
  38 + if(url == null)
  39 + {
  40 + super.finish();
  41 + return;
  42 + }
27 video.setVideoURI(Uri.parse(url)); 43 video.setVideoURI(Uri.parse(url));
28 video.setMediaController(new MediaController(this)); 44 video.setMediaController(new MediaController(this));
29 video.start(); 45 video.start();
30 video.requestFocus(); 46 video.requestFocus();
31 } 47 }
  48 +
  49 + public void playNext()
  50 + {
  51 + String next = h.next();
  52 + Log.d(TAG,next);
  53 + //play(next);
  54 + play("/sdcard/a.ts");
  55 + }
32 56
33 @Override 57 @Override
34 public boolean onCreateOptionsMenu(Menu menu) { 58 public boolean onCreateOptionsMenu(Menu menu) {