The easiest way to play SOUND with MONO on OSX

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

I tried several ways, unfortunately, no one worked:

  • NSSound seems to be not yet supported by Mono.

    MonoMac.AppKit.NSSound alarm = new MonoMac.AppKit.NSSound("alarm.wav"); alarm.Play(); 
  • Using SoundPlayer does not work either:

     System.Media.SoundPlayer player = new System.Media.SoundPlayer("alarm.wav"); player.PlaySync(); 
  • I was able to play the sound by opening System.Diagnostics.Process , and then using the OSX afplay command line afplay . Unfortunately, this command opens in a new terminal window, which is quite annoying in a graphical application.

I understand that Mono has CoreAudio bindings. But I did not understand how to use them to play sound.

+4
source share
1 answer

You are using the wrong NSSound constructor.

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

I put together a quick test project (based on the MonoMac project by default) to confirm that 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(); } 
+2
source

All Articles