控制音频中的声音以多种形式

huangapple go评论54阅读模式
英文:

Controlling Sounds in audio in multiple forms

问题

我想在我正在进行的 Windows Forms 项目中的资源文件夹中播放一个播放列表(wav 或 mp3)。该应用程序将完全脱机运行(实际上是一个游戏项目)。

我想做的是:

我需要能够停止或转换在一个窗体中创建的音乐,具体取决于情况。

在这种情况下,我无法想出如何编写代码块。有很多例子描述了如何在单个窗体上暂停和开始音乐,但我还没有找到覆盖整个项目并可以从不同窗体进行干预的示例。

我不确定我是否已经准确解释了我要问的问题,但我可以举个例子。假设我创建了 3 个窗体。

第一个窗体和第二个窗体是游戏菜单。
在这些菜单中,相同的音乐应该继续播放。

但当我到达第三个窗体时,这个音乐应该被切断并替换为游戏内的音乐。

我想要问的是:我如何能够控制我在第一个窗体中创建的音乐,以在第二个或第三个窗体中进行控制?

我不使用 SoundPlayer 类的原因是因为我必须同时播放多个声音。

也就是说,我需要以某种方式使音乐播放器 "static",以便我可以从所有窗体中进行控制,但我找不到方法来实现它。

例如,以这种方式,我在第一个窗体中开始播放音乐。我需要发出命令在第三个窗体中更改这个音乐,但由于我无法将这个过程设为静态,所以无法访问我在那里定义的 "soundfx" 对象。

英文:

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.

答案1

得分: 0

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.
英文:

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.

答案2

得分: 0

建议使用MediaPlayer。根据您的描述,MediaPlayer可以更好地满足您的需求。与SoundPlayer相比,MediaPlayer具有以下特点:

  1. 可以同时播放多个声音(创建多个MediaPlayer对象);
  2. 您可以调整音量(Volume属性);
  3. 您可以使用Play、Pause、Stop等方法进行控制;
  4. 您可以将IsMuted属性设置为True以实现静音;
  5. 您可以使用Balance属性调整左右扬声器的平衡;
  6. 通过SpeedRatio属性可以控制音频播放速度;
  7. 可以通过NaturalDuration属性获取音频的长度,并通过Position属性获取当前播放进度;
  8. 可以通过Position属性执行Seek操作。

使用MediaPlayer来播放音频文件如下:

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

MediaPlayer对象一次只能播放一个文件。文件是异步播放的,您也可以调用Close来释放文件。

有关详细信息,请参阅https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.mediaplayer?view=windowsdesktop-7.0

在使用Visual Studio时,您需要首先执行以下步骤:

这样您可以在工具栏中找到MediaPlayer。

如果您还有任何问题,请随时提出。

英文:

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:

控制音频中的声音以多种形式

控制音频中的声音以多种形式

This way you can find MediaPlayer in the toolbar.

If you still have any questions please speak up.

huangapple
  • 本文由 发表于 2023年2月19日 07:25:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497020.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定