如何使用System.Media.SoundPlayer异步播放声音文件? [英] How to use System.Media.SoundPlayer to asynchronously play a sound file?

查看:858
本文介绍了如何使用System.Media.SoundPlayer异步播放声音文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个看似简单的问题:

Here's a deceptively simple question:

什么是在Windows窗体异步播放嵌入的.wav资源文件的正确方法?

尝试#1:

var player = new SoundPlayer();
player.Stream = Resources.ResourceManager.GetStream("mySound");
player.Play(); // Note that Play is asynchronous


  • 好:不会阻塞UI线程

  • 缺点:声音播放和嵌入式
    资源流不会立即
    处置。

  • 尝试#2:

    using (var audioMemory = Resources.ResourceManager.GetStream("mySound"))
    {
        using (var player = new SoundPlayer(audioMemory))
        {
            player.Play();
        }
    }
    


    • 好:UI线程没有被阻塞,声音播放和音频内存流将立即处置

    • 坏:竞争条件!播放()是异步的,如果内存音频播放被处置之前完成......咚!运行时异常。

    • 尝试3:

      using (var audioMemory = Resources.ResourceManager.GetStream("mySound"))
      {
          using (var player = new SoundPlayer(audioMemory))
          {
              player.PlaySync();
          }
      }
      


      • 好:播放器和音频流被立即处置

      • 坏:PlaySync阻塞UI线程

      • 尝试#4:

        ThreadPool.QueueUserWorkItem(ignoredState =>
          {
            using (var audioMemory = Resources.ResourceManager.GetStream("mySound"))
            {
                using (var player = new SoundPlayer(audioMemory))
                {
                    player.PlaySync();
                }
            }
          });
        


        • 好:UI不会冻结,播放器和内存流将立即处置

        • 错误:因为这触发的时候,我们可能会耗尽线程池中的线程!见拉里·奥斯特曼的<一个href=\"http://blogs.msdn.com/larryosterman/archive/2009/06/29/what-s-wrong-with-this-$c$c-part-26-the-answer.aspx\">what's错这个code部分26 。

          • Good: UI doesn't freeze, player and memory stream are immediately disposed.
          • Bad: Because this fires often, we may run out of thread pool threads! See Larry Osterman's what's wrong with this code part 26.
          • 这似乎是声音播放应该有一个PlayAsyncCompleted事件。不幸的是,没有这样的事件存在。我缺少的东西吗?什么是在Windows窗体异步播放.wav嵌入式资源的正确方法?

            It seems like SoundPlayer should have a PlayAsyncCompleted event. Unfortunately, no such event exists. Am I missing something? What's the proper way to asynchronously play a .wav embedded resource in Windows Forms?

            推荐答案

            我没有足够的信誉发表评论,所以我就只是回答。

            I don't have enough reputation to comment so I'll just answer.

            如果您的要求来播放声音是看似简单(你只是想打的时候一个WinForm的用户做一些偶尔的声音),那么我会用上面的尝试4。

            If your requirements to play sound are "deceptively simple" (you just want to play the occasional sound when a single winform user does something) then I would use Attempt #4 above.

            拉​​里奥斯特曼的有什么不对的code部分26有他的系统分拆一个新的线程池的线程(播放声音),每个按键。他表示比锤打远上饱和的默认500线程池大小在约15秒打字,但此使用了使用线程池也异步RPC也是同一个客户机/服务器应用程序。实在不是一个看似简单的应用程序。

            Larry Osterman's "what's wrong with this code part 26" has his "system" spin off a new threadpool thread (to play sound) with each keystroke. He indicates than hammering away on it saturated the default 500 thread pool size in about 15 seconds of typing but this was also with a client/server app using async RPC that were also using the threadpool. Really not a "deceptively simple" application.

            如果你想在同一时间排队声音字节每秒(或更快)10秒或秒100S那么它真的不是一个简单应用和排队线程/优先级子系统很可能是为了。

            If you are trying to queue sound bytes every second (or faster) for 10s or 100s of seconds at a time then its really not a "simple application" and a queued threading/priority subsystem would probably be in order.

            这篇关于如何使用System.Media.SoundPlayer异步播放声音文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆