在Windows 10通用应用程序中播放音频和视频?

我需要在Windows应用程序中播放音频和视频,在该应用程序中,我可以从networkcall获取网址,并且需要将网址添加到源.
我以这种方式尝试过,但是没有播放视频或播放音频.
有人帮助我如何实现这一目标?
还有其他方法可以做到这一点吗?

提前致谢.

我的XAML程式码:

<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>

我的CS代码:

  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
    }

解决方法:

如果我做对了,那么您当前正在分配一个网站作为MediaElement的来源.那行不通.如果您要嵌入YouTube内容,则有两种可能性:

>将网页嵌入WebView
>将网址本身设置为来源

上一篇:C#-WPF的XAML:空白的工具提示文本时隐藏工具提示弹出窗口


下一篇:C#-RowHeaderTemplateSelector对象参数为null