Playing a video with Xamarin for iOS broken after latest update

孤街醉人 提交于 2019-12-12 10:43:12

问题


The following code in Xamarin for iOS was working fine prior to the Xamarin for iOS update to v2.0.50727

This is the code in a custom renderer in a Xamarin Forms app

 class WatchVideoRenderer : PageRenderer
{
    MPMoviePlayerController moviePlayer;

    protected override void OnElementChanged(VisualElementChangedEventArgs e)
    {
        base.OnElementChanged(e);

        var url =  new NSUrl("http://192.168.12.4:8085/MediaUploads/1/211/520140731170618/DPM202.mp4");
        moviePlayer = new MPMoviePlayerController();
        moviePlayer.ContentUrl = url;
        moviePlayer.View.Frame = new CGRect((float)((NativeView.Bounds.Width - 600) / 2), (float)((NativeView.Bounds.Height - 450) / 2), 600, 400);

        MPMoviePlayerController.Notifications.ObserveLoadStateDidChange(OnLoadStateChanged);
        MPMoviePlayerController.Notifications.ObservePlaybackDidFinish(OnPlaybackComplete);

        View.AddSubview(moviePlayer.View);

        moviePlayer.PrepareToPlay();
        moviePlayer.ShouldAutoplay = true;
        moviePlayer.Play();
    }

    private void OnLoadStateChanged(object sender, NSNotificationEventArgs e)
    {
        if (moviePlayer.LoadState == MPMovieLoadState.Playable)
        {

        }
    }

    private void OnPlaybackComplete(object sender, MPMoviePlayerFinishedEventArgs e)
    {

    }
}

As i said this was working till day before yesterday, after which I installed 2 updates on Xamarin. iOS & this is now failing. All i see is a black canvas & the video never loads. No notifications from the MPMoviePlayerController are ever raised. There is a release of this app scheduled for next week & this last minute bug is causing me headaches. Any help is really appreciated.


回答1:


I resolved a similar issue by pushing a new image context and that did the trick. I modified the code to include the BeginImageContext() and EndImageContext().

UIGraphics.BeginImageContext(new CGSize(1,1));
var url =  new NSUrl("http://192.168.12.4:8085/MediaUploads/1/211/520140731170618/DPM202.mp4");
moviePlayer = new MPMoviePlayerController();
moviePlayer.ContentUrl = url;
moviePlayer.View.Frame = new CGRect((float)((NativeView.Bounds.Width - 600) / 2), (float)((NativeView.Bounds.Height - 450) / 2), 600, 400);
UIGraphics.EndImageContext();



回答2:


Try switching to AVPlayerViewController instead of MPMoviePlayerController. Throw this code inside your OnElementChanged method.

My renderer was a view renderer and not a page renderer so you might have to tweak it a bit.

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);
} 


来源:https://stackoverflow.com/questions/29672385/playing-a-video-with-xamarin-for-ios-broken-after-latest-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!