How can I add a video view to my iOS .NET project?

Kim Strasser 811 Reputation points
2024-06-12T11:59:53.5666667+00:00

I want to play a video in my iOS .NET project. I have installed the NuGet package XamarinMediaManager version 1.2.2: https://github.com/Baseflow/XamarinMediaManager

But the problem is that I only hear the sound of the video when I debug on my iPad.

I have tried to right click my iOS project and Add-->New File-->iOS View Controller to my project but I don't know how to use it to create a subview for my video.

How can I create a video view in my iOS .NET project so that I can use it with XamarinMediaManager version 1.2.2?

I want to create a similar video view in my iOS project than in my Android project: https://learn.microsoft.com/en-us/answers/questions/1693485/how-can-i-find-out-if-my-video-view-is-on-top-of-m?comment=answer-1538504&page=1#comment-1590286

Program.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();
        }
    }
}


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);
            await CrossMediaManager.Current.Play("https://videos.pexels.com/video-files/7356431/7356431-hd_1280_720_25fps.mp4");
        }
        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            base.Draw(gameTime);
        }
    }
}

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,134 questions
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 28,321 Reputation points Microsoft Vendor
    2024-06-20T08:22:13.9466667+00:00

    Hello,

    Why am I getting the exception?

    This exception is thrown by the third-party package, please see their source code- https://github.com/Baseflow/XamarinMediaManager/blob/develop/MediaManager/Platforms/Ios/Video/VideoView.cs#L36

    Because you are using MonoGame, and MonoGame has its own method to get the current controller, the rootcontroller will be null in MediaManager.

    For this, please contact the owners of MediaManager to ask if there are any ways to avoid their GetCurrentUIViewController() method and pass the gameController of MonoGame.

    (Note: Third-party tools are not supported on Q&A, please get further help from them)

    In other way, you could try to present an AVPlayerViewController (without MediaManager package)

    protected override void LoadContent()
    {
         _spriteBatch = new SpriteBatch(GraphicsDevice);
     
         var player = new AVPlayer(NSUrl.FromString("https://videos.pexels.com/video-files/7356431/7356431-hd_1280_720_25fps.mp4"));
         var PlayerViewController = new AVPlayerViewController();
         PlayerViewController.Player = player;
     
         var gameController = this.Services.GetService(typeof(UIViewController)) as UIViewController;
         gameController.PresentViewController(PlayerViewController, true, null);
     
    }
    

    Best Regards,

    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful