英文:
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.
- 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.
- 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.
- 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.
- 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具有以下特点:
- 可以同时播放多个声音(创建多个MediaPlayer对象);
- 您可以调整音量(Volume属性);
- 您可以使用Play、Pause、Stop等方法进行控制;
- 您可以将IsMuted属性设置为True以实现静音;
- 您可以使用Balance属性调整左右扬声器的平衡;
- 通过SpeedRatio属性可以控制音频播放速度;
- 可以通过NaturalDuration属性获取音频的长度,并通过Position属性获取当前播放进度;
- 可以通过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:
- Multiple sounds can be played at the same time (create multiple MediaPlayer objects);
- You can adjust the volume (Volume property);
- You can use Play, Pause, Stop and other methods to control;
- You can set the IsMuted property to True to achieve mute;
- You can use the Balance property to adjust the balance of the left and right speakers;
- The speed of audio playback can be controlled through the SpeedRatio property;
- The length of the audio can be obtained through the NaturalDuration property, and the current playback progress can be obtained through the Position property;
- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论