playing .wav file with C# - c#

I'm trying to play a .Wav file thats located in a folder inside my project.
The sound file is located on "Resource/Sounds/slot_roll_on.Wav"
The resource folder is a folder I created myself, in the root of the project.
This is the code I'm using to run the .wav file
Assembly a = Assembly.GetExecutingAssembly();
Stream s = a.GetManifestResourceStream("kisscam.g.resources.Resources.Sounds.slot_roll_on.wav");
SoundPlayer snd = new SoundPlayer(s);
snd.Play();
I couldn't get the sound to play, I keep getting the windows sound for sound not found.
Somewhere on stack overflow I found this code to find what the right assembly path should be.
Assembly a = Assembly.GetExecutingAssembly();
string[] resourceNames = Assembly.GetExecutingAssembly().GetManifestResourceNames();
int i;
for (i = 0; i < resourceNames.Length; i++)
{
MessageBox.Show(resourceNames[i]);
}
It output these 2 paths
kisscam.Properties.Resources.resources
and
kissscam.g.resources
I tried using them both, but none of them works.
Anyone know what I'm doing wrong ?

Add your Wav file to resources by going to your Project Properties --> Resources Select Audio and Browse to the file. You will then be able to see it as part pf Propeties.Resources. It will add it to a Resources Folder where you can set it to embedded or leave it as is, which is set as content
Accessed like this
private void button1_Click(object sender, EventArgs e)
{
SoundPlayer snd = new SoundPlayer( Properties.Resources.tada);
snd.Play();
}

If you want to add music in your program by playing your .wav file in projects. Then you have to add the .wav file like this.
using System.Media; // write down it at the top of the FORM
SoundPlayer my_wave_file = new SoundPlayer("F:/SOund wave file/airplanefly.wav");
my_wave_file.PlaySync(); // PlaySync means that once sound start then no other activity if form will occur untill sound goes to finish
Remember that you have to write the path of the file with forward slashes (/) format, don't use back slashes () during giving a path to the file, otherwise you will get an error

Currently I know two ways to do so, see below:
Use file path
First put the file in the root folder of the project, then no matter you run the program under Debug or Release mode, the file can both be accessed for sure. Next use the class SoundPlayer to paly it.
var basePath = System.AppDomain.CurrentDomain.BaseDirectory;
SoundPlayer player = new SoundPlayer();
player.SoundLocation = Path.Combine(basePath, #"./../../Reminder.wav");
player.Load();
player.Play();
Use resource
Follow below animate, add "Exsiting file" to the project.
SoundPlayer player = new SoundPlayer(Properties.Resources.Reminder);
player.Play();
The strength of this way compared to the other is:
Only the folder "Release" under the "bin" directory need to be copy when run the program.

Related

Please be sure a sound file exists at the specified location

i developed a simple app to play music.
When i run it on debug it works just fine. But after i published it as an application and install it on another machine, it won't run because the file paths is not recognized.
Here is my code:
SoundPlayer player = new SoundPlayer();
player.SoundLocation = #"C:\Users\chris\source\repos\Aplikasi Reminder\Aplikasi Reminder\Resources\mixkit-correct-answer-tone-2870.wav";
player.Play();
I tried to change the paths into:
player.SoundLocation = #"Aplikasi_reminder.Properties.Resources.mixkit-correct-answer-tone-2870.wav";
and it won't too..
Please can someone tell me how to fix that, thank you..
Include your files in the project`s resources folder by right-clicking on your project name>>properties>>Recources>>select audio>>drag your .wav file.
Then you can play the file from Memory Stream:
public void Play()
{
SoundPlayer player = new SoundPlayer();
player.Stream = YOUR_PROJECT_NAME.Properties.Resources.YOUR_FILE_NAME;
player.Play();
}

playing of .wav file

I am making a piano app, where each key plays a different .wav file. For instance, if the C note is pressed then C.wav is played.
So far I have only managed to play a wav file using the code in the screenshot, otherwise the file is not found. However this method is not suitable for what I am trying to do. The wav file i am trying to play is called hello.wav and I have provided the location. Any suggestions?
This is the current code:
SoundPlayer sp = new SoundPlayer(PianoApp.Properties.Resources.Hello);
sp.Play();
or as it appears in my IDE:
This is the file location:
I,
Add you your file .wav in ressource of your project with mySoundFile as name, and code like this
Stream str = Properties.Resources.mySoundFile;
SoundPlayer snd = new SoundPlayer(str);
snd.Play();
with multiple .wav ressources
Stream str1 = Properties.Resources.mySoundFile1;
Stream str2 = Properties.Resources.mySoundFile2;
RecordPlayer rp = new RecordPlayer();
rp.Open(new WaveReader(str1));
rp.Play();
rp.Open(new WaveReader(str2));
rp.Play();
If your problem is to find your files in the output folder of your application, then just set the "Copy to Output Directory" option to "Copy always" in the Properties window.
But if you want to keep them in resources, then you can also use Properties.Resources.ResourceManager.GetStream("audioResourceName") to get your audio files according to a particular key name that was pressed. Thus, you should name all your resources accordingly.

c# Add Soundfile to Setup and direct to file path

I want to create a SetupFile for my project. I have two sounds that I use... that's my current code (which works fine when I debug):
SoundPlayer clckSound = new SoundPlayer(#"C:\Users\My Name\Downloads\Button Push.wav");
SoundPlayer wonSound = new SoundPlayer(#"C:\Users\My Name\Downloads\Applause.wav");
Inside my Setup Project in the "Application Folder" I created another folder named "Assets" where I put the two sound files.
I tried it with the following code which didnt work.
SoundPlayer clckSound = new SoundPlayer(Application.StartupPath + "\\Assets\\Button Push.wav");
SoundPlayer wonSound = new SoundPlayer(Application.StartupPath + "\\Assets\\Applause.wav");
How do I specify the file path in my code so that the sounds get played properly after installation of the Setup file?
Thanks for your help!

Embed File in a Executable

I'm trying to build a simple program and I want to find a way to embed a file (or multiple files) in the executable.
The program is very simple. I will be building a form using C# in visual studio. On the form, there will be couple questions and a submit button.
Once the user has answer all the questions and hit the submit button, if all answers are correct, I want to give the user the file as a prize. (The file can be image, video, or a zip file that contains multiple other files)
The way I want to give the user the file is very flexible. It can just be creating this file in the same directory as the executable, or given the download option for the user to save it somewhere else.
Below is the pseudo code
private void submit_Click(object sender, EventArgs e)
{
//functions to check all answers
if(all answers are correct)
{
label.Text = "Congrats! You answered all questions correctly";
//create the file that was embeded into the same directory as the executable
//let's call the file 'prize.img'
Process.Start("prize.img");
}
else
label.Text = "Some answers were not correct";
}
The logic is pretty simple and straight forward. The problem is, how can I embed "prize.img" into the executable? I will be giving this program (.exe) to a friend so he will not have any source and I can't guarantee the path.
You want to do embed the file as a resource.
Right-click the project file, select Properties.
In the window that opens, go to the Resources tab, and if it has just a blue link in the middle of the tab-page, click it, to create a new resource.
In your code you can type in Resources.TheNameYouGaveTheFileHere and you can access its contents. Note that the first time you use the Resources class in a class, you need to add a using directive (hit Ctrl+. after typing Resources to get the menu to get VS to do it for you).
Do you need help with the saving of the file also?
Edit:
You could do something like this:
var resource = Properties.Resources.yourResource;
FileStream fileStream = new FileStream("filename.exe", FileMode.CreateNew);
for (int i = 0; i < resource.Length; i++)
fileStream.WriteByte((byte)resource[i]);
fileStream.Close();
Does it help you?
EDIT:
As I can see you are getting a stream, here is an update to make it work:
var resource = Properties.Resources.yourResource;
FileStream fileStream = new FileStream("filename.exe", FileMode.CreateNew);
resource.CopyTo(fileStream);
fileStream.Close();
Does it work now?
Can't you add the image file as a resource in your solution? And then you can reference that resource in your code?
See if this may be what you're looking for:
Embedding Image Resource in Project
The link provided offers step-by-step how to accomplish this:
After adding the image as a resource, the instructions show how to access your image resource and use it in a program. Just click the link above and follow the instructions.
I followed the instructions above and wrote my program like this:
using System.Drawing;
using System.Windows.Forms;
using System.Reflection;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var myAssembly = Assembly.GetExecutingAssembly();
var myStream = myAssembly.GetManifestResourceStream("WindowsFormsApplication6.Desert.jpg");
var image = new Bitmap(myStream);
pictureBox1.Image = image;
}
}
}
My output looks like this:
The image on the form came from the image that I embedded as a resource. Very easy to do.
Add the file into project then set its Build Action as Embeded resource.
From your code you can do something like this:
var _assembly = Assembly.GetExecutingAssembly();
var _stream = _assembly.GetManifestResourceStream("namespace.fileneme");

Cannot locate the wav file in the root folder of the project

I have code below. It works fine. But I would like to locate the "sound.wav" file the folder of the project. I mean ı don't want to put it in "D:\audio\background\sound.wav". I put the audio file the folder of the project but I couldn't do that . What changes should I do in System.Uri(#"D:\audio\background\sound.wav"))"
Thanks.
my simple program is this. I just want to play sound.wav file from home folder.
private void button1_Click(object sender, EventArgs e)
{
var background = new System.Windows.Media.MediaPlayer();
background.Open(new System.Uri(#"D:\sound.wav"));
background.Play();
}
You can use Environment.CurrentDirectory to get current working directory of your application. Then use Path.Combine to create path to audio folder inside working directory:
var path = Path.Combine(Environment.CurrentDirectory, "audio", "sound.wav");
If you want add your audio files in the resources properties->resources->addresource,after you done that just do this:
SoundPlayer sndplayr = new SoundPlayer(YourNameSpace.Properties.Resources.TDB_Groove_04_140_BPM__RC_);
sndplayr.Play();
//TDB_Groove_04_140_BPM__RC_ was a file i have and added to my project just as example
If you prefer to place files in startup folder then:
var background = new System.Windows.Media.MediaPlayer();
background.Open(new Uri(Application.StartupPath + #"\YourWavFile.wav"));
background.Play();
Assuming you have a directory called audio in your project you could access the file with a string like this:
"..\audio\sound.wav"
Or if your application is being run in a directory further down in your project you can use "~" to access the home directory

Categories