Permalink
Browse files

Fix side by side controls

Controls did not work if shown/enabled before players where initialized and exceptions could happen if play command was issued before onPrepared.
  • Loading branch information...
1 parent 411e1b2 commit 6c01864f2321d43b125f2e980c4322d6152b9c2a @protyposis committed Nov 12, 2016
@@ -42,7 +42,7 @@
private android.widget.VideoView mAndroidVideoView;
private VideoView mMpxVideoView;
- private MediaController.MediaPlayerControl mMediaPlayerControl;
+ private MediaPlayerMultiControl mMediaPlayerControl;
private MediaController mMediaController;
@Override
@@ -58,6 +58,7 @@ protected void onCreate(Bundle savedInstanceState) {
mMediaController = new MediaController(this);
mMediaController.setAnchorView(findViewById(R.id.container));
mMediaController.setMediaPlayer(mMediaPlayerControl);
+ mMediaController.setEnabled(false);
mVideoUri = getIntent().getData();
getActionBar().setSubtitle(mVideoUri+"");
@@ -66,16 +67,30 @@ protected void onCreate(Bundle savedInstanceState) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
+ final Runnable enableMediaController = new Runnable() {
+ int preparedCount = 0;
+ @Override
+ public void run() {
+ if(++preparedCount == mMediaPlayerControl.getControlsCount()) {
+ // Enable controller when all players are initialized
+ mMediaController.setEnabled(true);
+ }
+ }
+ };
+
mAndroidVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mAndroidVideoView.seekTo(0); // display first frame
+ enableMediaController.run();
+
}
});
mMpxVideoView.setOnPreparedListener(new net.protyposis.android.mediaplayer.MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(net.protyposis.android.mediaplayer.MediaPlayer mp) {
mMpxVideoView.seekTo(0); // display first frame
+ enableMediaController.run();
}
});
@@ -122,12 +137,16 @@ protected void onStop() {
private List<MediaController.MediaPlayerControl> mControls;
public MediaPlayerMultiControl(MediaController.MediaPlayerControl... controls) {
- mControls = new ArrayList<MediaController.MediaPlayerControl>();
+ mControls = new ArrayList<>();
for(MediaController.MediaPlayerControl mpc : controls) {
mControls.add(mpc);
}
}
+ public int getControlsCount() {
+ return mControls.size();
+ }
+
@Override
public void start() {
for(MediaController.MediaPlayerControl mpc : mControls) {

0 comments on commit 6c01864

Please sign in to comment.