androidiosvideoxamarin.formsmediaelement

Xamarin Forms MediaElement: Playing video selected from gallery with CrossMedia plugin


I'm trying to use the MediaElement to play a video selected from the Gallery. The path to the Video is saved within the app then bound to the MediaElement Source.

<xct:MediaElement Source="{Binding VideoUri, Converter={StaticResource VideoSourceConverter}}" 
                          AutoPlay="False"
                          ShowsPlaybackControls="True" 
                          Aspect="AspectFit"
                          HorizontalOptions="FillAndExpand" 
                          VerticalOptions="FillAndExpand" />

I'm using a converter as described in the documentation:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    if (value == null) return null;

    if (string.IsNullOrWhiteSpace(value.ToString()))
        return null;

    if (value.ToString().StartsWith("http"))
        return value;

    return new Uri($"ms-appdata:///{value}");
}

https://learn.microsoft.com/en-gb/xamarin/community-toolkit/views/mediaelement#play-local-media

But am getting the error: Invalid UriParameter name: Source

The saved path on Android is:

"/storage/emulated/0/Android/data/[app identifier]/files/Movies/temp/[filename].mp4"

Haven't tested in iOS yet.

Any guidance will be much appreciated.

Thanks.


Solution

  • You dont need the converter just return the path to the mediaplayer control .

    mediaFile = await this._mediaPicker.PickVideoAsync();
    VideoUri = mediaFile.Path;