Search code examples
iosxamarinavplayeravplayerlayeravplayerviewcontroller

AVPlayer Controls not working properly


I've been trying to implement AVPlayer but I found some issues. First, on the demo (https://developer.xamarin.com/recipes/ios/media/video_and_photos/play_a_video_using_avplayer/) there's no controls...is this normal? Controls should be enable by default(apple documentation)

On my app I override AVPlayerViewController class and the controls are visible on landscape but behind the video. So most of the slider is not visible. This is weird. Also, while playing, the button pause is actually the button play and is disabled. Tapping fullscreen shows the fullscreen view, I can hear the video playing but there's only a black screen. Here's how I'm creating the video player:

this code is in a subclass of AVPlayerViewController and I set it's frame with the parent's frame. It seems to work fine except the controls part

playerItem = new AVPlayerItem (new NSUrl(url));
player = new AVPlayer (playerItem);
playerLayer = AVPlayerLayer.FromPlayer (player);
playerLayer.Frame = View.Bounds;
View.Layer.AddSublayer (playerLayer);

Has anyone seen this before?


Solution

  • This works for me, If you are subclassing AVPlayerViewController it will already have the Player property so you don't need to create another one.

    using System;
    using AVFoundation;
    using AVKit;
    using Foundation;
    
    namespace SOText
    {
        public partial class ViewController : AVPlayerViewController
        {
            public ViewController (IntPtr handle) : base (handle)
            {
            }
    
            public override void ViewDidLoad ()
            {
                base.ViewDidLoad ();
                Player = new AVPlayer (NSUrl.FromFilename ("SampleVideo_320x240_1mb.3gp"));
                AVPlayerLayer playerLayer = AVPlayerLayer.FromPlayer (Player);
                playerLayer.Frame = View.Bounds;
                Player.Play();
            }
        }
    }
    

    Let me know if that works for you.