Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Audio and Video playing in Windows 10 universal app?

I need to play audio and video in windows app where I Get the Url from networkcall and I need to add Url to Source. I tried in this way, But no video is played or audio is played. Someone help me in how to achieve this? Is there any other way to achieve this?

Thanks in Advance.

My xaml code:

<StackPanel HorizontalAlignment="Center" Grid.Row="3">

         <MediaElement x:Name="media" 
              Source="Videos/video1.mp4" 
              Width="400" 
              AutoPlay="False"
              AreTransportControlsEnabled="True" />

            <StackPanel Orientation="Horizontal"
            HorizontalAlignment="Center">

                <Button Content="Play" Click="Play_Click"/>
                <Button Content="Pause" Click="Pause_Click"/>
                <Button Content="Stop" Click="Stop_Click" />

            </StackPanel>
        </StackPanel>

My cs code:

  async void Play_Click(object sender, RoutedEventArgs e)
    {
        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        {
            Uri pathUri = new Uri("https://www.youtube.com/watch?v=sOEg_YZQsTI");
            media.Source = pathUri;
            Util.debugLog("PLaying ...");
            media.Play();
            media.Volume = 40;
        });
    }

    async void Pause_Click(object sender, RoutedEventArgs e)
    {
        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        {
            Util.debugLog("Paused .. ");
            media.Pause();
        });
    }

    async void Stop_Click(object sender, RoutedEventArgs e)
    {
        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        {
            Util.debugLog("Stoped ..");
            media.Stop();
        });
    }


    void Media_MediaFailed(object sender, ExceptionRoutedEventArgs e)
    {
        // Handle failed media event
    }

    void Media_MediaOpened(object sender, RoutedEventArgs e)
    {
        // Handle open media event
    }

    void Media_MediaEnded(object sender, RoutedEventArgs e)
    {
        // Handle media ended event
    }
like image 625
djkpA Avatar asked Dec 12 '25 19:12

djkpA


1 Answers

If I get it right you are currently assigning a website as the source of the MediaElement. That can't work. If you want to embed youtube content you have two possibilities:

  1. Embed the webpage in a WebView
  2. Set the url to the video itself as the Source
like image 125
Rafael Regh Avatar answered Dec 15 '25 10:12

Rafael Regh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!