How to play video and audio from separate URLs in UWP app?

坚强是说给别人听的谎言 提交于 2019-12-11 08:53:30

问题


I'm trying to create a video player for a UWP Desktop App. I not able to play a video and audio from different URLs. I have given my code below but I haven't given audio and video URLs. I'm using a Xampp local server for my case. Please Help Me.

My 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);
        }
    }
}

My 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:


How do I combine two sources and play them on the media player.

From syntax point of view, the Source property can only be set once and last valid. So you media will only play audio from mediaSource_audio. For your requirement, you could make two MediaPlayer and use MediaTimelineController to synchronize content across multiple players.

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;

Usage

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

For more, please refer this document.



来源:https://stackoverflow.com/questions/51276649/how-to-play-video-and-audio-from-separate-urls-in-uwp-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!