Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swipe Gesture are not working in YouTubePlayerView in full screen mode

I am using the YouTube API, and I want to apply the Swipe left and right gesture on YouTubePlayerView in full screen mode.

The Swipe gestures are not working in Android version 4.0+ when YouTubePlayerView is in full screen mode.

Please help me with this. Thanks in advance.

like image 722
N Sharma Avatar asked Jun 10 '13 19:06

N Sharma


2 Answers

You can try Extending YoutubePlayerView and Override onTouchEvent and return false

like image 50
Mo Adel Avatar answered Nov 18 '22 23:11

Mo Adel


Better late than never.

The problem is the equivalent to the z-index in css. The video in fullscreen is added after the activity is started and on the most top of the view stack so everything is under it.

In this example, we are going to put a fullscreen-invisible dialog above everything so that we can attach any gesture we want to its view (layout) and execute callbacks in our activity.

  1. Wait for the video to be added to the screen. You can set a PlayerStateChangeListener to your current player and execute the following code in onVideoStarted callback method.
  2. The following code will add a transparent dialog which will be on top of the view hierarchy (even upper than the video):

    // Add listeners to YouTubePlayer instance
    player.setPlayerStateChangeListener(new PlayerStateChangeListener() {
       //... other methods 
         @Override
         public void onVideoStarted() {      
    
            // Setting style with no title (defined later in the answer)
            BasicPlayerActivity.this.mDialog = new Dialog(BasicPlayerActivity.this, R.style.AppTheme_NoActionBar_Fullscreen);     BasicPlayerActivity.this.mDialog.setContentView(R.layout.overlay_layout);
            View v = BasicPlayerActivity.this.mDialog.findViewById(R.id.container);
    
            //Adding touch listener (OR ANY LISTENER YOU WANT)  
            v.setOnTouchListener(new OnSwipeTouchListener(BasicPlayerActivity.this){        
                public void onSwipeRight() {
                    // TODO: Previous Video or whatever
                }
    
                public void onSwipeLeft() {
                    // TODO: Next video or whatever
                }
            });
    
            //Setting transparent dialog background (invisible)                                                                       
            BasicPlayerActivity.this.mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
            BasicPlayerActivity.this.mDialog.show();
    }
    });
    
    1. In you styles.xml

          <style name="AppTheme.NoActionBar">
              <item name="windowActionBar">false</item>
              <item name="windowNoTitle">true</item>
          </style>
      
          <style name="AppTheme.NoActionBar.Fullscreen">
              <item name="android:windowFullscreen">true</item>
              <item name="android:backgroundDimEnabled">false</item>
              <item name="android:backgroundDimAmount">0</item>
          </style>
      

You can also set the cancel callback or whatever to do things. It is up to you.

I hope this would help to you or anyone having this problem.

like image 38
Juan Aguilar Guisado Avatar answered Nov 18 '22 22:11

Juan Aguilar Guisado