Xamarin MediaElement won't release video source

Viewed 881

Hi guys I am using MediaElement in Xamarin for video playback of local device files.

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/mediaelement

The issue I am having is being able to release the video source for MediaElement. I call Stop() and set the Source property to null, yet the last video is still loaded into the MediaElement with playback capabilities.

Am I missing something or is this a bug?

Here is some of the related code:

                             <MediaElement
                                HeightRequest="200"
                                ShowsPlaybackControls="True"
                                BackgroundColor="#141d3d"
                                Grid.Row="1"
                                Aspect="AspectFill"
                                AutoPlay="True"
                                IsVisible="{Binding IsVideoVisible}"
                                Source="{Binding AnprVidSource}"
                                x:Name="meAnpr"
                                KeepScreenOn="True" />

Assigning a local address to the source property.

AnprVidSource = UserVideo.Path;

Code trying to clear the MediaElements source which seems to do nothing

            meAnpr.Stop();
            meAnpr.IsEnabled = false;
            viewModel.AnprVidSource = null;
3 Answers

I started using MediaElement and now i think i chose the wrong route.This bug is still active.

Once a MediaElement is created,it cannot be set to Hide.IsVisible = false and Source=null has no effect. So,a workaround i found to atleast make it hide is using Opacity.

Also, you can't draw an overlay over MediaElement,because for some reason , MediaElement always remains at top.

In my case, i need to show VideoRecorder and MediaElement alternatively and sometimes Opacity patch does not work too.So, workaround for this:

videoPlayer.PropertyChanged += VideoPlayer_PropertyChanged;
private void VideoPlayer_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        
        if(e.PropertyName == nameof(IsVisible))
        {
          if(videoPlayer.IsVisible){
              videoPlayer.WidthRequest = 500; videoPlayer.HeightRequest = 500;
          }else{
               videoPlayer.WidthRequest = 0; videoPlayer.HeightRequest = 0;
          }
        }            
    }

Even if the above solution do not work for you, you will have to create a new MediaElelemnt whenever required and remove the old one.

Maybe this will help others.

Wanted to show that the video had not yet been set and show an icon.

So whenever I needed to set the MediaElement to null or blank, I set the source to a short mp4 I created from a jpg.

Added the file to Android Resource subfolder

Resources\raw\videonotset.mp4

Set the source

mediaelement.Source = "ms-appx:///videonotset.mp4";

Did the same for the Image element showing the camera icon

Added the file to Android Resource subfolder

Resources\drawable\imagenotset.jpg

Set the source

imageelement.Source = "imagenotset.jpg";

Related