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:
-
NSSound seems to be not supported by Mono yet.
MonoMac.AppKit.NSSound alarm = new MonoMac.AppKit.NSSound("alarm.wav"); alarm.Play(); -
Using a
SoundPlayerdidn’t work either:System.Media.SoundPlayer player = new System.Media.SoundPlayer("alarm.wav"); player.PlaySync(); -
I was able to play a sound by opening a
System.Diagnostics.Processand then use the OSX command line commandafplay. Unfortunately, this command opens in a new terminal window, which is quite annoying in a GUI app.
I realize there are CoreAudio bindings in Mono. But I have not figured out how to use them to play a sound.
You’re using the wrong NSSound constructor is all.
new MonoMac.AppKit.NSSound("alarm.wav")expects anNSData(implicitly cast fromstring), you want to usenew NSSound(string, bool). Probably want to passfalsefor the second parameter.I threw together a quicky test project (based on the default MonoMac project) to confirm this works: