2

I have the following problem: I just want to play a short sound but I hear nothing. The soundfile's property "Copy to output directory" says "Copy always". Strangely when I copy a existing and working example nothing happens but the original works. I can't find my problem.

Additionally systemsound don't work either.

Any ideas?

Thank you!!

using System.Media;
using System.Windows.Input;
using Microsoft.Windows.Controls.Ribbon;

namespace WpfRibbonApplication14
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : RibbonWindow
    {
        SoundPlayer player = new SoundPlayer("sound.wav");

        public MainWindow()
        {
            InitializeComponent();
            player.LoadAsync();

            // Insert code required on object creation below this point.
        }

        private void Button1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            player.Play();
        }
    }
}
user774326
  • 117
  • 2
  • 11
  • Try turning up the volume. Without an exception there isn't anything to diagnose. You critically depend on Environment.CurrentDirectory to be set correctly, that isn't healthy. – Hans Passant Aug 22 '11 at 21:04
  • That's the first thing that I did but it didn't work. Thanks for the hint concering the directory, but this is just to see if it works. – user774326 Aug 22 '11 at 21:10
  • I don't know if that's of importance but setting a breakpoint on the player.play() line doesn't cause a break nothing happens. I'm at a loss – user774326 Aug 22 '11 at 21:15
  • Focus on learning how to set a button's Click event handler. Any WPF tutorial covers that. – Hans Passant Aug 22 '11 at 21:19
  • Hi Hans! I actually did it like this because the example that I downloaded did it like this also. Although I'm a beginner I know how to write a Click event. Listening to you now it works. hmmm ... I'm just stupid or need a break of whatever .... Thank you! – user774326 Aug 22 '11 at 21:27
  • I think what Hans was trying to tell you is to check if your Button1_MouseLeftButtonDown was actually hooked up in the MainWindow.xaml to Button1's click event such as: – ShelbyZ Aug 29 '11 at 22:14
  • A few suggestions: 1) Check the .wav file is uncompressed - SoundPlayer is extremely limited in terms of what it can play - which is uncompressed .wav files only! 2) Try listening to the soundplayer LoadCompleted event. Stick a breakpoint in the handler - is it being hit? If so, it may be the case the button click event is being raised before the sound file is loaded. – Sam Hogarth Jan 23 '12 at 10:24
  • @Hans Passant, it would be helpful to the community if you could copy your comment into an answer and if user774326 could mark this question as answered. Thanks. – Sheridan Feb 11 '12 at 00:41

1 Answers1

1

Check that your Button1_MouseLeftButtonDown event handler was actually hooked up in your MainWindow.xaml to Button1's click event, e.g.: <Button Name="Button1" Click="Button1_MouseLeftButtonDown" />

PS: I came here expecting this question to be unanswered, but it had actually been answered in the comments, so I'm reposting the answer here. I am not trying to steal Hans Passant's rep, honest!

Community
  • 1
  • 1
dodgy_coder
  • 12,407
  • 10
  • 54
  • 67