9 Replies - 10283 Views - Last Post: 22 January 2013 - 02:49 PM

#1 Decord  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 19-December 12

[C++ .NET] Include a sounds file and play it

Posted 22 January 2013 - 11:16 AM

Hey guys!

I'm just putting the final pieces together for a launcher I'm making for a game.
I just figured that it would be pretty cool to have a music file playing. (Gonna add an option to mute it, but however:)

What I'm interested in learning:

  • A way to include a music file (.wav/.mp3) to the .exe so the user doesn't have to have the file seperatly on his/hers computer in order to play the sound.
  • A way to simply play the file. For example on Form_Load event.


Thanks a bunch in advance!
I've really been thinking of learning how to do it, but I've just never sat down and tried it.

Kind regards, Decord

Is This A Good Question/Topic? 0
  • +

Replies To: [C++ .NET] Include a sounds file and play it

#2 Decord  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 19-December 12

Re: [C++ .NET] Include a sounds file and play it

Posted 22 January 2013 - 11:32 AM

After a quick look I found PlaySound, but when I try to use it, all it does is a System BEEP sound.

My code is like following:
PlaySound(TEXT("SystemStart"), NULL, SND_ALIAS);


What's wrong?
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,165
  • Joined: 05-May 12

Re: [C++ .NET] Include a sounds file and play it

Posted 22 January 2013 - 11:43 AM

There is a specific subforum for C++/CLI, but admittedly there is very few people who visit that subforum.

Anyway, what you are looking to do is embed the media file as resource.

If you are using WinForms, then you can open the resource as a stream and pass that to the constructor of the SoundPlayer class.

If you are using WPF, then you can probably pass a URI to the MediaPlayer.Play() of the form res:// pointing to the resource in your executable. (I'm saying probably because I've never tried this before in WPF. I've only used the res:// trick before with the older unmanaged HTML dialogs suppplied by Microsoft. Usually MS believes in the principle of least surprise and backward compatibility, but with the recent era of Windows 8, some of that is being thrown out the window.

This post has been edited by Skydiver: 22 January 2013 - 11:43 AM

Was This Post Helpful? 1
  • +
  • -

#4 Decord  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 19-December 12

Re: [C++ .NET] Include a sounds file and play it

Posted 22 January 2013 - 11:44 AM

(I couldn't find the Edit Post button, sorry)

I went ahead trying simple solution like:

PlaySound(TEXT("C:\\Test.wav"), NULL, SND_ALIAS); //Having the file in my C folder

and also
PlaySound(TEXT("Test.wav"), NULL, SND_ALIAS); //Having the file in my project folder


However, when I try to play the sound it just gives me the defaul windows BEEP
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,165
  • Joined: 05-May 12

Re: [C++ .NET] Include a sounds file and play it

Posted 22 January 2013 - 11:49 AM

It gives you a beep in both cases, or just the second case?
Was This Post Helpful? 0
  • +
  • -

#6 Decord  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 19-December 12

Re: [C++ .NET] Include a sounds file and play it

Posted 22 January 2013 - 11:52 AM

View PostSkydiver, on 22 January 2013 - 11:49 AM, said:

It gives you a beep in both cases, or just the second case?


Both cases
Was This Post Helpful? 0
  • +
  • -

#7 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 802
  • View blog
  • Posts: 2,363
  • Joined: 20-March 10

Re: [C++ .NET] Include a sounds file and play it

Posted 22 January 2013 - 12:44 PM

It might be better as Skydiver has said to use

SoundPlayer.

code for WinForms using namespace System::Media
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

	
        SoundPlayer^ sndplayr = gcnew SoundPlayer(L"C:\\music.wav");
        sndplayr->Play();
   
    
			 }



Snoopy.
Was This Post Helpful? 0
  • +
  • -

#8 Decord  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 19-December 12

Re: [C++ .NET] Include a sounds file and play it

Posted 22 January 2013 - 01:08 PM

View Postsnoopy11, on 22 January 2013 - 12:44 PM, said:

It might be better as Skydiver has said to use

SoundPlayer.

code for WinForms using namespace System::Media
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

	
        SoundPlayer^ sndplayr = gcnew SoundPlayer(L"C:\\music.wav");
        sndplayr->Play();
   
    
			 }



Snoopy.


SoundPlayer^ sndplayr = gcnew SoundPlayer(L"C:\\music.wav");
        sndplayer->Play();



It tells me that music.wav (test.wav) isn't a valid .wav file :o
Was This Post Helpful? 0
  • +
  • -

#9 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 802
  • View blog
  • Posts: 2,363
  • Joined: 20-March 10

Re: [C++ .NET] Include a sounds file and play it

Posted 22 January 2013 - 02:04 PM

Might be that your .wav file isnt a .wav file but a .mp3 file that has been renamed as .wav

download a .wav file from the net and try that

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#10 Decord  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 19-December 12

Re: [C++ .NET] Include a sounds file and play it

Posted 22 January 2013 - 02:49 PM

View Postsnoopy11, on 22 January 2013 - 02:04 PM, said:

Might be that your .wav file isnt a .wav file but a .mp3 file that has been renamed as .wav

download a .wav file from the net and try that

Snoopy.

Oh right, of course it couldn't be that simple as just renaming the file.. :)
Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1