Stopbyte

How to play an Online Video/Audio inside WinRT MediaElement object?

Is it possible to play an online video/audio media file inside a WinRT MediaElement in a UWP App.

i tried to play a youtube video using this XAML code below but it fails:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition Height="30" />
    </Grid.RowDefinitions>
    <MediaElement x:Name="_MediaElement" 
                    Source="https://www.youtube.com/watch?v=eegYeXSds2I" 
                    Height="500" 
                    Width="600" 
                    AutoPlay="False" />
 
    <StackPanel Grid.Row="1" Orientation="Horizontal" 
                HorizontalAlignment="Center" VerticalAlignment="Center">
        <Button Content="Play" Click="Play_Click"/>
        <Button Content="Pause" Click="Pause_Click"/>
        <Button Content="Stop" Click="Stop_Click" />
    </StackPanel>
</Grid>

The XAML above didn’t work and didn’t play the video i tried to play the video manually from the code behind using this code:

public MainWindow()
{
    InitializeComponent();
 
    Uri uri = new Uri("https://www.youtube.com/watch?v=eegYeXSds2I");
    _MediaElement.Source = uri;
    _MediaElement.Play();
}

But that didn’t work as well, please Help.

2 Likes

The MediaElement in WinRT can play online media files automatically, but the problem in your XAML and code is that you are not giving it a path/uri to a media file but instead you are giving it a path to the youtube webpage that hosts the video frame.

  • This is not the media file path:
    https://www.youtube.com/watch?v=eegYeXSds2I
    It’s only a Web page URL.

  • To fix that issue you have two options:

  1. Use a WebView and display the Web Page within it. that url above will work for a WebView Control.

  2. Or Get the real Video Url and in case of Youtube Media files i don’t think that would be an easy task.

2 Likes