sound play for any computers - c#

I use these following codes to play music while clicking the button.
private void button1_Click(object sender, EventArgs e)
{
SoundPlayer s = new SoundPlayer();
s.SoundLocation = #"f:\1.wav";
s.Play();
}
But these codes are only for my computer what should i do so that i can play this sound on the other computers like database. what should i do so that i can play this song in other computers?
Thanks in advance

You can import the file in the project's resources and load it from there using
Properties.Resources.<name_of_resource>
In order to import something to the resources do the following (assuming you use Visual Studio 2010 - it is similar to other versions I think):
In Visual Studio 2010 solution explorer, right click Properties -> Open -> Resources -> Add Resource -> Add Existing File
Note that when you will install your application later on other computers the resources will be installed as well.
In order to play the sound you need to do the following then:
SoundPlayer myPlayer = new SoundPlayer(yourNamespace.Properties.Resources.mySound);
myPlayer.Play();

You can do something like, get the file content as Stream from the database

You can do something like, get the file content as Stream from the database or remote system and pass the stream to the SoundPlayer instance.

You can store the sounds in a subfolder of the applications and access them via file's relative path (I believe, but I'm going to look it up precisely, it's Application.StartupPath in a winforms environment).
Or you could make them resources (see gkaran89's answer).

Related

C#: Open a Fullscreen process

I'm creating a program that opens to applications (.exe). I can open one of them, that's a windowed application (samp.exe), but I can't open the other one (gta_sa.exe), that's a fullscreen application.
I don't know if using Process.Start() doesn't allow me to open it, but I guess it's not because of it.
Here's my code:
private void btnSA_Click(object sender, EventArgs e)
{
Process.Start(Properties.Settings.Default.SAPath);
}
The Properties.Settings.Default.SAPath is the file path (C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto San Andreas\gta_sa.exe)
I found why it wasn't working.
For some reason my computer didn't show up the game because of some configs. After reversing them I managed to open the game normally.
Thank you all for your answers!

Issue with SoundPlayer

Situation
this is probably a really simple question with a really simple answer, but I can't find the answer anywhere, so I'm going to ask here, I've been working around this because it isn't a huge issue for me, but it will be when I submit my work.
Problem
I have a soundplayer, it works fine on my PC and does exactly what I want it to do on this PC.
SoundPlayer player = new SoundPlayer(#"C:\Users\Font\Desktop\GRADED_UNIT_SOLUTION_PLANNING_UPDATED\HillRacingGraded\HillRacingGraded\Resources\Audio\" + track + ".wav");
The problem is the path.
because of THIS path my program won't work on ANY other System apart from the one it's working on right now. It crashes soon after startup.
And.. as you can probably see from the path, it's going to eventually be graded, so my lecturer will need to use this program without having to switch around a directory.
How can I get the Soundplayer path to start at "HillRacingGraded\ ...\ ..."?
Rather than it starting at the C: drive.
If you're using System.Media.SoundPlayer, it supports reading of sound files from streams and I've seen it successfully used in the past from the UnmanagedResourceStream you get from an embedded resource. So one option would be to embed your sound file as a resource in your application and play it from there.
If embedding the file isn't an option, you can get paths relative to your executable folder using code similar to that shown below. Then you just have to provide your executable and the resource files in a subdirectory (but be careful with GetExecutingAssembly() if there's any chance your code is in a DLL and can be hosted by an arbitrary executable).
var assembly = Assembly.GetExecutingAssembly();
var folder = Path.GetDirectoryName(assembly.Location);
var soundFile = Path.Combine(folder, "MySoundFile.wav");
The special folder path helpers can also reduce the hard-coding but work best when your application will be installed via an installer and you can put files in the appropriate places automatically (which isn't likely to be the case for a school project).
For me (VS 2022, .net 6, C # 10) it worked:
Import the "file.wav" file into the main directory.
Change in: Properties (file.wav) - Copy to Output Directory to: Copy always.
Later it was enough to:
SoundPlayer player = new SoundPlayer("file.wav");
player.Load ();
player.Play ();

Windows Form Application : Publish issue

I'm very new to Visual Studio 2010. I decided to create a simple WFA with C#.
Everything work fine with Images and Audio playback. My intention is to create a standalone application and send it to my friend as a gift. Problem I facing now is that when I tried to publish the application, the Images / Audio is still using the same location in my PC. So it won't play nor display any images.
Any comment on this or guide for me? I did try search for solution but doesn't seems to have any luck.
This is the code that I used to play Audio :
SoundPlayer ply = new SoundPlayer(#"C:\Users\Liam619\Documents\Visual Studio 2010\Projects\BirthdayApp\BirthdayApp\Resources\BirthdaySong.wav");
If I remove or change the path, the Application will hit error for locating the Audio file.
There may be several solutions to your problem.
a) embed the sound into a resource. As a beginner, resources may be a bit tricky to get it right the first time. But I want to encourage you reading something about it. You'll need resources when you want to translate your first program.
b) create an installer which copies the sound file to the installation directory. Try InnoSetup. If you're a programmer, sooner or later, you'll need to create a Setup anyway. Always worth knowing how to do that.
In that case, you still need the path to the sound file, but if you install your EXE into the same path as the sound file, see getting the application's executable directory.
everything in the database whether images or audio refers to your own server database.you have to send the database too with the app and the correct version .NET framework needs to be installed on the target PC.

WMP sounds are not working on other Computers

My project is to build a simple game and for now I only made the Main Menu Form1 with 5 buttons. The buttons have a MouseClick and MouseEnter on them, and I have a background music track using a WMP method (using WMPLib and axWMPLib).
My problem is when I'm taking the bin/debug putting it on a rar file and giving it to my friends, they say they don't hear the sound. I made the project trough WMP version 11, so I asked them if their WMP version is 11 and they said yes. I have no idea why I hear the sounds on my computer and they don't.
I tried to give them the folders:
bin\Release
bin\Debug
x86\Release
x86\Debug
but they still said that they can't hear any sound from all of them.
EDIT
All my sounds are in a folder called "Sounds". I found some details and found out that you need to embeding those WMP sounds to "Resources".
So how can I do that, and how I call them when Form1 loads up. And no, this following code doesn't work:
BackGround.URL = Properties.Resources.Invincible;
It says I can't convert System.IO.UnmanagedMemoryStream to String.
Sincerely I don't recommend you to use WMP, so give a look to the SoundPlayer class.
However I think the problem is that you don't give the correct location to your files. So what you can do is to locate your files in your application folder, get its location and create the location of the music files.
So try:
string musicName = Application.StartupPath + "music.mp3";
Or if you have a Sounds folder in the application path use:
string musicName = Application.StartupPath + "\\Sounds\\music.mp3";
Else if you insert your music file in the application resources this:
BackGround.URL = Properties.Resources.Invincible;
didn't work because BackGround.URL is of type string while Properties.Resources.Invincible is the music file stream.
I don't know if using WMP you can set the stream from where it can play the file. Although the SoundPlayer class I linked previously contains a property from where you can set the input stream. You can do it in this way:
SoundPlayer mySoundPlayer = new SoundPlayer();
mySoundPlayer.Stream = Properties.Resources.Invincible;
mySoundPlayer.Load();
mySounPlayer.Play(); //plays the Properties.Resources.Invincible sound

OpenFileDialog/c# slow on any file. better solution?

I am opening a file using the OpenFileDialog in c# and I am noticing it is taking between 20-40 seconds to load my file and clear the dialog.
Here is my sample code:
private void btnOpen_Click(object sender, EventArgs e)
{
if (ofdSettings.ShowDialog() == DialogResult.OK)
{
// do nothing
}
}
even with this limited example it takes the 20-40 second duration for the dialog to clear.
the file i'm selecting is a xml file that is only 1.36kb large
I had the same problem, openFileDialog1.ShowDialog() was slow, taking 10 seconds after closing it to execute the next line of my program.
I noticed in the dialog that I had a couple old shortcuts under "Computer" pointing to webdav url's which were no longer valid. I deleted these shortcuts from windows explorer, and the program is fast now.
Check if you have any network connection shortcuts tied to your computer, which also display in the dialog (on the left-hand panel in Windows 7). Try removing them and see if the dialog is faster.
Another option which helped in my case:
OpenFileDialog ofd = new OpenFileDialog
{
...
AutoUpgradeEnabled = false
};
With this option, OpenFileDialog renders simpler UI, "pre-Vista" style according to MSDN article.
I also had this problem when I want to open a example.url file with file open dialog. It takes 0-10 seconds. Then I find out that this has something todo with the file type association (*.url) When I changed the association from default web browser to notepad++ the problem was gone. But I this was no solution for me, because when somebody clicked on a example.url, the default browser should open this file. To solve this I added DereferenceLinks = false.
OpenFileDialog ofd = new OpenFileDialog
{
...
DereferenceLinks = false
};
For me this solution works perfect
You can use a free tool like ProcExp (SysInternals.com) to monitor what your application is doing during the lag. Is it scanning the file system? The registry? The network (maybe it is trying to connect to a network share that is slow to respond).
BTW, you can run ProcExp.exe without installing it from http://live.sysinternals.com/!

Categories