我尝试在WPF中实现视频。视频在Visual Studio中可见(所以我认为路径是正确的)但在我运行程序时不可见(我也听不到任何声音)。我有1个MediaElement来实现视频和下面的3个按钮(播放,暂停,静音)。视频的Build-Property是“Resource”。
XAML的代码:
<StackPanel HorizontalAlignment="Center" Width="340" Height="300" Margin="281,63,0,0" VerticalAlignment="Center">
<MediaElement Name="myMedia" Source="testvideo.wmv" LoadedBehavior="Manual" Width="320" Height="240" />
<StackPanel Orientation="Horizontal" Margin="0, 10, 0, 0">
<Button Content="Play" Margin="0, 0, 10, 0" Padding="5" Click="mediaPlay"/>
<Button Content="Pause" Margin="0, 0, 10, 0" Padding="5" Click="mediaPause"/>
<Button x:Name="muteButt" Content="Mute" Padding="5" Click="mediaMute"/>
</StackPanel>
</StackPanel>
代码背后:
public MainWindow()
{
InitializeComponent();
myMedia.Volume = 100;
myMedia.Play();
myMedia.Position = new TimeSpan(0, 0, 5); // first frame 5 seconds
}
void mediaPlay(Object sender, EventArgs e)
{
myMedia.Play();
}
void mediaPause(Object sender, EventArgs e)
{
myMedia.Pause();
}
void mediaMute(Object sender, EventArgs e)
{
if (myMedia.Volume == 100)
{
myMedia.Volume = 0;
muteButt.Content = "Listen";
}
else
{
myMedia.Volume = 100;
muteButt.Content = "Mute";
}
}
答案 0 :(得分:0)
我发现了我的问题: 我不得不再次在代码中设置MediaElement的属性“Source”。我不知道为什么,但现在它有效!