最简单的方法在OSX与MONO播放声音 [英] Easiest way to play SOUND with MONO on OSX

查看:193
本文介绍了最简单的方法在OSX与MONO播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用单编写一个简单的定时器应用的OSX10.6。我怎么能起到报警声(它可能是一个WAV / MP3文件或别的什么)?

I am writing a simple timer app for OSX10.6 using Mono. How can I play an alarm sound (it may be a wav/mp3 file or something else)?

我试了几种方法,遗憾的是没有做的工作:

I tried several ways, unfortunately none did work:


  1. NSSound 似乎不受单被支持。

MonoMac.AppKit.NSSound alarm = new MonoMac.AppKit.NSSound("alarm.wav");
alarm.Play();


  • 使用声音播放没有工作之一:

    System.Media.SoundPlayer player = new System.Media.SoundPlayer("alarm.wav");
    player.PlaySync();
    


  • 我可以通过打开的System.Diagnostics.Process 播放声音,然后使用OSX命令行命令 afplay 。不幸的是,这个命令在一个新的终端窗口,这是一个GUI应用程序很烦人打开。

  • I was able to play a sound by opening a System.Diagnostics.Process and then use the OSX command line command afplay. Unfortunately, this command opens in a new terminal window, which is quite annoying in a GUI app.

    我知道有在单CoreAudio的绑定。但我还没有想出如何利用它们来播放声音。

    I realize there are CoreAudio bindings in Mono. But I have not figured out how to use them to play a sound.

    推荐答案

    您使用了错误NSSound构造就是一切。

    You're using the wrong NSSound constructor is all.

    新MonoMac.AppKit.NSSound(alarm.wav) 期待一个的NSData (隐式地从投串),您要使用的 新NSSound(字符串,布尔) 。也许想通过第二个参数。

    new MonoMac.AppKit.NSSound("alarm.wav") expects an NSData (implicitly cast from string), you want to use new NSSound(string, bool). Probably want to pass false for the second parameter.

    我扔在一起quicky测试项目(基于默认MonoMac项目)确认本作品:

    I threw together a quicky test project (based on the default MonoMac project) to confirm this works:

    public override void FinishedLaunching (NSObject notification)
    {
      mainWindowController = new MainWindowController ();
      mainWindowController.Window.MakeKeyAndOrderFront (this);
    
      // Only lines added
      var sound = new NSSound("/Users/kevinmontrose/Desktop/bear_growl_y.wav", byRef: false);
    
      sound.Play();
    }
    

    这篇关于最简单的方法在OSX与MONO播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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