I am trying to play a wav file form a projects resources.
I found this example: Click Here
I imported two wav files per instructions named "Button.wav" and "Stop.wav".
I commented out this line Stream soundStream; because it did not seem to belong.
When I run the code the sound files do not play, but I do get the "Windows Ding" sound.
This is issue is not critical, just a proff of principle for me at this point.

fyi... I tried it with and without Stream soundStream; before posting.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Reflection;
using System.IO;
using System.Resources;
using System.Media;
using System.Diagnostics;

namespace GC_RunTimeCalculater
{
    public partial class Form1 : Form
    {
        decimal totalRunTime = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {     
            if (checkBox1.Checked)
            {
                Assembly assembly;
                // Stream soundStream;
                SoundPlayer sp;
                assembly = Assembly.GetExecutingAssembly();
                sp = new SoundPlayer(assembly.GetManifestResourceStream("Button.wav"));
                sp.Play();
            }

            if (secondTemp.Value < startTemp.Value)
            {
                Assembly assembly;
                //Stream soundStream;
                SoundPlayer sp;
                assembly = Assembly.GetExecutingAssembly();
                sp = new SoundPlayer(assembly.GetManifestResourceStream("Stop.wav"));
                sp.Play();

                MessageBox.Show("2nd Temp is less than Start Temp.", "Wait!!");
            }
            if (secondTemp.Value > finalTemp.Value)
            {
                Assembly assembly;
                //Stream soundStream;
                SoundPlayer sp;
                assembly = Assembly.GetExecutingAssembly();
                sp = new SoundPlayer(assembly.GetManifestResourceStream("Stop.wav"));
                sp.Play();

                MessageBox.Show("Final Temp is less than 2n Temp.", "Wait!!");
            }

            #region old div by zero
            //code not need, set the min ramp value to 1, dah!!
            //if ((ramp1.Value == 0) || (ramp2.Value == 0))
            //{
            //    MessageBox.Show("Ramp cannot be 0!!");
            //    labelTotalRunTime.Text = "";
            //    return;
            //} 
            #endregion

           totalRunTime = (secondTemp.Value - startTemp.Value) / ramp1.Value + initialHold.Value +
                (finalTemp.Value - secondTemp.Value) / ramp2.Value + finalHold.Value;

            //totalRunTime = secondTemp.Value - startTemp.Value;
            //totalRunTime = totalRunTime / ramp1.Value + initialHold.Value;
            //totalRunTime = totalRunTime + finalTemp.Value - secondTemp.Value / ramp2.Value + finalHold.Value;

            labelTotalRunTime.Text = totalRunTime.ToString();    
        }

        private void finalTemp_ValueChanged(object sender, EventArgs e)
        {

        }

        private void secondTemp_ValueChanged(object sender, EventArgs e)
        {
            finalTemp.Value = secondTemp.Value;
        }
    }
}

Edited 3 Years Ago by Triryche

If you load the file from the Resources class it will load as a stream. Simply pass it as the argument to the soundplayer:

        SoundPlayer sp = new SoundPlayer(GC_RunTimeCalculater.Properties.Resources.Button);
        sp.Play();

On a side note the MessageBox will play a sound aqccording to the icon you use:

MessageBox.Show("2nd Temp is less than Start Temp.","Wait!!", _
                    MessageBoxButtons.OK, _
                    MessageBoxIcon.Error);

Edited 3 Years Ago by tinstaafl

Thanks for the reply.
I get the following error:

`Error  1   'GC_RunTimeCalculater.Properties.Resources' does not contain a definition for 'Button'  F:\CSharp\GC_RunTimeCalculator\WindowsFormsApplication4\Form1.cs    36  80  GC_RunTimeCalculater`

If the file is named Button.wav in the Resources tab, rename it to Button, or change your statement to use Button.wav. Either way Intellisense will tell you what files are available. When you type this much: GC_RunTimeCalculater.Properties.Resources then type '.' Intellisense will show a list of options. In that list will be the files you added to your Resources.

Edited 3 Years Ago by tinstaafl

Intellisense shows:
.Culture
.Equals
.ReferenceEquals
.ResourceManager

Yeah.
I'm still only getting
.Culture
.Equals
.ReferenceEquals
.ResourceManager

Perhaps I did not add the wav files properly.

I think you're confusing methods. The way you load the resource will determine the method you use to play the stream.

The easiest way to load a resource is through the designer. Here's the MSDN link for adding resources.

Once you've done that do a click-pause-click on the resource name and edit the name to match your code. The name of the file doesn't have to have any bearing on the name of the resource. Now Intellisense can find the resource, while you're programming, and if you name the resource 'Button', my original code will work:

 SoundPlayer sp = new SoundPlayer(GC_RunTimeCalculater.Properties.Resources.Button);
sp.Play();

The problem, with setting the resource to be embedded at compile time, is, you can't find any problems until you run your code.

Edited 3 Years Ago by tinstaafl

tinstaafl and ddanbe, thanks for all your help!!

For proper programming etiquiite, in general, should I be creating soundPlayer objects outside of the methods?

Also, when it was orginally not working but I had no compile errors, I take it I was getting the Windows "Ding" because it could not execute that portion of the code, even though it compiled with now errors?

you can declare the soundplayer once at the class level then just set the Stream property to the Resource before you play the sound

sp.Stream = GC_RunTimeCalculater.Properties.Resources.Button;
This question has already been answered. Start a new discussion instead.