SoundPlayer
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: SoundPlayer

  1. #1
    Join Date
    Dec 2010
    Posts
    4

    SoundPlayer

    Hello some questions about code ...
    Code:
    private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) 
    		 {
    			 System::Media::SoundPlayer player("C:\\Windows\\Media\\ding.wav");
                            player.Play();
    		 }
    Is there any idea how to include sound file into program ? Without this path from drive.

    And one more, if i press button and the music is playing, button is pressed, when i press again it's unpressed and music stop.

    Thanks,

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,594

    Re: SoundPlayer

    Quote Originally Posted by Nie View Post
    Is there any idea how to include sound file into program ? Without this path from drive.
    You mean you want to have the sound file as a resource inside your .exe file? I once tried something similar (for a cursor resource, though) in http://www.codeguru.com/forum/showthread.php?t=504282 and finally had to resort to some hack. That hack would work here as well but I don't think it's really elegant or convenient.

    However, in the meantime I found some things on MSDN I didn't know yet then that might allow to get rid of the hack. But that could get a bit complicated and therefore I don't want to make further experiments in this direction before I know that this is actually what you want.

    And one more, if i press button and the music is playing, button is pressed, when i press again it's unpressed and music stop.
    This is a bit easier. To be able to stop the sound once you've started it, the SoundPlayer object not only needs to persist beyond the scope of the button's click handler, it also has to remain accessible. To achieve this, I made the player object a member of the form class. I also added a bool variable that indicates whether the program currently is in the "playing" state:

    Code:
        // Additional members:
    
      private:
        Media::SoundPlayer ^m_Player;
        bool m_bPlaying;
    The two members are initialized upon the form's construction:

    Code:
      public:
        Form1(void) : m_Player(gcnew Media::SoundPlayer("C:\\Windows\\Media\\ding.wav")), m_bPlaying(false)
        {
          InitializeComponent();
          //
          //TODO: Konstruktorcode hier hinzufügen.
          //
        }
    This is the button click handler that handles starting and stopping of the sound playback:

    Code:
    System::Void Form1::btnPlayStop_Click(System::Object^  sender, System::EventArgs^  e)
    {
      Button ^btnMe = static_cast<Button ^>(sender);
      if (!m_bPlaying) {
        m_Player->Play();
        btnMe->Text = "Stop";
        m_bPlaying = true;
      } else {
        m_Player->Stop();
        btnMe->Text = "Play";
        m_bPlaying = false;
      }
    }
    Note that I placed this event handler in a separate Form1.cpp file. If you want to place it inline in the class body in Form1.h (like the IDE does it), simply don't qualify the handler function's name with Form1::.

    Windows Form's standard Button class doesn't support the two-state pressed/released mode. To compensate this I am changing the button's caption instead. A ToolStripButton supports that mode, however.

    Also note that there seems to be no way of detecting whether/when the SoundPlayer has finished playing the sound. Therefore the button remains in the role of a Stop button even after the sound has finished. Pressing it in this state has no effect besides reverting the button back into a Play button which then behaves as expected.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,594

    Re: SoundPlayer

    I don't know whether you're still interested in a way of adding the audio file as a resource to your .exe at all since it's been some weeks now that you didn't answer my last post, but even if not: Maybe other viewers are.

    After adding ding.wav to the .resX file as an audio resource I could load it using the ResourceManager class. The Form1 constructor that instantiates the SoundPlayer now looks like that:

    Code:
      public:
        Form1(void) : m_bPlaying(false)
        {
          InitializeComponent();
          //
          //TODO: Konstruktorcode hier hinzufügen.
          //
    
          ResourceManager ^resm = gcnew ResourceManager(Form1::typeid);
          m_Player = gcnew SoundPlayer(resm->GetStream("ding"));
        }
    Wasn't really that complicated either after all...
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center