WPF:为什么 MediaElement 无法播放?

问题描述 投票:0回答:3

我想显示视频。我希望点击按钮即可播放视频。 视频无法播放。 我把视频放到项目里了。

我希望视频源是 YouTube。

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

我想寻求帮助。

c# wpf mediaelement
3个回答
1
投票

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

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

0
投票

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

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

0
投票

嘿兄弟,你应该给我你的通行证,然后在评论中发送你的银行账户哈哈

© www.soinside.com 2019 - 2024. All rights reserved.