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

Xamarin.Forms VideoView showing black box or crashing

$
0
0

Currently we're working on a xamarin.forms videoplayer for android and ios. We're using this xamarin forum topic as a reference. The same black screen problem is present there as well but no solution has been given so far. We use a custom renderer for the videoplayer:

[assembly: ExportRenderer(typeof(MyApp.MyVideoView), typeof(MyVideoViewRenderer))]

namespace MyApp
{

    public class MyVideoViewRenderer : ViewRenderer<MyVideoView,VideoView>, ISurfaceHolderCallback
    {
        public VideoView videoview;
        public MediaPlayer player;

        public MyVideoViewRenderer ()
        {
            Console.WriteLine ("VideoViewRenderer loaded");
        }

        public void SurfaceChanged (ISurfaceHolder holder, global::Android.Graphics.Format format, int width, int height)
        {

        }

        public void SurfaceDestroyed (ISurfaceHolder holder)
        {

        }

        protected override void OnElementChanged (ElementChangedEventArgs<MyVideoView> e)
        {
            base.OnElementChanged (e);
            e.NewElement.StopAction = () => {
                this.player.Stop();
                this.Control.StopPlayback ();
            };

            videoview = new VideoView (Context); //Context

            base.SetNativeControl (videoview);
            Control.Holder.AddCallback (this);
            player = new MediaPlayer();
            play (e.NewElement.FileSource);

        }

        void play(string fullPath)
        {
            AssetFileDescriptor afd = Forms.Context.Assets.OpenFd(fullPath);
            if (afd != null)
            {
                if (player != null) {
                    player = new MediaPlayer ();
                }

                player = new MediaPlayer ();
                player.SetDataSource(afd.FileDescriptor, afd.StartOffset, afd.Length);
                player.Prepare();
                player.Start();
                player.PrepareAsync();

            }
        }

        public void SurfaceCreated (ISurfaceHolder holder)
        {
            player.SetDisplay(holder);
        }    
    }
}

Our VideoView class is as follows:

namespace MyApp
{
    public class MyVideoView : View
    {
        public Action StopAction;

        public MyVideoView ()
        {
            Console.WriteLine("VideoView loaded");
        }

        public static readonly BindableProperty FileSourceProperty = BindableProperty.Create<MyVideoView,string>(p => p.FileSource,string.Empty);

        public string FileSource 
        {
            get { return (string)GetValue (FileSourceProperty); }
            set { SetValue (FileSourceProperty, value); }
        }

        public void Stop()
        {
            if(StopAction != null)
                StopAction ();
        }
    }
}

We implement the videoplayer using the following code:

//<localControls:VideoView FileSource="video.mp4" Grid.RowSpan="2" x:Name="video"></localControls:VideoView>

MyVideoView videoplayer = new MyVideoView();
videoplayer.FileSource= "sample.mp4";
videoplayer.WidthRequest = 300;
videoplayer.HeightRequest = 300;

When running our code we receive an error saying:

[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] Java.Lang.IllegalStateException: Exception of type 'Java.Lang.IllegalStateException' was thrown.
[MonoDroid] at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in /Users/builder/data/lanes/2307/f7b9e87d/source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:61
[MonoDroid] at Android.Runtime.JNIEnv.CallVoidMethod (intptr,intptr) [0x00062] in /Users/builder/data/lanes/2307/f7b9e87d/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:554
[MonoDroid] at Android.Media.MediaPlayer.PrepareAsync () [0x00043] in /Users/builder/data/lanes/2307/f7b9e87d/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.Media.MediaPlayer.cs:2432
[MonoDroid] at MyApp.MyVideoViewRenderer.play (string) [0x00075] in /Users/jeroenboumans/Documents/Git/MyApp/Frontend/App/MyApp/Droid/VideoViewRenderer.cs:74

and so on...

If the code inside the play-method inside the MyVideoViewRenderer class is commented it shows a black box instead of a videoplayer. If uncommented (as in example) it crashes showing the errors above.

Any ideas on this?


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>