【发布时间】:2012-01-24 04:56:46
【问题描述】:
我正在尝试在 Storyboard 中使用 MediaTimeline 和 DoubleAnimation 来播放带有动画的音频。一切都是在没有 XAML 的 C# 中实现的。在下面的代码中调用 Storyboard.Begin 之后没有任何反应,并且没有引发异常。 mediaElement 的 HasAudio 属性为 false 并且永远不会调用 MediaElement 的 MediaOpened 事件。似乎 mediaElement 无法加载音频文件。我不知道是 MediaElement 还是 MediaTimeline 负责加载音频。没有 MediaTimeline,DoubleAnimation 也能正常工作。我在这里错过了什么?
private void SetStoryboard(double beginLeft, double endLeft, TimeSpan duration)
{
mediaElement = new MediaElement();
mediaElement.LoadedBehavior = MediaState.Manual;
mediaElement.ScrubbingEnabled = true;
mediaElement.MediaOpened += new RoutedEventHandler(MediaElement_MediaOpened);
mediaTimeline = new MediaTimeline();
mediaTimeline.Duration = new Duration(duration);
mediaTimeline.Source = new Uri(musicFilePath);
mediaTimeline.FillBehavior = FillBehavior.Stop;
Storyboard.SetTarget(mediaTimeline, mediaElement);
anim = new DoubleAnimation();
anim.From = beginLeft;
anim.To = endLeft;
anim.Duration = new Duration(duration);
anim.FillBehavior = FillBehavior.Stop;
Storyboard.SetTarget(anim, slider);
Storyboard.SetTargetProperty(anim, new PropertyPath(Canvas.LeftProperty));
storyboard = new Storyboard();
storyboard.SlipBehavior = SlipBehavior.Slip;
storyboard.Children.Add(mediaTimeline);
storyboard.Children.Add(anim);
storyboard.Begin(this, true);
}
【问题讨论】:
标签: wpf animation storyboard mediaelement