We help IT Professionals succeed at work.
Get Started

How to play small sound file continuously in Silverlight?

858 Views
Last Modified: 2013-11-12
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?


 Silverlight Application with clickable button, but I want to use clickable image so that an event can be fired
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>
..............................................................................
Code Behind File
...................................................................
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();
			
		}
	}
}
..................

Open in new window

Comment
Watch Question
Software Developer
Top Expert 2006
Commented:
This problem has been solved!
Unlock 1 Answer and 9 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE