如何在 Windows 窗体应用程序中依次播放两种声音?

发布于 2024-12-10 08:26:12 字数 126 浏览 2 评论 0原文

我想依次播放两种声音以响应按钮单击。当第一个声音结束时,第二个声音应该开始播放。

我的问题是,每次单击按钮时,这两个声音都是不同的,并且我不知道它们的长度以便使用 Thread.Sleep。但我不希望这些声音相互重叠播放。

I want to play two sounds one after the other in reaction to a button click. When the first sound finishes, the second sound should start playing.

My problem is that every time the button is clicked these two sounds are different and I don't know their lengths in order to use Thread.Sleep. But I don't want these sounds to play on top of each other.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

┼── 2024-12-17 08:26:12

听起来好像您正在使用 SoundPlayer 类的 PlaySync 方法..首先将其添加到顶部:

using System.Media;

然后编写这样的代码:

SoundPlayer player = new SoundPlayer(@"path to first media file");
player.PlaySync();

player = new SoundPlayer(@"path to second media file");
player.PlaySync();

该类自 .NET 2.0 起可用,因此您应该拥有它。

Sounds like you're after the PlaySync method of SoundPlayer class.. first add this on top:

using System.Media;

Then have such code:

SoundPlayer player = new SoundPlayer(@"path to first media file");
player.PlaySync();

player = new SoundPlayer(@"path to second media file");
player.PlaySync();

This class is available since .NET 2.0 so you should have it.

九公里浅绿 2024-12-17 08:26:12

MediaPlayer 有 MediaEnded 事件。在事件处理程序中,只需启动新媒体,它们就会连续播放。

protected System.Windows.Media.MediaPlayer pl = new MediaPlayer();

public void StartPlayback(){
  pl.Open(new Uri(@"/Path/to/media/file.wav"));
  pl.MediaEnded += PlayNext;
  pl.Play();
  }

private void PlayNext(object sender, EventArgs e){
  pl.Open(new Uri(@"/Path/to/media/file.wav"));
  pl.Play();
  }

The MediaPlayer has MediaEnded event. In the event handler, just start the new media and they should play back to back.

protected System.Windows.Media.MediaPlayer pl = new MediaPlayer();

public void StartPlayback(){
  pl.Open(new Uri(@"/Path/to/media/file.wav"));
  pl.MediaEnded += PlayNext;
  pl.Play();
  }

private void PlayNext(object sender, EventArgs e){
  pl.Open(new Uri(@"/Path/to/media/file.wav"));
  pl.Play();
  }
还在原地等你 2024-12-17 08:26:12

在 Shadow Wizards 的例子中,没有必要每次都创建一个新的声音播放器。
这也有效:

player.SoundLocation = "path/to/media";
player.PlaySync();
player.SoundLocation = "path/to/media2";
player.PlaySync();

In Shadow Wizards example, it's not necessary to make a new soundplayer each time.
This also works:

player.SoundLocation = "path/to/media";
player.PlaySync();
player.SoundLocation = "path/to/media2";
player.PlaySync();
青丝拂面 2024-12-17 08:26:12

使用 NAudio 的示例

private List<string> wavlist = new List<string>();
wavlist.Add("c:\\1.wav");
wavlist.Add("c:\\2.wav");
foreach(string file  in wavlist)
{
          AudioFileReader audio = new AudioFileReader(file);
          audio.Volume = 1;
          IWavePlayer player = new WaveOut(WaveCallbackInfo.FunctionCallback());
          player.Init(audio);
          player.Play();
          System.Threading.Thread.Sleep(audio.TotalTime);
          player.Stop();
          player.Dispose();
          audio.Dispose();
          player = null;
          audio = null;
 }

Example Using NAudio

private List<string> wavlist = new List<string>();
wavlist.Add("c:\\1.wav");
wavlist.Add("c:\\2.wav");
foreach(string file  in wavlist)
{
          AudioFileReader audio = new AudioFileReader(file);
          audio.Volume = 1;
          IWavePlayer player = new WaveOut(WaveCallbackInfo.FunctionCallback());
          player.Init(audio);
          player.Play();
          System.Threading.Thread.Sleep(audio.TotalTime);
          player.Stop();
          player.Dispose();
          audio.Dispose();
          player = null;
          audio = null;
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文