I have a Xamarin.Forms app that displays a video player page with native video players using DependencyService calls. Currently the video player works for Windows Phone using a renderer and custom control class, but when I try to mimick the same functionality with iOS, it plays the sound, but doesn't show the video.
Does anyone know of an example that contains the necessary custom renderer classes and controls that allow iOS to display a video with controls? Here is what I'm trying to use:
[assembly: ExportRenderer(typeof(Example.Pages.VideoPlayerPage), typeof(Example.iOS.VideoPageRenderer))]
namespace Example.iOS
{
class VideoPageRenderer : PageRenderer
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
var page = e.NewElement as VideoPlayerPage;
var newPage = new NativeVideoPage(page.url, page.width, page.height);
this.AddChildViewController(newPage);
}
}
}
VideoPlayerPage is a type of Content Page used to display the control in Xamarin.Forms.
public class NativeVideoPage : UIViewController
{
public NativeVideoPage(string Url, double Width, double Height)
{
MPMoviePlayerController videoPlayer = new MPMoviePlayerController();
videoPlayer.ShouldAutoplay = true;
videoPlayer.ContentUrl = new NSUrl(Url);
View.Add(videoPlayer.View);
videoPlayer.SetFullscreen(true, true);
videoPlayer.PrepareToPlay();
videoPlayer.Play();
}
}
NativeVideoPage is the platform specific page for the Xamarin.Forms Content Page (VideoPlayerPage).
public class VideoPlayer_iOS : IPlayVideo
{
public VideoPlayer_iOS() {}
public void PlayVideo(Video video)
{
MPMoviePlayerController videoPlayer = new MPMoviePlayerController
{
ContentUrl = new NSUrl(video.Url),
ShouldAutoplay = true
};
videoPlayer.PrepareToPlay();
}
This is the custom control i'm trying to use.