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!
Related
I am using the following code:
SoundPlayer soundPlay = new SoundPlayer(#"C:\more\more\Assets\Sounds\menuHoover.mp3");
soundPlay.Play();
when clicking in a button to reproduce a sound, but I have to use the absolute path to works, since I am not the only one doing working on this project, I have to use a poth comon to others too. How can I use the relative path to make this work?
I have tried diferent paths, but none seems to work. Only the absolute one.
You may include the file as Embedded Resource into your executable. Then there is no need to distrubute the file separately.
To access the sound resource, use Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("<Namespace>.menuHoover.mp3"); and initialize the SoundPlayer as new SoundPlayer(stream);
What to put in <Namespace>? Have a look in the project properties at "Default namespace". Suppose this is A.B.C. This would be the Namespace if you add the mp3 file at the root directory of the project. If you add it to a sub-directory, say X\Y\Z, the namespace would be A.B.C.X.Y.Z (special rules apply if the directory name contains characters not allowed in an identifier). See also Name of embedded resource
You have several options one is to add the location of the assets to a configuration file such as app.config
For example you could add to your app.config file:
<configuration>
<appSettings>
<add key="AssetPath" value="C:\\more\\more\\Assets\\Sounds\\"/>
</appSettings>
</configuration>
Then in your code you could use something like:
string assetPath = ConfigurationManager.AppSettings["AssetPath"];
SoundPlayer soundPlay = new SoundPlayer(Path.Combine(assetPath,"menuHoover.mp3");
soundPlay.Play();
Another would be to build a path dynamically based on where your application was executed from and then build a relative path from that:
string appDirectory = AppDomain.CurrentDomain.BaseDirectory;
string assetPath = Path.Combine(appDirectory,"Assets","Sounds");
SoundPlayer soundPlay = new SoundPlayer(Path.Combine(assetPath,"menuHoover.mp3");
soundPlay.Play();
sorry im very new to unity and c#.
Im trying to read an image and apply it as a texture.
The code works when i try it with an image on my computer, however, when i try it with my android devices (phone and AR glasses), i've not been able to specify the file path correctly. How do i specify the file path properly of android devices or is there a way i can get these file path?
Thank you so much for any help in advance! :)
void Start()
{
thisTexture = new Texture2D(100, 100);
//string path = "C:/Users/kenny/Desktop/5th March/im.png"; //this works
string path = "file:///storage/emulated/0/im2.png"; // this doesnt work
bytes = File.ReadAllBytes(path);
thisTexture.LoadImage(bytes);
GetComponent<Renderer>().material.mainTexture = thisTexture;
}
For mobile devices it is not as straightforward to get a file from a plain path.
One way of doing so is by using the Resources folder. In your root folder (Assets), create a new folder named Resources. Put your image there.
Then you can do something like this:
// path without file extension!
var texture = Resources.Load<Texture2D>("path/to/texture");
You can get more references for this functionality here: https://forum.unity.com/threads/how-to-load-a-image-from-the-resources-folder-to-a-texture2d.101542/
Alternatively, you could use the StreamingAssets folder, but that's another story.
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
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.
I am trying to set a filepath for a SoundPlayer object
If I have a sounds folder in my main project folder. How do I go about sending
Soundplayer test = new Soundplayer("Sounds/Fireball.wav");
Where the file is relative to your main project is not important. What's important is where will the sound file be relative to your application at deployment / debug time. If it will have the same relative path as that of the main .exe path then you can use the following.
var root = typeof(Program).Assembly.Location;
var soundPath = Path.Combine(root, #"sounds\Fireball.wav");
var test = new SoundPlayer(soundPath);
Have you tried the path as #"Sounds\Fireball.wav"?
If you are running out of Visual Studio, the current working directory will be bin\Debug, so the file in question would need to be in bin\Debug\Sounds\Fireball.wav.
Also, as others have mentioned, you should use backslash \ rather than forwardslash /