Baseflow / Baseflow/XamarinMediaManager

System.InvalidOperationException has been thrown. ViewController can't be null.

Open
#916 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
763
Forks
299
PR merge metrics
No merged PRs in 30d

Description

## 💬 Questions and Help

I want to use Xamarin MediaManager in my MonoGame iOS game to play videos. But the problem is that I always get this exception in the following line in Programs.cs:

`UIApplication.Main(args, null, typeof(Program));`

> System.InvalidOperationException has been thrown. ViewController can't be null.

I get the exception after this code is executed in Game1.cs:

> var gameController = Services.GetService(typeof(UIViewController)) as UIViewController;
gameController.PresentViewController(new DisplayVideoController(), true, null);

Is it possible to use MonoGame's gameController with Xamarin MediaManager?

Programs.cs:

using Foundation;
using MediaManager;
using UIKit;
using SharedCode;

namespace Testios
{
[Register("AppDelegate")]
class Program : UIApplicationDelegate
{
private static Game1 game;

internal static void RunGame()
{
game = new Game1();
game.Run();
}

static void Main(string[] args)
{
UIApplication.Main(args, null, typeof(Program));
}

public override void FinishedLaunching(UIApplication app)
{
CrossMediaManager.Current.Init();

RunGame();
}
}
}

DisplayVideoController.cs:

using MediaManager;
using MediaManager.Platforms.Ios.Video;
using UIKit;

namespace Testios
{
public class DisplayVideoController : UIViewController
{
public DisplayVideoController()
{
}

public override void ViewDidLoad()
{
var videoView = new VideoView(this.View.Frame)
{
AutoresizingMask = UIViewAutoresizing.All,
};

this.View!.AddSubview(videoView);
CrossMediaManager.Current.MediaPlayer.VideoView = videoView;
CrossMediaManager.Current.MediaItemFinished += Current_MediaItemFinished;
}

public override async void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
await CrossMediaManager.Current.Play("https://videos.pexels.com/video-files/7356431/7356431-hd_1280_720_25fps.mp4");
}

private void Current_MediaItemFinished(object? sender, MediaManager.Media.MediaItemEventArgs e)
{
this.DismissViewControllerAsync(true);
}
}
}

Game1.cs:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MediaManager;
using Microsoft.Xna.Framework.Media;

namespace SharedCode
{
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;

public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}

protected override void Initialize()
{
base.Initialize();
}

protected override async void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);

var gameController = Services.GetService(typeof(UIViewController)) as UIViewController;
gameController.PresentViewController(new DisplayVideoController(), true, null);
}

protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
}

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);
}
}
}

Contributor guide

Open the contributing guide

Research direction

Read Programs.cs, Game1.cs, and DisplayVideoController.cs alongside the reported InvalidOperationException, starting at UIApplication.Main and the LoadContent call that presents the controller. Reproduce the MonoGame iOS startup flow and verify whether the reported controller and Xamarin MediaManager initialization work together; done requires a confirmed resolution or documented limitation for this integration.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
mobile-dev
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.