Does anyone know how to STOP soundPlayer when form is closed

C, C++, Visual C++, C++.Net Topics
Post Reply
User avatar
Trebor29
Lieutenant
Lieutenant
Posts: 75
Joined: Thu Apr 29, 2010 12:34 am

Does anyone know how to STOP soundPlayer when form is closed

Post by Trebor29 » Sat Jan 08, 2011 8:55 pm

Hi,
Im creating a tick tack toe game in Visual Studio C# and have added sounds throughout the game already. I have just added a form to act as a messageBox for when the player levels up and have added my sound to the form_load...

The problem I am having is that when the player clicks OK and the messageBox form closes, the sound does not stop. I have tried all sorts but it just keeps rejecting me with those bloody red lines!! :?
I have also tried to open a new playGameForm (the form it returns to), which is what I have dont on the others but this is not ideal as when this form loads it has a sound of its own, and I do not want it to repeat that sound so soon during the users experience. Im sure I can use "something.Stop();" but dont know what the "something is"!!

Code: Select all

namespace TickTackToeAssignment
{
    public partial class MessageBoxForm : Form
    {
        public MessageBoxForm()
        {
            InitializeComponent();
        }

        private void OkButtonMsgBox_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void MessageBoxForm_Load(object sender, EventArgs e)
        {
            SoundPlayer heartBeat = new SoundPlayer();
            heartBeat.SoundLocation = Application.StartupPath + "\\..\\..\\heartBeat.wav";
            heartBeat.PlayLooping();
        }
    }
}
User avatar
Herath
Major
Major
Posts: 417
Joined: Thu Aug 05, 2010 7:09 pm

Re: Does anyone know how to STOP soundPlayer when form is closed

Post by Herath » Sat Jan 08, 2011 11:27 pm

declare the soundplayer object as a class member variable, and not inside the form load event. do you initialization and playing of sound inside form load event. upon event for exiting from form is called, stop the soundplayer by calling the relavent method. that should do it. :).
the reason why your sound keep playing must be due to the reason that ur sound is playing in another thread.so u might have to do cros thread operations!

[ Post made via Mobile Device ] Image
User avatar
Trebor29
Lieutenant
Lieutenant
Posts: 75
Joined: Thu Apr 29, 2010 12:34 am

Re: Does anyone know how to STOP soundPlayer when form is closed

Post by Trebor29 » Sun Jan 09, 2011 12:07 am

yep, that done.... :lol:

Thanks dude!
User avatar
Herath
Major
Major
Posts: 417
Joined: Thu Aug 05, 2010 7:09 pm

Re: Does anyone know how to STOP soundPlayer when form is closed

Post by Herath » Sun Jan 09, 2011 7:31 am

just wanted to add some info. normally. initializations are done inside the class constructor. and releasing of resources used is done in the destructor method. but in .net you do not worry much about memory management. it is automatically done by the .net runtime for managed code.

[ Post made via Mobile Device ] Image
User avatar
Trebor29
Lieutenant
Lieutenant
Posts: 75
Joined: Thu Apr 29, 2010 12:34 am

Re: Does anyone know how to STOP soundPlayer when form is closed

Post by Trebor29 » Sun Jan 09, 2011 9:59 pm

Hi, thanks for that Herath!

Interesting comment!! :ugeek:

I am also doing android development for a work based project and have learnt that, as memory and battery power is limited on mobile devices/handsets, size and memory are an important consideration.
Do you think your comments are relevant for this area... android development? :?:

Im taking them on board anyway, but am just interested to know.

Thanks. Trebor
User avatar
Herath
Major
Major
Posts: 417
Joined: Thu Aug 05, 2010 7:09 pm

Re: Does anyone know how to STOP soundPlayer when form is closed

Post by Herath » Sun Jan 09, 2011 10:27 pm

java virtual machine does automatic garbage collection. but you will have to pay attention on cpu power required for execution of code. like not depending on loops and arrays for data storage and manipulation. selecting proper data structures will be important. i do not know much about mobile and embedded development at its core level. aforementioned is my general idea. :)

[ Post made via Mobile Device ] Image
Post Reply

Return to “C/C++ Programming”