Search code examples
c#visual-studiowinformsnaudiosoundplayer

Controlling Sounds in audio in multiple forms


I want to play a playlist in the resources folder for a windows forms project I'm working on (wav or mp3). The application will be completely offline. (actually a game project)

What I want to do is this:

I need to be able to stop or convert a music that I created in one form to another sound file, depending on the situation.

In this case, I couldn't figure out how to write a code block. There are many examples that describe how we can pause and start the music on single form, but I have not come across an example that covers the whole project and can be intervened from different forms.

I'm not sure if I explained exactly what I wanted to ask, but I can give an example as follows. Let's say I created 3 forms.

1st form and 2nd form game menus. In these menus, the same music should continue to play.

But when I reach the 3rd form, this music should be cut and replaced with in-game music.

What I want to ask is: How can I control the music I created in form 1, in form 2 or form 3?

The reason I don't use the soundplayer class is because I have to play multiple sounds at once.

i.e., somehow I need to make the music player "static" so that I can control it from all forms, but I couldn't find the way around it.

var importer = new RawSourceWaveStream(Properties.Resources.sound, new WaveFormat());
 var soundFx = new WaveOut();
 soundFx.Init(importer);
 soundFx.Play();

For example, in this way, I start playing the music in the form1. I need to give the command to change this music in the form3, but somehow I cannot reach the "soundfx" object that I defined there because I cannot make this process static.


Solution

  • It is recommended that you use MediaPlayer. From your description, MediaPlayer can better meet your needs. Compared with SoundPlayer, MediaPlayer has the following characteristics:

    1. Multiple sounds can be played at the same time (create multiple MediaPlayer objects);
    2. You can adjust the volume (Volume property);
    3. You can use Play, Pause, Stop and other methods to control;
    4. You can set the IsMuted property to True to achieve mute;
    5. You can use the Balance property to adjust the balance of the left and right speakers;
    6. The speed of audio playback can be controlled through the SpeedRatio property;
    7. The length of the audio can be obtained through the NaturalDuration property, and the current playback progress can be obtained through the Position property;
    8. Seek can be performed through the Position property;

    Use MediaPlayer to play audio files as follows:

       MediaPlayer player = new MediaPlayer();
       player.Open(new Uri("BLOW.WAV", UriKind.Relative));
       player. Play();
    

    A MediaPlayer object can only play one file at a time. And the file is played asynchronously, you can also call Close to release the file. For details, please refer to https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.mediaplayer?view=windowsdesktop-7.0

    When using it in VS, you need to perform the following steps first:

    enter image description here

    enter image description here

    This way you can find MediaPlayer in the toolbar.

    If you still have any questions please speak up.