0

When I run in Microsoft Visual Studio, the sound works. However, after I published it, the sound doenst work. The sound properties are set to Content. Can someone help? Thanks

private void InitializeSound()
        {
            // Create an instance of the SoundPlayer class.
            player = new SoundPlayer(@"Sounds\sirenpolice3.wav");
        }

enter image description here

  • are the sounds being copied to the output folder when you publish? – shox Nov 24 '20 at 21:16
  • what do you mean by output folder? After published, there is only an application(.exe) and a Program Debug Database (.pdb). The Deployment mode of the application when published is self-contained. – Sophie Chai Nov 24 '20 at 21:20
  • You are attempting to play a sound from a specific folder (relative to the executable), yes all the DLLs etc are self contained, the resources are not. Copy your sounds folder to where you are running and it should work. If you want the sounds embedded you'll need to have them as a resource rather than just a folder. – shox Nov 24 '20 at 21:48
  • okay, so I set the property of the audio to Resources. It throws a FileNotFound exception. I think I missed some steps. How should I set it as a resource? – Sophie Chai Nov 25 '20 at 17:49
  • This should help. You need to create it as a resource then access it as a resource. https://stackoverflow.com/questions/90697/how-to-create-and-use-resources-in-net#:~:text=Right%20click%20the%20project%20you,Click%20the%20%22Resources%22%20tab. – shox Nov 25 '20 at 18:46
  • it works! thank you! – Sophie Chai Nov 25 '20 at 20:02

1 Answers1

0

try this sample

using (var player = new SoundPlayer(@"Sounds\sirenpolice3.wav"))
        {
            // Use PlaySync to load and then play the sound.
            // ... The program will pause until the sound is complete.
            player.PlaySync();
        }
AVTUNEY
  • 923
  • 6
  • 11