(X) Hide this
    • Login
    • Join
      • Generate New Image
        By clicking 'Register' you accept the terms of use .

Forums

Search in:

Go

Playing audio in Silverlight

  • lnikolov
    Lazar Nikolov
    202 points
    1 posts
    Jun 14, 2011, 08:28 PM
        Report Abuse

    Hi :) I have a little issue with a small project I develop. I am struggling with the audio management in Silverlight. I need to play a .wma beep file when I click a button. What cound I do?

    Reply

Answers

  • ppopadiyn
    Pencho Popadiyn
    591 points
    17 posts
    Jun 15, 2011, 04:59 PM
    Answer              Report Abuse

    Hello Lazar,

    You need to perform the following steps:

    1. Add the target .wma file in your project.
    2. Set its Build Action to Resource.
    3. 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>

       

    4. 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.
    5. Attach to the Click event of the button control that will play the audio.
    6. 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

    Reply

All Posts

  • ppopadiyn
    Pencho Popadiyn
    591 points
    17 posts
    Jun 15, 2011, 04:59 PM
    Answer              Report Abuse

    Hello Lazar,

    You need to perform the following steps:

    1. Add the target .wma file in your project.
    2. Set its Build Action to Resource.
    3. 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>

       

    4. 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.
    5. Attach to the Click event of the button control that will play the audio.
    6. 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

    Reply
  • lnikolov
    Lazar Nikolov
    202 points
    1 posts
    Jun 16, 2011, 12:19 AM
            Report Abuse

    Thanks :) This all helped me a lot :)

    Reply

  • Jack Smith
    22 points
    11 posts
    Jun 17, 2013, 12:56 PM
            Report Abuse

    Now i Not to need say any other Only say to this please follow first answer. it is absolutely right. I also give it and use it.

    ASP.Net Development Services India

    Reply
Page  

To reply to this thread you must Log In to your account


Thread


Tags: 
  • Asked
    884 days ago
  • Viewed
    1604
  • Last Activity
    150 days ago

Related Threads


Shortcuts