Skip to content

Instantly share code, notes, and snippets.

@moagrius
Created May 10, 2016 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moagrius/4732021bf4ca5c8957b9e8a15429e2a5 to your computer and use it in GitHub Desktop.
Save moagrius/4732021bf4ca5c8957b9e8a15429e2a5 to your computer and use it in GitHub Desktop.
// add your package here
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.SurfaceTexture;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.widget.MediaController;
import android.widget.RelativeLayout;
public class VideoPlayerActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener {
private TextureView mTextureView;
private Surface mSurface;
private MediaController mMediaController;
private BackgroundAudioService mBackgroundAudioService;
private boolean mServiceIsBound;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout contentView = new RelativeLayout(this);
contentView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
showControls();
return false;
}
});
setContentView(contentView);
mTextureView = new TextureView(this);
mTextureView.setSurfaceTextureListener(this);
contentView.addView(mTextureView);
mMediaController = new MediaController(this);
mMediaController.setAnchorView(contentView);
Intent service = new Intent(this, BackgroundAudioService.class);
bindService(service, mServiceConnection, Context.BIND_AUTO_CREATE);
}
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
if(mServiceIsBound){
return;
}
BackgroundAudioService.BackgroundAudioServiceBinder binder = (BackgroundAudioService.BackgroundAudioServiceBinder) service;
mBackgroundAudioService = binder.getService();
mMediaController.setMediaPlayer(mBackgroundAudioService);
mBackgroundAudioService.setSurface(mSurface, false);
mBackgroundAudioService.playUri(Uri.parse("file:///android_asset/one.mp4"));
mServiceIsBound = true;
NotificationProvider.getOngoingVideoNotification(getApplicationContext());
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mServiceIsBound = false;
NotificationProvider.cancelAllNotifications(getApplicationContext());
if(mMediaController.isShowing()) {
mMediaController.setEnabled(false);
mMediaController.hide();
}
}
};
private void showControls(){
if(mServiceIsBound && mMediaController != null && !mMediaController.isShowing()) {
mMediaController.setEnabled(true);
mMediaController.show(0);
}
}
private void setSurface(Surface surface, boolean shouldBlock){
mSurface = surface;
if(mBackgroundAudioService != null){
mBackgroundAudioService.setSurface(mSurface, shouldBlock);
}
}
@Override
public void onPause(){
super.onPause();
}
@Override
public void onResume(){
super.onResume();
showControls();
}
@Override
protected void onDestroy() {
super.onDestroy();
NotificationProvider.cancelAllNotifications(getApplicationContext());
if(mServiceIsBound) {
mBackgroundAudioService.getExoPlayer().stop();
mBackgroundAudioService.getExoPlayer().release();
mBackgroundAudioService.stopSelf();
unbindService(mServiceConnection);
mServiceIsBound = false;
}
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
setSurface(new Surface(surfaceTexture), false);
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
setSurface(null, true);
return true;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
}
}
@saurabhkpatel
Copy link

I could not able to resolve NotificationProvider in your code. Is there any solution or hint for it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment