iOS Question Automatically control VideoView fullscreen mode based on orientation

Jeff Rowberg

New Member
Licensed User
I'm using B4i 3.50 with a VideoView (iMedia library) and attempting to have fullscreen mode automatically enabled or disabled based on whether the device is in portrait or landscape mode. It's almost working, but the dimensions reported while the video is in fullscreen mode are incorrect, even though the Resize event is firing at the expected times.

Here is the code I have to control fullscreen mode:

B4X:
Private Sub PageHome_Resize(Width As Int, Height As Int)
    Log("PageHome_Resize(), Width=" & Width & ", Height=" & Height)
    Dim vvo As NativeObject = vv
    Dim player As NativeObject = vvo.GetField("player")
    If Width > Height Then
        Log("Enabling fullscreen")
        player.RunMethod("setFullscreen:animated:", Array(True, False))
    Else
        Log("Disabling fullscreen")
        player.RunMethod("setFullscreen:animated:", Array(False, False))
    End If
End Sub

Here is the relevant output log data at first boot:

B4X:
Application_Start
Application_Active
PageHome_Appear()
PageHome_Resize(), Width=375, Height=603
Disabling fullscreen
Video ready

Now I rotate to landscape mode (iPhone 6 with iOS 10.2):

B4X:
PageHome_Resize(), Width=667, Height=343
Enabling fullscreen

That is also correct, and the video switches to fullscreen mode as intended. Now I switch back to portrait, but look at the numbers:

B4X:
PageHome_Resize(), Width=667, Height=331
Enabling fullscreen

The values still indicate landscape orientation, although height has changed by a value of 12. I can only imagine this is because the actual page is obstructed by the player, but the Resize event is still firing even though the page isn't visible.

If I tap the "exit fullscreen" icon manually in the player, then PageHome reappears and the correct width and height are given:

B4X:
PageHome_Appear()
PageHome_Resize(), Width=375, Height=603
Video width=375, height=210.7728
Disabling fullscreen

...but this defeats the purpose, since it requires user intervention other than simply rotating the device.

Hopefully there is some solution:
  1. Fix the Resize event to detect/show the right values
  2. Obtain orientation data from the media player somehow
  3. Obtain orientation data from the app or device rather than page dimensions
Actually, #3 is ideal. I will look for this mechanism in the meantime.
 
Last edited:

Jeff Rowberg

New Member
Licensed User
Fixed! I used some native Objective-C code pulled from this StackOverflow question:

http://stackoverflow.com/questions/5559652/how-do-i-detect-the-orientation-of-the-device-on-ios

Here's the relevant code in the application:

B4X:
#If OBJC
#define dDeviceOrientation [[UIDevice currentDevice] orientation]
#define isPortrait  UIDeviceOrientationIsPortrait(dDeviceOrientation)
#define isLandscape UIDeviceOrientationIsLandscape(dDeviceOrientation)
#define isFaceUp    dDeviceOrientation == UIDeviceOrientationFaceUp   ? YES : NO
#define isFaceDown  dDeviceOrientation == UIDeviceOrientationFaceDown ? YES : NO

- (BOOL) isLandscapeOrientation {
    return isLandscape;
}
#end if

Private Sub PageHome_Resize(Width As Int, Height As Int)
    Log("PageHome_Resize(), Width=" & Width & ", Height=" & Height)
    Dim jo As NativeObject = Me
    Dim vvo As NativeObject = vv
    Dim player As NativeObject = vvo.GetField("player")
    If jo.RunMethod("isLandscapeOrientation", Null).AsBoolean Then
        Log("Enabling fullscreen")
        player.RunMethod("setFullscreen:animated:", Array(True, False))
    Else
        Log("Disabling fullscreen")
        player.RunMethod("setFullscreen:animated:", Array(False, False))
    End If
End Sub
 
Upvote 0
Top