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

时间:2015-08-01 10:55:12

标签: c# windows xaml windows-10 uwp

我需要在Windows应用中播放音频和视频,我从网络电话获取网址,我需要将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
    }

1 个答案:

答案 0 :(得分:3)

如果我做对了,你当前正在指定一个网站作为MediaElement的来源。那不行。如果您想嵌入YouTube内容,您有两种可能:

  1. 将网页嵌入WebView
  2. 将网址设置为Source