Search code examples
c#soundplayer

playing of .wav file


I am making a piano app, where each key plays a different .wav file. For instance, if the C note is pressed then C.wav is played.

So far I have only managed to play a wav file using the code in the screenshot, otherwise the file is not found. However this method is not suitable for what I am trying to do. The wav file i am trying to play is called hello.wav and I have provided the location. Any suggestions?

This is the current code:

SoundPlayer sp = new SoundPlayer(PianoApp.Properties.Resources.Hello);
sp.Play();

or as it appears in my IDE:

current code

This is the file location:

file location


Solution

  • I,

    Add you your file .wav in ressource of your project with mySoundFile as name, and code like this

     Stream str = Properties.Resources.mySoundFile;
     SoundPlayer snd = new SoundPlayer(str);
     snd.Play();
    

    with multiple .wav ressources

     Stream str1 = Properties.Resources.mySoundFile1;
     Stream str2 = Properties.Resources.mySoundFile2;
    
     RecordPlayer rp = new RecordPlayer();
     rp.Open(new WaveReader(str1));
     rp.Play();
    
     rp.Open(new WaveReader(str2));
     rp.Play();