Combining MediaTimeline with animation in Storyboard programmatically

眉间皱痕 提交于 2019-12-11 05:13:47

问题


I am trying to use MediaTimeline and DoubleAnimation inside Storyboard to play audio with animation. Everything is implemented in C# without XAML. Nothing happens after Storyboard.Begin is called in the code below and no exception is thrown. mediaElement's HasAudio property is false and MediaOpened event of MediaElement is never called. It seems mediaElement can't load the audio file. I don't know whether MediaElement or MediaTimeline is responsible for loading the audio. DoubleAnimation works fine without the MediaTimeline. What am I missing here?

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);
}

回答1:


Your MediaElement is not part of the VisualTree. You are constructing it but as it is not assigned to the visual tree it will never get used by the Layout System.

You can either add it in code-Behind by adding it to a valid container element. e.g.

XAML:

<Grid x:Name="parentGrid">

Code-behind:

var mediaElement = new MediaElement();
parentGrid.Children.Add(mediaElement);

Or you can place the MediaElement on your page/window, remove the line where you construct the MediaElement ( .. = new MediaElement) and just access it via code-behind:

XAML:

<MediaElement x:Name="mediaElement" Width="200" Height="100"/>


来源:https://stackoverflow.com/questions/8571882/combining-mediatimeline-with-animation-in-storyboard-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!