前端代码.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;
}
}));
}