public class VideoActivity extends Activity implements IVLCVout.Callback {
public final static String TAG = "IPTV";
public final static String LOCATION = "com.example.android.tvleanback.ui.VideoActivity.location";
private String mFilePath;
// display surface
private SurfaceView mSurface;
private SurfaceHolder holder;
// media player
private LibVLC libvlc;
private MediaPlayer mMediaPlayer = null;
private int mVideoWidth;
private int mVideoHeight;
private final static int VideoSizeChanged = -1;
/*************
* Activity
*************/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample);
// Receive path to play from intent
Intent intent = getIntent();
mFilePath = intent.getExtras().getString("stream");
Log.d(TAG, "Playing back " + mFilePath);
mSurface = (SurfaceView) findViewById(R.id.surface);
holder = mSurface.getHolder();
//holder.addCallback(this);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setSize(mVideoWidth, mVideoHeight);
}
@Override
protected void onResume() {
super.onResume();
createPlayer(mFilePath);
}
@Override
protected void onPause() {
super.onPause();
releasePlayer();
}
@Override
protected void onDestroy() {
super.onDestroy();
releasePlayer();
}
/*************
* Surface
*************/
private void setSize(int width, int height) {
mVideoWidth = width;
mVideoHeight = height;
if (mVideoWidth * mVideoHeight <= 1)
return;
if(holder == null || mSurface == null)
return;
// get screen size
int w = getWindow().getDecorView().getWidth();
int h = getWindow().getDecorView().getHeight();
// getWindow().getDecorView() doesn't always take orientation into
// account, we have to correct the values
boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
if (w > h && isPortrait || w < h && !isPortrait) {
int i = w;
w = h;
h = i;
}
float videoAR = (float) mVideoWidth / (float) mVideoHeight;
float screenAR = (float) w / (float) h;
if (screenAR < videoAR)
h = (int) (w / videoAR);
else
w = (int) (h * videoAR);
// force surface buffer size
holder.setFixedSize(mVideoWidth, mVideoHeight);
// set display size
LayoutParams lp = mSurface.getLayoutParams();
lp.width = w;
lp.height = h;
mSurface.setLayoutParams(lp);
mSurface.invalidate();
}
/*************
* Player
*************/
private void createPlayer(String media) {
releasePlayer();
try {
if (media.length() > 0) {
Toast toast = Toast.makeText(this, media, Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0,
0);
toast.show();
}
// Create LibVLC
// TODO: make this more robust, and sync with audio demo
ArrayList<String> options = new ArrayList<String>();
//options.add("--subsdec-encoding <encoding>");
options.add("--aout=opensles");
options.add("--audio-time-stretch"); // time stretching
options.add("-vvv"); // verbosity
options.add("--http-reconnect");
options.add("--network-caching="+6*1000);
libvlc = new LibVLC(this, options);
//libvlc.setOnHardwareAccelerationError(this);
holder.setKeepScreenOn(true);
// Create media player
mMediaPlayer = new MediaPlayer(libvlc);
mMediaPlayer.setEventListener(mPlayerListener);
// Set up video output
final IVLCVout vout = mMediaPlayer.getVLCVout();
vout.setVideoView(mSurface);
//vout.setSubtitlesView(mSurfaceSubtitles);
vout.addCallback(this);
vout.attachViews();
Media m = new Media(libvlc, Uri.parse(media));
mMediaPlayer.setMedia(m);
mMediaPlayer.play();
} catch (Exception e) {
Toast.makeText(this, "Error creating player!", Toast.LENGTH_LONG).show();
}
mSurface.getRootView().setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
Float p = event.getX()/size.x;
Long pos = (long) (mMediaPlayer.getLength() / p);
if (mMediaPlayer.isSeekable()) {
//mLibVLC.setTime( pos );
mMediaPlayer.setPosition(p);
} else {
}
}
return true;
}
});
}
// TODO: handle this cleaner
private void releasePlayer() {
if (libvlc == null)
return;
mMediaPlayer.stop();
final IVLCVout vout = mMediaPlayer.getVLCVout();
vout.removeCallback(this);
vout.detachViews();
holder = null;
libvlc.release();
libvlc = null;
mVideoWidth = 0;
mVideoHeight = 0;
}
/*************
* Events
*************/
private MediaPlayer.EventListener mPlayerListener = new MyPlayerListener(this);
public void onNewLayout(IVLCVout vout, int width, int height, int visibleWidth, int visibleHeight, int sarNum, int sarDen) {
if (width * height == 0)
return;
// store video size
mVideoWidth = width;
mVideoHeight = height;
setSize(mVideoWidth, mVideoHeight);
}
@Override
public void onSurfacesCreated(IVLCVout vout) {
}
@Override
public void onSurfacesDestroyed(IVLCVout vout) {
}
private static class MyPlayerListener implements MediaPlayer.EventListener {
private WeakReference<VideoActivity> mOwner;
public MyPlayerListener(VideoActivity owner) {
mOwner = new WeakReference<VideoActivity>(owner);
}
@Override
public void onEvent(MediaPlayer.Event event) {
VideoActivity player = mOwner.get();
Log.d(TAG, "Player EVENT");
switch(event.type) {
case MediaPlayer.Event.EndReached:
Log.d(TAG, "MediaPlayerEndReached");
player.releasePlayer();
break;
case MediaPlayer.Event.EncounteredError:
Log.d(TAG, "Media Player Error, re-try");
//player.releasePlayer();
break;
case MediaPlayer.Event.Playing:
case MediaPlayer.Event.Paused:
case MediaPlayer.Event.Stopped:
default:
break;
}
}
}
public void onHardwareAccelerationError(IVLCVout vout) {
// Handle errors with hardware acceleration
Log.e(TAG, "Error with hardware acceleration");
this.releasePlayer();
Toast.makeText(this, "Error with hardware acceleration", Toast.LENGTH_LONG).show();
}
}