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

本文关键字:10通 应用程序 Windows 视频 播放 音频 | 更新日期: 2023-09-27 18:05:55

我需要在windows应用程序中播放音频和视频,我从networkcall获得Url,我需要将Url添加到源。我试过这种方式,但是没有视频或音频播放。有人能告诉我如何做到这一点吗?还有其他方法可以做到这一点吗?

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
    }

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

如果我做对了,你目前正在分配一个网站作为MediaElement的来源。这行不通。如果你想嵌入youtube内容,你有两种可能性:

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