How can I play a video from a folder in my iOS project with the native iOS AVPlayer?

Kim Strasser 811 Reputation points
2024-06-25T22:54:11.0433333+00:00

I want to add a video file, for example an .mp4 file, to my iOS project. Can I create a folder named Resources in my iOS project, adding the .mp4 file into this folder and playing it with AVPlayer?

Bildschirmfoto 2024-06-21 um 10.46.01

How can I play a video from my iOS project instead of a video from an url with AVPlayer?

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, () =>
{
    player.Play();
});
NSNotificationCenter.DefaultCenter.AddObserver(new NSString("AVPlayerItemDidPlayToEndTimeNotification"), (notification) =>
{
    PlayerViewController.DismissViewController(true, null);
}, PlayerViewController.Player.CurrentItem);
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,849 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,176 questions
0 comments No comments
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 28,876 Reputation points Microsoft Vendor
    2024-06-26T06:27:33.72+00:00

    Hello,

    How can I play a video from my iOS project instead of a video from an url with AVPlayer?

    Please drag your video file into your iOS project, then set the build action to BundleResource

    After that, you can play the video with the following code:

    var playURL = NSBundle.MainBundle.GetUrlForResource("aaa","mp4");// the file name and file extension
    var player = new AVPlayer(playURL);
    

    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