2

can anybody help me with a c# xamarin code sample how to include into a layout as a VideoView control a LibVLC VideoView? I want to display a short video and then, after it's finished to return on my main layout. I succeeded to include the VLC player in my project (thanks to http://xamaringuyshow.com/2019/08/23/xamarin-forms-vlc-video-payer/ ), but I want to play the video in a separate layout and return after the clip is finished.

When the clip is done (but the media player doesn't have any control buttons - to seek the video, to pause or to play - another problem...) , and I try to continue with the main thread of my application, I get a "Only the original thread that created a view hierarchy can touch its views" error and the application is crashed.

This is the part of my code with the LibVLC:

public void ReplayVideoVLC(string link)
        {
            mainActivity.SetContentView(App3.Resource.Layout.VideoFullScreen);

            _libVLC = new LibVLC();
            _mediaPlayer = new MediaPlayer(_libVLC)
            {
                EnableHardwareDecoding = true
            };

            _videoView = new LibVLCSharp.Platforms.Android.VideoView(mainActivity) { MediaPlayer = _mediaPlayer };
            mainActivity.AddContentView(_videoView, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent));            
            var media = new Media(_libVLC, link, FromType.FromLocation);            
            Core.Initialize();

            _videoView.SetZOrderOnTop(true);
            _videoView.MediaPlayer.Stopped += MediaPlayer_Stopped;
            _videoView.MediaPlayer.EncounteredError += MediaPlayer_EncounteredError;
            video_error = false;
            vlc_video_playing = true;
                try
                {            
                    _videoView.MediaPlayer.Play(media);
                }
                catch (Exception exx)
                {
                    exx = exx;
                    vlc_video_playing = false;
                }           
        }

        private void MediaPlayer_EncounteredError(object sender, EventArgs e)
        {
            //error on VLC replay...            
            video_error = true;
            Xamarin.Forms.Device.OpenUri(new Uri(client.current_video_url));
            vlc_video_playing = false;
        }

        private void MediaPlayer_Stopped(object sender, EventArgs e)
        {           
            try
            {         
                _videoView.MediaPlayer = null;
                _videoView = null;
            }
            catch (Exception exx)
            {
                //exx = exx;
            }

            if (!video_error)
            {
        mainActivity.ContinueGame();                
            }
            else
            {           
                Xamarin.Forms.Device.OpenUri(new Uri(client.current_video_url));
            }
            vlc_video_playing = false;
        }
```c#
Saamer
  • 4,687
  • 1
  • 13
  • 55
miki74
  • 21
  • 4

2 Answers2

1

You have to move the portion of the background task that updates the UI onto the main thread using Activity.RunOnUiThread. So, it's probably going to look something like this:

Activity.RunOnUiThread(_videoView.MediaPlayer.Play(media));

Saamer
  • 4,687
  • 1
  • 13
  • 55
0

Try one of the official samples, like LocalNetwork, which has navigation built-in.

but the media player doesn't have any control buttons - to seek the video, to pause or to play - another problem...

Yes it does. The mediaplayer has APIs for all of that, you can explore the docs with intellisense. If you mean UI control buttons, you want the MediaElement, in which case I'd advise you to start from this sample.

I try to continue with the main thread of my application, I get a "Only the original thread that created a view hierarchy can touch its views" error and the application is crashed.

I'd advise you to specify which line of your code exactly is causing the crash.

mfkl
  • 1,914
  • 1
  • 11
  • 21