How to play WAV audio file from Resources?

后端 未结 7 2140
面向向阳花
面向向阳花 2020-12-02 10:10

How can I play a WAV audio file in from my project\'s Resources? My project is a Windows Forms application in C#.

7条回答
  •  一个人的身影
    2020-12-02 10:23

    When you have to add sounds into your project, you will do so by playing .wav file(s). Then you have to add the .wav file(s) like this.

       using System.Media; //write this at the top of the code
    
       SoundPlayer my_wave_file = new SoundPlayer("F:/SOund wave file/airplanefly.wav");
       my_wave_file.PlaySync(); // PlaySync means that once sound start then no other activity if form will occur untill sound goes to finish
    

    Remember that you have to write the path of the file with forward slashes (/) format, don't use back slashes (\) when giving a path to the file, else you will get an error.

    Also note, if you want other things to happen while the sound is playing, you can change my_wave_file.PlaySync(); with my_wave_file.PlayAsync();.

提交回复
热议问题