Search code examples
uwpwindows-media-player

Media source does not anything


I'm trying to implement a Media Player Element in an UWP app but I can't seem to get the Source working. When I visit this page, it doesn't show the media player and I can't play it.

Code Behind

public sealed partial class MoviesOverview : Page
{
        public MoviesOverview()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            vm.Movie = SerializationService.Json.Deserialize<Movie>(e.Parameter.ToString());
            SetSource();
        }

        private void SetSource()
        {
            mediaPlayer.Source = MediaSource.CreateFromUri(new Uri($"ms - appx:///Assets/Movies/{vm.Movie.Title}.mp4"));
        }
}

Using the Media Player Element

<MediaPlayerElement x:Name="mediaPlayer" Height="720" Width="1280" HorizontalAlignment="Center" VerticalAlignment="Center" />

Solution

  • Media source does not anything

    You could check this line $"ms - appx:///Assets/Movies/{vm.Movie.Title}.mp4". Please don't add blank space in ms-appx:///. Then print above uri and make sure the media file exist in Assets/Movies folder. MediaPlayerElement could not autoplay by default. please set AutoPlay property as true like the following.

    <MediaPlayerElement
        x:Name="mediaPlayer"
        Width="1280"
        Height="720"
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        AutoPlay="True"
        />
    

    Code Behind

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        SetSource();
    
    }
    private void SetSource()
    {
        mediaPlayer.Source = MediaSource.CreateFromUri(new Uri($"ms-appx:///Assets/{"test"}.mp4"));
    }