Created
May 10, 2016 21:20
-
-
Save moagrius/4732021bf4ca5c8957b9e8a15429e2a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I could not able to resolve NotificationProvider in your code. Is there any solution or hint for it?