How can I use the native iOS AVPlayer to play videos?

Kim Strasser 771 Reputation points
2024-06-21T08:48:36.4966667+00:00

I want to use the native iOS AVPlayer to play videos in full screen when the user taps on a button in my game. In addition, I want to sometimes display a video when the user reaches a certain point in my game.

Currently, the video is displayed in full screen but the problem is that the video is not removed from screen after the video has played until the end. What can I do so that the video is automatically removed from the screen when it has played until the end?

In addition, how can I play a video from my iOS project instead of a video from an url? Can I add a video file to my iOS project and play it with AVPlayer?

Bildschirmfoto 2024-06-21 um 10.46.01

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)         
        {             
            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);  
           
            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;
            PlayerViewController.EntersFullScreenWhenPlaybackBegins = true;
            PlayerViewController.ShowsPlaybackControls = false;
            var gameController = this.Services.GetService(typeof(UIViewController)) as UIViewController;
            gameController.PresentViewController(PlayerViewController, true, () =>{ 
    
            PlayerViewController.Player.Play();//autoplay when present the controller

            });
        }  
        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,095 questions
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 28,161 Reputation points Microsoft Vendor
    2024-06-25T08:30:55.1566667+00:00

    Hello,

    This is an extra question from How can I add a video view to my iOS .NET project? - Microsoft Q&A

    the problem is that the video is not removed from screen after the video has played until the end.

    Please refer to the following code:

    protected override void LoadContent()
    
      {
    
    ...
      gameController.PresentViewController(PlayerViewController, true, () =>
    
      {
    
          player.Play();
    
      });
    
    
    
      NSNotificationCenter.DefaultCenter.AddObserver(new NSString("AVPlayerItemDidPlayToEndTimeNotification"), (notification) =>
    
      {
          PlayerViewController.DismissViewController(true,null);// add observer to catch the ending event
    
      }, PlayerViewController.Player.CurrentItem);
      }
    

    In addition, how can I play a video from my iOS project instead of a video from an url? Can I add a video file to my iOS project and play it with AVPlayer?

    Yes, you can do this. For your new question, it would be best if you try to open up a new thread for it. In this way, it will make answer searching in the forum easier and be beneficial to other community members as well. And you can attach the new thread link here, we can work together to figure it out.

    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 comments No comments

0 additional answers

Sort by: Most helpful