Xamarin.Forms AVPlayer exit fullscreen from method
I have a Xamarin application which plays a video for 30 seconds and then shows a dialog. The user can continue to watch (dismiss the dialog) or click an action in the dialog (which then should close full-screen and navigate elsewhere in the app).
I have this working on the simulator. However, when I run the following code on an real iOS device, the app crashes without any stacktrace.
ViewModel.cs
var openLink = await _userDialogs.ConfirmAsync("...", cancelText: "Dismiss", okText: "Yes");
if (openLink)
{
await _navigationService.Close(scope);
MessagingCenter.Send(Application.Current, "CloseFullscreen");
}
VideoRenderer.cs
_playerViewController = new AVPlayerViewController()
{
ExitsFullScreenWhenPlaybackEnds = true
};
player = new AVPlayer();
_playerViewController.Player = player;
...
SetNativeControl(_playerViewController.View);
MessagingCenter.Subscribe<Xamarin.Forms.Application>(this, "CloseFullscreen", (app) =>
{
BeginInvokeOnMainThread(() =>
{
var selectorName = "exitFullScreenAnimated:completionHandler:";
var selector = new ObjCRuntime.Selector(selectorName);
if (_playerViewController.RespondsToSelector(selector))
_playerViewController.PerformSelector(selector, NSObject.FromObject(true), 0);
});
});
Please note that I am not using the CrossMediaManager plugin due to this bug: https://github.com/Baseflow/XamarinMediaManager/issues/629
Source: Xamarin.ios Questions