Search code examples
c#visual-studiowinformsaudiowav

Windows error sound instead of music is played


I have a little code that is supposed to play music in the background. The problem is that the music does not play, but the sound of the Windows error is played.

System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream resStream =
    assembly.GetManifestResourceStream(@"SuperJenya.horror_music.wav");
SoundPlayer player = new SoundPlayer(resStream);
player.PlayLooping();
player.PlaySync();

Solution

  • SoundPlayer.PlaySync Method No .wav file specified or could not be loaded

    The PlaySync method uses the current thread to play the .wav file, preventing the thread from processing other messages until loading is complete. After a .wav file has been successfully loaded from a stream or URL, subsequent calls to the SoundPlayer's play method will not require reloading the .wav file until the sound's path changes.

    If a .wav file has not been specified or cannot be loaded, the PlaySync method will play the default beep sound. So it seems to fit well with what you said about the Windows error sound being played. SoundPlayer.PlayLooping has the same problem.

    A .wav file can be loaded into memory ahead of time using the LoadAsync or Load methods. Or use the Play method directly

    Of course, if this still doesn't work, it doesn't rule out that there is something wrong with your wav file.

    There are also the following suggestions:

    1. If none of the above operations work, you can also use JetPlayer. Because SoundPool can only apply for a maximum memory space of 1M, this means that we can only use some very short sound clips instead of using it to play songs or game background music.

    2. SoundPool provides pause and stop methods, but these methods are recommended not to be used lightly, because sometimes they may cause your program to terminate inexplicably. Some friends also reported that they will not stop playing the sound immediately, but will stop after playing the data in the buffer, and may play for one second longer.

    3. The audio format is recommended to use OGG format. Use WAV format audio files to store game sound effects. After repeated tests, when the sound effect playback interval is short, it will be abnormally closed (it is said that SoundPool currently only supports 16bit WAV files). Later, the file was converted to OGG format, and the problem was solved.