如何在 UWP 应用中从单独的 URL 播放视频和音频? [英] How to play video and audio from separate URLs in UWP app?

查看:35
本文介绍了如何在 UWP 应用中从单独的 URL 播放视频和音频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我的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>

推荐答案

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

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

从语法的角度来看,Source 属性只能设置一次并且最后一次有效.所以你的媒体只会播放来自 mediaSource_audio 的音频.根据您的要求,您可以制作两个 MediaPlayer 并使用 MediaTimelineController 以在多个播放器之间同步内容.

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;

使用

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

有关更多信息,请参阅此文档.

For more, please refer this document.

这篇关于如何在 UWP 应用中从单独的 URL 播放视频和音频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆