VideoActivity.java 1.87 KB
package com.upc.pbe.upcnews;

import java.io.IOException;

import io.vov.vitamio.widget.VideoView;
import android.app.Activity;
import io.vov.vitamio.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import io.vov.vitamio.widget.MediaController;
import android.widget.Toast;

//Tercera activitat principal, executa la reproduccio del video sencer
public class VideoActivity extends Activity {
	
	private final static String TAG = "VideoActivity";
	private VideoView video;
	private HLS h;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		//Creem el layout
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_video);
		video = (VideoView) findViewById(R.id.videoView1);
		//Creem un listener associat al fi de l'activitat (el fi de cada ts)
		video.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
		    public void onCompletion(MediaPlayer mp) {
		    	//Al acabar cada ts, reproduit el seguent
		        playNext();
		    } 
		});
		//Creem un gestor HLS, carreguem el video i iniciem la reproduccio
		h = ((UpcApp)getApplication()).getHLS();
		h.setDefQuality(((UpcApp)getApplication()).getQuality());
		h.loadVideo();
		playNext();
	}
	
	public void play(String url) {
		if(url == null) {
			super.finish();
			return;
		}
		Log.d(TAG,url);
		video.setVideoURI(Uri.parse(url));
		video.setMediaController(new MediaController(this));
		video.start();
		video.requestFocus();
	}
	
	public void playNext() {
		try {
			String next = h.next();
			play(next);
		}
		catch(IOException e) {
			Toast.makeText(this, "No s'ha trobat el segment", Toast.LENGTH_LONG).show();
			super.finish();
			return;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		//Determina el funcionament al apretar la tecla d'opcions
		getMenuInflater().inflate(R.menu.activity_video, menu);
		return true;
	}
}