c# - 设置 LoadedBehavior 时无法控制 MediaElement 手动
问题描述
我正在使用 VS2019。我想mediaelement
在UserControl
. 例如,当我按下底部的“播放”时,它会显示本地视频。所以问题是,当LoadedBehavior
被设置Manual
时,按下播放按钮时什么都没有。但是当我将状态更改为 时Play
,播放器会成功自动播放视频。这是我的 XMAL 代码,包括 a MediaElement
、 4Button
和 2 Slider
。
<UserControl x:Class="DictionaryPinApp.UserControlHome"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:DictionaryPinApp"
mc:Ignorable="d"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
d:DesignHeight="450" Width="auto">
<Grid>
<StackPanel>
<MediaElement x:Name="Media" Margin="100" Width="300" Height="200" VerticalAlignment="Center" LoadedBehavior="Manual" UnloadedBehavior="Stop" Stretch="Fill" MediaOpened="Element_MediaOpened" MediaEnded="Element_MediaEnded" />
<StackPanel Width="450" HorizontalAlignment="Center" Orientation="Horizontal">
<Button MouseDown="OnMouseDown_Play" Margin="140 -120 0 0" Width="30" Height="30" Style="{StaticResource MaterialDesignIconButton}" Foreground="#FF472076" ToolTip="Play">
<materialDesign:PackIcon Kind="Play" />
</Button>
<Button MouseDown="OnMouseDown_Pause" Margin="5 -120 0 0" Width="30" Height="30" Style="{StaticResource MaterialDesignIconButton}" Foreground="#FF472076" ToolTip="Pause">
<materialDesign:PackIcon Kind="Pause" />
</Button>
<Button MouseDown="OnMouseDown_Stop" Margin="5 -120 0 0" Width="30" Height="30" Style="{StaticResource MaterialDesignIconButton}" Foreground="#FF472076" ToolTip="Stop">
<materialDesign:PackIcon Kind="Stop" />
</Button>
<Button Margin="5 -120 0 0" Width="30" Height="30" Style="{StaticResource MaterialDesignIconButton}" Foreground="#FF472076" ToolTip="Volume">
<materialDesign:PackIcon Kind="VolumeMedium" />
</Button>
<Slider x:Name="VolumeSlider" ValueChanged="ChangeMediaVolume" Margin="-10 -105 0 0" Value="50" Width="70" Foreground="#FF472076" Minimum="0" Maximum="100" Style="{StaticResource MaterialDesignDiscreteSlider}" />
<Slider x:Name="TimelineSlider" ValueChanged="SeekToMediaPosition" Margin="-260 -95 0 0" Value="0" Width="300" Foreground="#FF472076"/>
</StackPanel>
</StackPanel>
<!-- </materialDesign:TransitioningContent> -->
</Grid>
</UserControl>
这是我的 C# 代码。我正在使用绝对 uri 源。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 DictionaryPinApp
{
/// <summary>
/// UserControlHome.xaml
/// </summary>
public partial class UserControlHome : UserControl
{
public UserControlHome()
{
InitializeComponent();
Media.Source = new Uri(@"f:\GraduationProject\fullver.mp4", UriKind.Absolute);
}
private void Element_MediaOpened(object sender, EventArgs e)
{
TimelineSlider.Maximum = Media.NaturalDuration.TimeSpan.TotalMilliseconds;
}
private void Element_MediaEnded(object sender, EventArgs e)
{
Media.Stop();
}
void OnMouseDown_Play(object sender, MouseButtonEventArgs args)
{
Media.Play();
InitializePropertyValues();
}
void OnMouseDown_Pause(object sender, MouseButtonEventArgs args)
{
Media.Pause();
}
void OnMouseDown_Stop(object sender, MouseButtonEventArgs args)
{
Media.Stop();
}
private void ChangeMediaVolume(object sender, RoutedPropertyChangedEventArgs<double> args)
{
Media.Volume = (double)VolumeSlider.Value;
}
private void SeekToMediaPosition(object sender, RoutedPropertyChangedEventArgs<double> args)
{
int SliderValue = (int)TimelineSlider.Value;
Media.Position = ts;
}
private void InitializePropertyValues()
{
Media.Volume = (double)VolumeSlider.Value;
}
}
}
请帮我解决这个问题。它已经让我好几天了。谢谢!
解决方案
推荐阅读
- javascript - 如果后续的承诺将根据条件返回,如何使用承诺链?
- python - 仅在没有 linter 错误的情况下配置 VS Code 以执行 Python 脚本
- javascript - 按下后退按钮时,jQuery 或 Javascript 会引发确认()
- extendscript - ExtendScript(After Effects Script)如何在当前时间将不透明度关键帧添加到任何选定的图层?
- javascript - Puppeteer 流逻辑,检查是否发生导航(vs 等待)
- node.js - nodejs应用程序崩溃后Docker容器不会重新启动?
- java - 试图阻止用户在 java 中将字符串输入我的双精度
- error-handling - 包 pdftex.def 错误:文件;我该怎么做才能让 LATEX 显示图像?
- javascript - Winston - 显示错误堆栈跟踪对象和正常格式的消息
- angular - 在 Angular 9 中,如果导航器不在线,如何仅将缓存用于 http get 请求