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

import io.vov.vitamio.MediaPlayer;
import io.vov.vitamio.widget.MediaController;
import io.vov.vitamio.widget.VideoView;

import java.io.IOException;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
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 = null;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		//Creem el layout
		super.onCreate(savedInstanceState);
		if (!io.vov.vitamio.LibsChecker.checkVitamioLibs(this))
		{
			return;
		}
		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)
		io.vov.vitamio.widget.MediaController mc = new MediaController(this);
		video.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
		    public void onCompletion(MediaPlayer mp) {
		    	//Al acabar cada ts, reproduit el seguent
		        playNext();
		    } 
		});
		mc.setOnFFLeftListener(new View.OnClickListener() {
			public void onClick(View v) {
				Log.d(TAG, "FFLEFT");
				video.stopPlayback();
				playPrevious();
			}
		});
		mc.setOnFFRightListener(new View.OnClickListener() {
			public void onClick(View v) {
				Log.d(TAG, "FFRIGHT");
				video.stopPlayback();
				playNext();
			}
		});
		video.setVideoQuality(MediaPlayer.VIDEOQUALITY_HIGH);
		//Creem un gestor HLS, carreguem el video i iniciem la reproduccio
		h = ((UpcApp)getApplication()).getHLS();
		h.setDefQuality(((UpcApp)getApplication()).getQuality());
		h.loadVideo();
		video.stopPlayback();
		video.setMediaController(mc);
		playNext();
	}
	
	public void play(String url) {
		if(url == null) {
			super.finish();
			return;
		}
		Log.d(TAG,"A reproduir: " + url);
		video.setVideoPath("file://" + url);
		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;
		}
	}
	public void playPrevious() {
		try {
			String prev = h.previous();
			play(prev);
		}
		catch(IOException e) {
			Toast.makeText(this, "No s'ha trobat el segment", Toast.LENGTH_LONG).show();
			super.finish();
			return;
		}
	}
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		if (video != null)
			video.setVideoLayout(VideoView.VIDEO_LAYOUT_SCALE, 0);
		super.onConfigurationChanged(newConfig);
	}

	// Que coño es esto gueyes???
	/*@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		//Determina el funcionament al apretar la tecla d'opcions
		getMenuInflater().inflate(R.menu.activity_video, menu);
		return true;
	}*/
}