如何在UWP应用中从单独的URL播放视频和音频?

时间:2018-07-11 03:03:43

标签: c# uwp media-player uwp-xaml

我正在尝试为UWP桌面应用创建视频播放器。我无法播放来自不同URL的视频和音频。我在下面提供了代码,但没有提供音频和视频URL。我正在为我的案例使用Xampp本地服务器。请帮助我。

我的MainPage.xaml.cs

namespace my_video_player
{
    public sealed partial class MainPage : Page
    {
        MediaPlayer video_player;
        MediaSource mediaSource_video;
        MediaSource mediaSource_audio;

        public MainPage()
        {
            this.InitializeComponent();
            video_player = new MediaPlayer();

            Uri video_uri = new Uri("THE-URL-OF-THE-VIDEO");
            Uri audio_uri = new Uri("THE-URL-OF-THE-AUDIO");
            mediaSource_video = MediaSource.CreateFromUri(video_uri);
            mediaSource_audio = MediaSource.CreateFromUri(audio_uri);
            video_player.Source = mediaSource_video;
            video_player.Source = mediaSource_audio;
            video_player_screen.SetMediaPlayer(video_player);
        }
    }
}

我的MainPage.xaml

<Page
    x:Class="my_video_player.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:my_video_player"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid HorizontalAlignment="Center" VerticalAlignment="Top" Height="710" Width="1260" Margin="0,10,0,0">
        <MediaPlayerElement 
            x:Name="video_player_screen" 
            HorizontalAlignment="Left" 
            VerticalAlignment="Center"
            AreTransportControlsEnabled="True">
        </MediaPlayerElement>
    </Grid>
</Page>

1 个答案:

答案 0 :(得分:4)

  

如何合并两个来源并在媒体播放器上播放它们。

从语法角度来看,Source属性只能设置一次,最后一次有效。因此,您的媒体将仅播放mediaSource_audio中的音频。根据您的要求,您可以制作两个MediaPlayer,然后使用MediaTimelineController在多个播放器上同步内容。

mediaPlayer = new MediaPlayer();
mediaPlayer.Source = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/example_video.mkv"));
_mediaPlayerElement.SetMediaPlayer(mediaPlayer);


_mediaPlayer2 = new MediaPlayer();
_mediaPlayer2.Source = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/example_video_2.mkv"));
_mediaPlayerElement2.SetMediaPlayer(_mediaPlayer2);

_mediaTimelineController = new MediaTimelineController();

mediaPlayer.CommandManager.IsEnabled = false;
mediaPlayer.TimelineController = _mediaTimelineController;

_mediaPlayer2.CommandManager.IsEnabled = false;
_mediaPlayer2.TimelineController = _mediaTimelineController;

用法

private void PlayButton_Click(object sender, RoutedEventArgs e)
{
    _mediaTimelineController.Start();
}

有关更多信息,请参阅此document