Skip to content

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

@Hobbit7

Description

@Hobbit7

💬 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);
    }
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions