Hello Lazar,
You need to perform the following steps:
- Add the target .wma file in your project.
- Set its Build Action to Resource.
- Next, declare a new MediaElement in your XAML in the following manner (note, that you need to set the AutoPlay property to False):
<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid x:Name="LayoutRoot"
Background="White">
<MediaElement x:Name="mediaElement"
Source="/SilverlightApplication1;component/Sound/Ringtone.wma"
AutoPlay="False"
Volume="1" />
<Button x:Name="PlaySoundButton"
Content="PlaySound"
Click="PlaySoundButtonClick"/>
</Grid>
</UserControl>
- In the previous code-snippet "SilvelightApplication1" is the name of the project that contains the sound. And "/Sound/Ringtone.wma" is the actual path to the sound.
- Attach to the Click event of the button control that will play the audio.
- From the code-behind, just invoke the MedialElement.Play() method.
using System.Windows;
namespace SilverlightApplication1
{
public partial class MainPage
{
public MainPage()
{
InitializeComponent();
}
private void PlaySoundButtonClick( object sender, RoutedEventArgs e )
{
this.mediaElement.Play();
}
}
}
Hope, this will work for you
Regards
Pencho