0
votes

I have wrote a simple video view renderer for android even though the video is not resize correctly but is showing video and the sound.

I tried a simple video view renderer for iOS. When I call play with the AVPlayer, but there is no error or video showing on the screen.

[assembly:ExportRenderer(typeof(VideoPlayer.VideoView), typeof(VideoViewRenderer))]

namespace VideoPlayer.iOS { public class VideoViewRenderer : ViewRenderer { public VideoViewRenderer() { }

    protected override void OnElementChanged(ElementChangedEventArgs<VideoView> e)
    {
        base.OnElementChanged(e);

        if (Control == null)
        {
            var player = new AVPlayer();
            var playerLayer = AVPlayerLayer.FromPlayer(player);

            playerLayer.BackgroundColor = new CoreGraphics.CGColor(1.0f, 1.0f, 1.0f); 
            playerLayer.Frame = new CoreGraphics.CGRect(0, 0, 300, 300);
            NativeView.Layer.AddSublayer(playerLayer);
        }

        if (e.OldElement != null)
        {
            // Unsubscribe from event handlers and cleanup any resources
            e.OldElement.StartHandler -= Start;
            e.OldElement.StopHandler -= Stop;
        }

        if (e.NewElement != null)
        {
            e.NewElement.StartHandler += Start;
            e.NewElement.StopHandler += Stop;
        }
    }


    void Start(object sender, EventArgs e)
    {
        string file = Path.Combine(NSBundle.MainBundle.BundlePath, "demo.mp4");
        //var url = new NSUrl("http://www.androidbegin.com/tutorial/AndroidCommercial.3gp");
        var asset = AVAsset.FromUrl(new NSUrl(file));
        var playerItem = new AVPlayerItem(asset);
        var playerLayer = ((AVPlayerLayer)Layer.Sublayers[0]);
        var player = playerLayer.Player; 
        player.ReplaceCurrentItemWithPlayerItem(playerItem);

        //if (player.Rate > 0.0f)
        //  player.Rate = 0.0f;

        player.Play();
    }

    void Stop(object sender, EventArgs e)
    {
        var player = ((AVPlayerLayer)NativeView.Layer.Sublayers[0]).Player;
        player.Rate = 0.0f;
    }

    public override void LayoutSubviews()
    {
        base.LayoutSubviews();

    }
}

}

Am I missing some permission or setup in the project.

1
Are you getting any sound after you start playing it? - SushiHangover
No nothing . no sound and no video... - LittleFunny
I have it working only with sound. Video still not displaying. I didn't setNativeControl - LittleFunny
Managed to display the video. I added the AVPlayerController.View to the Control - LittleFunny
You can post your solution as an answer and accept it. Please do so so people searching for unanswered questions no longer find this. Thanks! - jgoldberger - MSFT

1 Answers

0
votes

I know I'm about a year too late but I just ran into this problem and got it working so hopefully this helps anyone else that comes across this!

The key is to use AVPlayerViewController. Also, make sure your URL works too. I had a bad URL for a good while which causes nothing to show up and I felt like an idiot when I realized that was the problem.

if(Control == null)
{
    AVPlayerViewController avpvc;
    AVPlayer avp;

    var url = NSUrl.FromString("SOME URL HERE"); //or NSUrl.FromFile 
    avp = new AVPlayer(url);
    avpvc = new AVPlayerViewController();
    avpvc.Player = avp;
    avpvc.ShowsPlaybackControls = true;

    avp.Play();

    this.SetNativeControl(avpvc.View);
}