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 3 import android.net.TrafficStats;
4 4  
5 5 //Mesura la velocitat (Bandwidth) d'Internet
6   -public class BandwidthMeasurer{
7   -
  6 +public class BandwidthMeasurer
  7 +{
8 8 public double Measure(long rxBytes, long Time){
9 9 long AfterTime = System.currentTimeMillis();
10 10 double bps;
... ...
src/com/upc/pbe/upcnews/Directoris.java
... ... @@ -4,9 +4,7 @@ import java.io.IOException;
4 4 import java.util.ArrayList;
5 5  
6 6 import android.app.Activity;
7   -import android.content.ComponentName;
8 7 import android.content.Intent;
9   -import android.net.Uri;
10 8 import android.os.Bundle;
11 9 import android.util.Log;
12 10 import android.view.KeyEvent;
... ... @@ -24,7 +22,6 @@ public class Directoris extends Activity implements OnItemClickListener {
24 22 private Descarrega d = new Descarrega();
25 23 private String URL, currentFolder;
26 24 private HTMLParser parser;
27   - private VideoActivity videoPlayer;
28 25  
29 26 public void onCreate(Bundle savedInstanceState){
30 27 //Creem el layout
... ... @@ -39,7 +36,6 @@ public class Directoris extends Activity implements OnItemClickListener {
39 36 parser = new HTMLParser(URL);
40 37 //Fem una llista ListView i la omplim amb la informacio trobada
41 38 ((ListView)findViewById(R.id.listView1)).setOnItemClickListener(this);
42   - videoPlayer = new VideoActivity();
43 39 this.showResources();
44 40 }
45 41  
... ... @@ -78,24 +74,16 @@ public class Directoris extends Activity implements OnItemClickListener {
78 74 ArrayList<ParentList> m3u8parsed = p.parseFile(playlist);
79 75 Log.d(TAG, "parsing completed");
80 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 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 88 catch (ErrorException e){
101 89 Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
... ...
src/com/upc/pbe/upcnews/HLS.java
1 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 6 import android.util.Log;
6 7  
7 8 //Gestor del protocol HTTP Live Streaming
8   -public class HLS {
9   -
  9 +public class HLS
  10 +{
10 11 private static final String TAG = "HLS";
11 12 private ArrayList<ParentList> videos;
12 13 private ArrayList<Segment> segments;
... ... @@ -16,15 +17,28 @@ public class HLS {
16 17 private int currentSegment;
17 18 private boolean endReached;
18 19 private String localFolder;
19   - //private fileDownloader fd;
  20 + //private FileDownloader fd;
20 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 36 currentVideo = currentQuality = currentSegment = 0;
24 37 this.videos = parsed;
25 38 endReached = false;
26 39 this.localFolder = localFolder;
27 40 bm = new BandwidthMeasurer();
  41 + //fd = new FileDownloader();
28 42 }
29 43  
30 44 public void loadVideo()
... ... @@ -37,7 +51,7 @@ public class HLS {
37 51 }
38 52 }
39 53  
40   - public String[] next()
  54 + public String next()
41 55 {
42 56 Segment seg = segments.get(currentSegment++);
43 57 Log.d(TAG, seg.getName() + " " + seg.getURL());
... ... @@ -49,9 +63,6 @@ public class HLS {
49 63 {
50 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 66 if(currentSegment == segments.size())
56 67 {
57 68 if(endReached)
... ... @@ -60,6 +71,7 @@ public class HLS {
60 71 }
61 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 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 15 private String url = null;
16 16 private String desc;
17 17 private final static String localPath = Environment.getExternalStorageDirectory().getPath() + "/UPC NEWS/";
  18 + private HLS h;
18 19  
19 20 public String getLocalPath(){
20 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 34 public String getUrl() {
24 35 url = "http://" + prefs.getString("server", getString(R.string.defaultURL));
25 36 return url;
... ...
src/com/upc/pbe/upcnews/VideoActivity.java
1 1 package com.upc.pbe.upcnews;
2 2  
3 3 import android.app.Activity;
  4 +import android.media.MediaPlayer;
4 5 import android.net.Uri;
5 6 import android.os.Bundle;
  7 +import android.util.Log;
6 8 import android.view.Menu;
7 9 import android.widget.MediaController;
8 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 18 @Override
17 19 public void onCreate(Bundle savedInstanceState) {
18 20 super.onCreate(savedInstanceState);
19 21 setContentView(R.layout.activity_video);
20 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 43 video.setVideoURI(Uri.parse(url));
28 44 video.setMediaController(new MediaController(this));
29 45 video.start();
30 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 57 @Override
34 58 public boolean onCreateOptionsMenu(Menu menu) {
... ...