views:

19

answers:

1

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 ............

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="testPrj.MainPage"
    Width="640" Height="480">
    <UserControl.Resources>
        <Storyboard x:Name="Storyboard1" RepeatBehavior="Forever"/>
        <Storyboard x:Name="Storyboard2"/>
    </UserControl.Resources>

    <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>
</UserControl>

...............

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

        }
    }
}
A: 

Both of the problems got resolved:

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.

The problem got solved after I have added MediaEnded event handler;

Now my changed XAML code is: .. .. ...

<MediaElement x:Name="sound2_mp3" Height="0" HorizontalAlignment="Left" Margin="105,230,0,0" VerticalAlignment="Top" Width="0" Source="/sound2.mp3" Stretch="Fill" DataContext="{Binding}" MediaEnded="start" AutoPlay="True" />
        <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" MediaEnded="start1"/>

... ... ...

And my Code behind file has:

    private void start(object sender, RoutedEventArgs e)
    {
        sound2_mp3.Position = TimeSpan.FromSeconds(0);
        sound2_mp3.Play();
    }
    private void start1(object sender, RoutedEventArgs e)
    {
        sound1_mp1.Position = TimeSpan.FromSeconds(0);
        sound1_mp1.Play();
    }

And my first question was:

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)

This is doable and got solved. I do not have any idea why I have not investigated and experimented with the event types of image element. So, the conclusion is: If the click-able element were an image, an event can be fired; for example in the following code segment I am using MouseLeftButtonUp event.

XAML

    <Image HorizontalAlignment="Left" Margin="88,239,0,118" Width="95" Source="me1.png" Stretch="Fill" MouseLeftButtonUp="imgGotClicked"/>    

Code Behind File

    private void imgGotClicked(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
          MessageBox.Show("Hello World!");
          Storyboard1.Stop();
          Storyboard2.Begin();
    }

Therefore, problem # 1 and problem # 2 both got solved. This thread's problem got solved. Therefore, it should be closed.

ash