Controlling Sounds in audio in multiple forms

121 Views Asked by At

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.

2

There are 2 best solutions below

0
Node defender On BEST ANSWER

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.

0
Jazz. On

I think that your intuition is valid. It is probably not the best way, but I inffer that it is a way that you would manage to do.

  1. Have a public static class, that holds a private (or public) instance of you sound player. Have public static methods of the different things that you want to do with the audio player.
  2. Control that class from all forms.

Since this is your intuition. I am curious of what have you so far in this way?

Anyhow, I have a few questions.

  • When you say "convert" the music, what do you mean?
  • What do you mean that you couldnt find a way around it?
  • Can you add some code of how you are controlling the sounds? With some code, people will be able to better support you.