2

I've been teaching myself c# and trying to create a program, so far everything I've researched and found has been working except the sound. The program counts down from 60 minutes, and when the dispatchertimer reaches the 15 minute, 10 minute and 5 minutes remaining mark I want sounds to play at each of those times. When I link the sound to a button it plays without a problem, but wont play at any of those intervals. Here is the code I have so far.

namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    //timer
    private int time = 3600;
    private DispatcherTimer Timer;

    //Time intervals
    private const int MINUTE_15 = 900;
    private const int MINUTE_10 = 600;
    private const int MINUTE_5 = 300;

    //Warning intervals
    private SoundPlayer warningSound15 = new SoundPlayer(@"Resources\se15.wav");
    private SoundPlayer warningSound10 = new SoundPlayer(@"Resources\sse10.wav");
    private SoundPlayer warningSound5 = new SoundPlayer(@"Resources\se05.wav");

    public MainWindow()
    {
        InitializeComponent();

        Timer = new DispatcherTimer();
        Timer.Interval = new TimeSpan(0, 0, 0, 1);
        Timer.Tick += Timer_Tick;

    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        if (time > 0)
        {
            if (time <= 600)
            {
                if (time % 2 == 0)
                {
                    countdown.Foreground = Brushes.Red;
                }
                else
                {
                    countdown.Foreground = Brushes.DarkRed;
                }

                countdown.Text = string.Format("{0} minutes {1} seconds", time / 60, time % 60);
            }
            else
            {
                countdown.Text = string.Format("{0} minutes {1} seconds", time / 60, time % 60);
            }
        }
        else
        {
            Timer.Stop();
            hint.Clear();
            hint.Text += "BOOOOOOOOM!!!";
        }

        //Play warning sound  
        switch (time)
        {
            case MINUTE_15:
                warningSound15.Play();
                break;

            case MINUTE_10:
                warningSound10.Play();
                break;

            case MINUTE_5:
                warningSound5.Play();
                break;
        }
        time--;
    }


    //sounds

    private void hint_TextChanged(object sender, TextChangedEventArgs e)
    {
    }

    SoundPlayer hint_sound = new SoundPlayer(@"Resources\Ding.wav");

    private void button_Click(object sender, RoutedEventArgs e)
    {
        hint_sound.Play();
    }

    //hints

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        hint.Clear();
        hint.Text += "A straight flush has won this hand";
    }

    private void button2_Click(object sender, RoutedEventArgs e)
    {
        hint.Clear();
        hint.Text += "The blue trunk may require further examination";
    }

    private void button3_Click(object sender, RoutedEventArgs e)
    {
        hint.Clear();
        hint.Text += "Does the diary entry provide any insight into the portraits?";
    }

    private void button4_Click(object sender, RoutedEventArgs e)
    {
        hint.Clear();
        hint.Text += "Now that you've assembled the pieces on the wall, do the colours look like anything?";
    }

    private void button5_Click(object sender, RoutedEventArgs e)
    {
        hint.Clear();
        hint.Text += "Perhaps the blacklight could be used on a lethal piece of evidence";
    }

    private void button7_Click(object sender, RoutedEventArgs e)
    {
        warningSound15.Play();
    }

    private void button8_Click(object sender, RoutedEventArgs e)
    {
        hint.Clear();
        hint.Text += "Good job!";
    }

    private void button14_Click_1(object sender, RoutedEventArgs e)
    {
        hint.Clear();
    }

    //timer control

    private void button6_Click_1(object sender, RoutedEventArgs e)
    {
        Timer.Stop();
    }

    private void button9_Click(object sender, RoutedEventArgs e)
    {
        Timer.Start();
    }

    private void button10_Click(object sender, RoutedEventArgs e)
    {
        Timer.Stop();
        time = 3600;
        countdown.Text = string.Empty;
        countdown.Text += "60 minutes 00 seconds";
        countdown.Foreground = Brushes.Black;
    }

    //music control

    private void button13_Click(object sender, RoutedEventArgs e)
    {
        if (mediaElement.Source != null)
            mediaElement.Play();
    }

    private void button12_Click(object sender, RoutedEventArgs e)
    {
        if (mediaElement.CanPause)
            mediaElement.Pause();
    }

    private void button11_Click(object sender, RoutedEventArgs e)
    {
        mediaElement.Position = TimeSpan.FromSeconds(0);
        mediaElement.Stop();
    }


}

}

Jzilbek
  • 21
  • 4
  • 1
    That exact code works if `warningSound15.Play` is in a button `Click` event? – BradleyDotNET Oct 06 '15 at 23:49
  • @BradleyDotNET Yes, exactly, i tested it with a button with this code; `private void button7_Click(object sender, RoutedEventArgs e) { warningSound15.Play(); }` – Jzilbek Oct 06 '15 at 23:54
  • If you set a breakpoint on the call to `Play` in *this* code, do you hit it? – BradleyDotNET Oct 07 '15 at 00:00
  • @BradleyDotNET if i set a breakpoint to the Play call for the button then yes, but if i set the breakpoint to the Play call for the 15 minutes mark, nothing happens – Jzilbek Oct 07 '15 at 00:05
  • Ok, one more question. Do you have any code hat actually *starts* the timer? That is, does everything except the sounds work? Your code looks right. You may want to step through it and make sure you are getting to the `switch`, see why your data is wrong, etc. if you are running your timer. – BradleyDotNET Oct 07 '15 at 00:13
  • @BradleyDotNET Yes, I have three buttons, one to start, one to stop and one to reset the timer. I apologize for not including those pieces of code. Still quite new to all this but happy and eager to learn. Everything does work, except the sound at those intervals. – Jzilbek Oct 07 '15 at 00:15
  • Ok, I wrote equivalent code and it works just fine: http://ideone.com/UESUyR is *anything* else messing with the `time` variable? Also, does setting a breakpoint on the `switch` get hit? – BradleyDotNET Oct 07 '15 at 00:28
  • @BradleyDotNET with a breakpoint on `switch`, it gets hit once i start the timer. I've edited the code I submitted to include everything which i should've done in the first place, Thank you for taking the time to look at this. – Jzilbek Oct 07 '15 at 00:35
  • I'm really sorry, but your code looks perfect, and very similar code executes correctly. I would try it with much smaller numbers and single step through to see what happens. – BradleyDotNET Oct 07 '15 at 00:39
  • @BradleyDotNET The fact that i'm such a beginner and you saying my code looks right made my day. No apologies needed. Thank you for taking time out of your day to look at this, it's very much appreciated. – Jzilbek Oct 07 '15 at 00:43
  • Well, the code could use some design help (read up on MVVM) :) That said it really does look functionally correct. – BradleyDotNET Oct 07 '15 at 00:47
  • @BradleyDotNET Thanks for the suggestion, will definitely read up on MVVM, the more learning the better. – Jzilbek Oct 07 '15 at 00:53
  • When you get a bit more rep, feel free to join us in the WPF chat room if you have questions when learning MVVM. We are happy to help! http://chat.stackoverflow.com/rooms/18165/wpf – BradleyDotNET Oct 07 '15 at 02:09

0 Answers0