WPF解决 MediaElement 只能播放一次的问题
1.设置LoadedBehavior =“Manual”
2.在MediaEnded事件中停止播放,并重设时间轴
//生成音频播放器
MediaElement mediaElement = new MediaElement();
mediaElement.LoadedBehavior = MediaState.Manual;
mediaElement.MediaEnded += UKPronMe_MediaEnded;
private void UKPronMe_MediaEnded(object sender, RoutedEventArgs e)
{
MediaElement mediaElement= sender as MediaElement;
mediaElement.Stop();
mediaElement.Position = TimeSpan.Zero;
}
如果不是按钮点击进行播放,而是一进页面就播放则要加MediaOpened=“MediaElement1_MediaOpened”
private void MediaElement1_MediaOpened(object sender, RoutedEventArgs e)
{
MediaElement mediaElement = sender as MediaElement;
mediaElement.Play();
}