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

Audio and Video playing in Windows 10 universal app?

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

提前致谢。

我的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 内容,您有两种选择:

  1. 将网页嵌入 WebView
  2. 将 url 设置为视频本身作为 Source