Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Hobbit7 opened this issue Jun 20, 2024 · 0 comments

Comments

@Hobbit7
Copy link

Hobbit7 commented Jun 20, 2024

💬 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);
    }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant