WPF:如何在 MediaElement 中播放来自服务器的视频(mp4 格式)?

WPF: How to play video (mp4 format) from servrer in MediaElement?

我想在 MediaElement 中直接从服务器播放视频。 (源将是服务器) 我有一个 URL 的服务器: http://videotherapy.co/dev/vt/api/dispatcher.php

和post以下json:

{"videoId":"22-1","api":"get-training-video"}

(where 22-1 is the videoId)

XAML代码:

<Window x:Class="MediaElementApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="467.91" Width="1300">
<Grid>
<MediaElement x:Name="mediaElement" HorizontalAlignment="Left" Height="418" Margin="246,10,0,0" VerticalAlignment="Top" Width="1036" LoadedBehavior="Manual" UnloadedBehavior="Stop" Source="Images\Wildlife.wmv" />
<Button x:Name="play" HorizontalAlignment="Left" Margin="538,161,0,0" VerticalAlignment="Top" Width="100" Height="84" Click="play_Click" >
    <Button.Background>
        <ImageBrush ImageSource="Images/smiley.jpg"/>
    </Button.Background>
</Button>

</Grid>

c#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MediaElementApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

        private void play_Click(object sender, RoutedEventArgs e)
        {

            mediaElement.Play();
        }
    }
}

如何做到这一点? 我想寻求帮助。

MediaElement 接受 URI 作为 Source。像这样,它可以将视频从 Web 流式传输到客户端应用程序。

如果这在您的情况下是不可能的。您必须将视频下载到 File。然后把这个文件的位置传给MediaElement

Source属性