views:

31

answers:

2

I'm a complete beginner in WPF and have an app that uses StoryBoard to play a sound.

public void PlaySound()
{
 MediaElement m = (MediaElement)audio.FindName("MySound.wma");
 m.IsMuted = false;
 FrameworkElement audioKey = (FrameworkElement)keys.FindName("MySound");
 Storyboard s = (Storyboard)audioKey.FindResource("MySound.wma");
 s.Begin(audioKey);
}

<Storyboard x:Key="MySound.wma">
 <MediaTimeline d:DesignTimeNaturalDuration="1.615" BeginTime="00:00:00" Storyboard.TargetName="MySound.wma" Source="Audio\MySound.wma"/>
</Storyboard>

I have a horrible lag and sometimes it takes good 10 seconds for the sound to be played. I suspect this has something to do with the fact that no matter how long I wait - The sound doesn't get played until after I leave the function. I don't understand it. I call Begin, and nothing happens. Is there a way to replace this method, or StoryBoard object with something that plays instantly and without a lag?

A: 

I'm not sure why you are needing to control this with a Storyboard. why not call Play directly on the MediaElement?

Abe Heidebrecht
Play doesn't do anything either :/
Rita
Okay, can you post the relevant parts of the XAML file including the MediaElement? It will help us figure out what is going on.
Abe Heidebrecht
A: 

I changed my code to -

<Button Cursor="Hand" HorizontalAlignment="Left" Margin="70,0,0,0" x:Name="MyButton"  Width="286" Content="Hi!" Focusable="False" IsTabStop="False">
        <Button.Triggers>
            <EventTrigger RoutedEvent="Button.PreviewMouseLeftButtonDown">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard SlipBehavior="Slip" BeginTime="00:00:00">
                            <MediaTimeline Storyboard.TargetName="MySound_wma" Source="MySound.wma"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
    </Button.Triggers>
</Button>

And it seems working fine now, thanks xD

Rita