WPF:为什么 MediaElement 不播放?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29376929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:03:17  来源:igfitidea点击:

WPF: Why MediaElement does not play?

c#wpfmediaelement

提问by oshrat shabtai

I want to display video. I want the video will play with a click on button. The video does not play. I put the video into the project.

我想显示视频。我希望视频将通过单击按钮播放。视频不播放。我把视频放到了项目中。

I want that the video source will be YouTube.

我希望视频源是 YouTube。

My XAML code is:

我的 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# code:

代码:

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

I would like for help.

我想寻求帮助。

采纳答案by Joe B

the media source should be from your file system example

媒体源应该来自您的文件系统示例

mediaElement.Source = new Uri(@"C:\User\Admin\Images\Wildlife.wmv")

回答by A. Lartey

Yes, Joe B is right. Another approach is to use the code behind method indicated below but this demands to place the media file in a subfolder within the application.

是的,乔 B 是对的。另一种方法是使用下面指出的代码隐藏方法,但这需要将媒体文件放在应用程序内的子文件夹中。

var videoPath = Directory.GetCurrentDirectory();
mediaElement.Source = new Uri(videoPath + @"\Images\Wildlife.wmv", UriKind.Relative);
mediaElement.Play();