locked
Click a button, make a sound

    Question

  • Hello.

    I want to make a program that when I click a button, a sound is made.

    I only need the code or properties, nothing else. Thanks

    Tuesday, March 25, 2014 3:59 PM

Answers

  • Hi Covey123,

    A good tutorial for you: Quickstart: video and audio and pre your requirement, I paste some code here. If you need more information, visit the link I suggested.

    In your XAML:

    <StackPanel HorizontalAlignment="Center">
        <MediaElement x:Name="media" 
                      Source="Videos/Video1.mp4" 
                      Width="300"
                      AreTransportControlsEnabled="False"
                      MediaFailed="Media_MediaFailed"
                      MediaOpened="Media_MediaOpened"
                      MediaEnded="Media_MediaEnded" />
        
        <StackPanel Orientation="Horizontal"
                    HorizontalAlignment="Center">
            
            <Button Content="Play" Click="Play_Click"/>
            <Button Content="Pause" Click="Pause_Click"/>
            <Button Content="Stop" Click="Stop_Click" />
            
        </StackPanel>
    </StackPanel>
    

    In your CS:

    void Play_Click(object sender, RoutedEventArgs e)
    {
        media.Play();
    }
    
    void Pause_Click(object sender, RoutedEventArgs e)
    {
        if (media.CanPause)
        {
            media.Pause();
        }
    }
    
    void Stop_Click(object sender, RoutedEventArgs e)
    {
        media.Stop();
    }
    
    void Media_MediaFailed(object sender, ExceptionRoutedEventArgs e)
    {
        // Handle failed media event
    }
    
    void Media_MediaOpened(object sender, RoutedEventArgs e)
    {
        // Handle open media event
    }
    
    void Media_MediaEnded(object sender, RoutedEventArgs e)
    {
        // Handle media ended event
    }

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Wednesday, March 26, 2014 1:56 AM
    Moderator