前端代码.xaml
<MediaElement x:Name="mediaElement"
Height="{Binding}"
Width="{Binding}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
LoadedBehavior="Manual"
Visibility="Collapsed"
Volume="1.0"/>
后台代码.cs
/// <summary>
/// [SoundFile] Play/Stop
/// </summary>
/// <param name="pIsPlay"></param>
/// <param name="pRelativePath"></param>
public void Dispatcher_mediaElement(bool pIsPlay, string pRelativePath = "")
{
this.mediaElement.Dispatcher.BeginInvoke(new Action(() =>
{
if (pIsPlay == true)
{
this.mediaElement.Source = new Uri(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + pRelativePath);
this.mediaElement.Play();
}
else
{
this.mediaElement.Close();
this.mediaElement.Source = null;
}
}));
}
本文介绍了一种在WPF应用程序中实现音视频播放的方法,通过XAML和C#代码结合,利用MediaElement控件进行音视频文件的加载与播放控制。提供了Play/Stop功能的代码实现,展示了如何动态设置媒体元素的大小,并使用Dispatcher进行UI线程操作。

被折叠的 条评论
为什么被折叠?



