-3

i creating one game and i want to add a sound effect. How do I do it. i dont know please help me. I did an archery game.

for example :

if( myArrow.left >= board.lef )
{
  //How should I write the code 
}
svick
  • 236,525
  • 50
  • 385
  • 514
Turgut Kanceltik
  • 639
  • 1
  • 8
  • 18

1 Answers1

2

If the file you want to play is wav file. You use SoundPlayer else you use WindowsMediaPlayer

For the SoundPlayer

using System.Media;

SoundPlayer player = new SoundPlayer("c:/path to file.wav");
player.Play();
//or
player.PlayLooping();
//then whenver you want to stop
player.Stop();

For WindowsMediaPlayer

using WMPLib;

WindowsMediaPlayer player = new WindowsMediaPlayer("c:/path to file.mp3");
player.controls.play();
//if you want to pause
player.controls.pause();
//if you want to stop
player.controls.stop();

You can also create a playlist and loop through it in you windows media player. (Just Check that article).

NOTE : The WMPLib namespace might not be in your project you need to add it by right clicking on your toolbox and clicking Choose Items. Add under COM Components select WindowsMediaPlayer. If the namespace is not still added then drag the control to your form and then delete it from your form. It will be added

And of course you can use Console.Beep(); for Console Applications.