adding music on forms app
i added a main menu music that plays on main menu yes great and also selection screen also great it stops at the loading screen which is what i wanted but im trying to add another song for loading screen and no matter what i do it wont play
51 Replies
i can send the code
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/i used chatgpt for help cause when i tried it didnt work and chatgpt didnt fix anything either
BlazeBin - zsgmksxluohb
A tool for sharing your source code with the world!
- you're instantiating two soundplayers for some reason
- have you tried playing the same file as the main menu to rule out file issues?
yes ive tried
the main menu music didnt work either if im not wrong
also am i supposed to use the same sound player?
i thought i had to make a new one since its gonna play something else
i'm not sure. i'm talking about line 22 of your code specifically.
let me see
yeah
im confused
by?
i dont get it
i checked the line
the code you've just pasted, you create a new instance of SoundPlayer on line 22 and assign it to Loadingmusic
then on line 23 you create another instance of SoundPlayer and assign it to Loadingmusic
the one created and assigned on line 22 never does anything
can you tell me how i really dont know anything about forums
create a new soundplayer right
why do you have line 22?
oh yeah about that
i fixed it
but i forgot to send the new version
my bad
should i send the updated version
go for it
and send the equivalent code from your other form that does work
BlazeBin - qshacdhpbxue
A tool for sharing your source code with the world!
i also changed the name of the file
both in files
and in the code
oh fuck me
my bad
wrong one again
so sorry
here is the final code of loading screen
BlazeBin - cxkccinjxzfd
A tool for sharing your source code with the world!
and im gona send the selection form
BlazeBin - fcxzsmistjks
A tool for sharing your source code with the world!
right, so that form is having its soundplayer passed in via constructor. where's the code that does that?
yes let me send it
its in the main menu
BlazeBin - hmbcsipawekw
A tool for sharing your source code with the world!
so the main screen loads and plays the wav file correctly?
yes
do you want me to record it?
and when you click on label3, the selection form is shown and the sound continues to play?
yes
nevermind i just am facing another error that i was not facing
let me fix that
on the mainscreen right now
and you then click on startgame, the sound stops and the loading screen is shown?
yes
oh what the hell now the label3 is giving an error it was working perfect
let me fix that rq
okay fixed
let me show you the program real quick
and now it works?
dude i swear it didnt work before and i didnt face any of those issues
and also another question when i mistakenly add an event how do i remove it
thats what casued me errors in my program i deleted them manually from the code
what do you mean by add an event?
like lets say i double click on a button that i am not supposed to and it adds a click event
you mean it adds a function to act as an event handler?
yes i think so it adds a "click" event
the event already exists (it's part of the framework), you're talking about generating an event handler and wiring it up
and i want to add music to the game screen aswell so the same steps right ?
if you're using the GUI i believe you'd need to go back onto the button, go to its properties and remove the handler there. you can then just delete the code.
so remove handler and delete the code
got it
it's been a long time since i worked with win forms but it'll be something like that
and also one more question i want to add a specific sound effect when a button is clicked
but i want it to play once only
and then move to the next forms
so i just add a sound player and then make it during the mouseclick event right
i am a really bad perfectionist because of the menu and sound stuff i didnt even get to develpp my game yet
yeah but another thing to bear in mind is that playing sound uses external resources which is why these SoundPlayers implement IDisposable
what does that mean
you'll want to give this a read https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/using-objects
Using objects that implement IDisposable - .NET
Learn how to use objects that implement the IDisposable interface in .NET. Types that use unmanaged resources implement IDisposable to allow resource reclaiming.
i added them to my project so it could play on my professors computer
would it not work then?
to the resources
as long as you distribute them with the application, yes
i will read this
im confused what do you mean
i'm talking about resources like file handles etc rather than the actual files
and other system resources it'll be using to play the audio
so you want to make sure that the SoundPlayers are disposed of (you'll see what that means when you read the link)
also how do i export my project as a rar and one more thing
i want to save a backup project like this one works perfectly
i want to save this as a new one
and move on another just inc ase somethnig goes wrong
your click handler will end up something like
thanks
- you'll end up publishing the application (or just using the build output) and you'll have to RAR it separately
- generally people keep track of the state of their source code using a version control system (VCS). git is by far the most popular VCS in use these days
anyhow i've got to dip, you've got a bunch of reading to do so good luck.
thanks