Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Android Video Player using Xamarin.Forms

$
0
0

I am trying to get a video player working in my Xamarin.Forms app on Android. I have implemented the iOS version using a ScrollViewRenderer where I add the MPMoviePlayerController (code at bottom, file attach not working). For Android, I consulted the library for playing a video in straight Android (http://developer.xamarin.com/recipes/android/media/video/play_video/), and attempted two different tacks:
- create the VideoView and add it using AddView on the renderer itself, or the inside horizontal scrollview
- use the PageRenderer, create the XAML from the video recipe, use this.Context as Activity, and SetContentView to the XAML.

Needless to say, both versions failed, leaving just a blank screen.

Does anyone have a sample that is working in .Forms, or ideas for other approaches I might attempt? Nearing the end of my rope here.

Thanks...


public class VideoScrollViewRenderer : ScrollViewRenderer
{
protected override void OnElementChanged (VisualElementChangedEventArgs e)
{
base.OnElementChanged (e);

        if (e.OldElement == null) 
        {
            VideoScrollView thisView = (VideoScrollView)Element;

            RectangleF dispRect = new RectangleF (0, 0, (float)thisView.ParentView.Width,(float)thisView.ParentView.Height);

            var view = GetView (dispRect);

            ShowMovie (thisView.videoName, view);
        }
    }

    UIView GetView (RectangleF dispRect)
    {
        var view = NativeView;
        view.Bounds = dispRect;
        return view;
    }

    static void ShowMovie (string videoName, UIView view)
    {
        string fileDir = string.Format ("video/{0}", videoName);
        string localDocUrl = Path.Combine (NSBundle.MainBundle.BundlePath, fileDir);
        var moviePlayer = new MPMoviePlayerController (new NSUrl (localDocUrl, false));
        moviePlayer.View.Frame = view.Bounds;
        view.Add (moviePlayer.View);
        moviePlayer.Play ();
    }
}

Viewing all articles
Browse latest Browse all 77050

Trending Articles