Search code examples
c#uwpmedia-playeruwp-xaml

How to play video and audio from separate URLs in UWP app?


I'm trying to create a video player for a UWP Desktop App. I not able to play a video and audio from different URLs. I have given my code below but I haven't given audio and video URLs. I'm using a Xampp local server for my case. Please Help Me.

My MainPage.xaml.cs:

namespace my_video_player
{
    public sealed partial class MainPage : Page
    {
        MediaPlayer video_player;
        MediaSource mediaSource_video;
        MediaSource mediaSource_audio;

        public MainPage()
        {
            this.InitializeComponent();
            video_player = new MediaPlayer();

            Uri video_uri = new Uri("THE-URL-OF-THE-VIDEO");
            Uri audio_uri = new Uri("THE-URL-OF-THE-AUDIO");
            mediaSource_video = MediaSource.CreateFromUri(video_uri);
            mediaSource_audio = MediaSource.CreateFromUri(audio_uri);
            video_player.Source = mediaSource_video;
            video_player.Source = mediaSource_audio;
            video_player_screen.SetMediaPlayer(video_player);
        }
    }
}

My MainPage.xaml:

<Page
    x:Class="my_video_player.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:my_video_player"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid HorizontalAlignment="Center" VerticalAlignment="Top" Height="710" Width="1260" Margin="0,10,0,0">
        <MediaPlayerElement 
            x:Name="video_player_screen" 
            HorizontalAlignment="Left" 
            VerticalAlignment="Center"
            AreTransportControlsEnabled="True">
        </MediaPlayerElement>
    </Grid>
</Page>

Solution

  • How do I combine two sources and play them on the media player.

    From syntax point of view, the Source property can only be set once and last valid. So you media will only play audio from mediaSource_audio. For your requirement, you could make two MediaPlayer and use MediaTimelineController to synchronize content across multiple players.

    mediaPlayer = new MediaPlayer();
    mediaPlayer.Source = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/example_video.mkv"));
    _mediaPlayerElement.SetMediaPlayer(mediaPlayer);
    
    
    _mediaPlayer2 = new MediaPlayer();
    _mediaPlayer2.Source = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/example_video_2.mkv"));
    _mediaPlayerElement2.SetMediaPlayer(_mediaPlayer2);
    
    _mediaTimelineController = new MediaTimelineController();
    
    mediaPlayer.CommandManager.IsEnabled = false;
    mediaPlayer.TimelineController = _mediaTimelineController;
    
    _mediaPlayer2.CommandManager.IsEnabled = false;
    _mediaPlayer2.TimelineController = _mediaTimelineController;
    

    Usage

    private void PlayButton_Click(object sender, RoutedEventArgs e)
    {
        _mediaTimelineController.Start();
    }
    

    For more, please refer this document.