如何从UWP应用不同的网址,播放视频和音频?(How to play video and audio

2019-09-28 13:00发布

我试图创建一个UWP桌面应用程序的视频播放器。 我不能够从不同的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>

Answer 1:

如何合并两个来源,并发挥他们的媒体播放器。

但从语法点,该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();
}

有关详细信息,请参阅本文档 。



文章来源: How to play video and audio from separate URLs in UWP app?