How to play small sound file continuously in Silverlight?

Posted by ash on Stack Overflow See other posts from Stack Overflow or by ash
Published on 2010-06-12T22:02:53Z Indexed on 2010/06/12 22:13 UTC
Read the original article Hit count: 303

Filed under:

Hello,

I have two questions regarding Silverlight's SoundPlay action and properties. My scenario is like:

I have two story board: The first story board has an image and a sound file; when the silverlight application gets loaded, the sound starts to play automatically, but if someone clicks the image, the sound file will stop and the second storyboard will start with a new sound file.

1) My first question is how to stop the first sound file of first story board when the second story board starts with the second sound file.

2) My second question is how to play a sound file continuously; for example, in Silverlight we can play a story board continuously with RepeatBehavior="Forever"; but I cannot find a way to play my 10 second sound file forever or continuously.

Note: I have attached a small XAML file to show what I am talking about; I am also stating that if instead of an image file, if there were a button, then I can stop the first music file after I click the button and start my second story board with a new sound file, but I would like to use image file instead of a button. Is it possible? If it is, how to do it?

Therefore, please answer my following two questions or give big hint or website tutorial links on

1) How to stop the first sound file of first story board when the second story board starts with the second sound file ( When the clickable element is an image instead of a button) 2) How to play a 10 second sound file continuously?

............Code Snippet......................

XAML ............

<Grid x:Name="LayoutRoot" Background="Red">
    <Button HorizontalAlignment="Left" Margin="212,0,0,111" VerticalAlignment="Bottom" Width="75" Content="Button" Click="onClick"/>
    <MediaElement x:Name="sound2_mp3" Height="0" HorizontalAlignment="Left" Margin="105,230,0,0" VerticalAlignment="Top" Width="0" Source="/sound2.mp3" Stretch="Fill"/>
    <MediaElement x:Name="sound1_mp1" Height="0" HorizontalAlignment="Left" Margin="190,164,0,0" VerticalAlignment="Top" Width="0" Source="/sound1.mp3" Stretch="Fill" AutoPlay="False"/>
</Grid>

..................................................................................................................................................................................................................... using System; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes;

namespace testPrj { public partial class MainPage : UserControl { public MainPage() { // Required to initialize variables InitializeComponent(); }

    private void onClick(object sender, System.Windows.RoutedEventArgs e)
    {
        Storyboard1.Stop();
        sound2_mp3.Stop();
        sound1_mp1.Play();

    }
}

} ...................................................................................................

© Stack Overflow or respective owner

Related posts about Silverlight