1

I want to play the keyboard 'click' sound when pressing buttons in my app. How do I access this sound clip with Monotouch? I don't want to pass my own sound using AudioToolbox SystemSound.FromFile(). So far all my searches have led to this solution or Objective-C code using 'AudioServicesCreateSystemSoundID' which I'm having trouble translating to C#.

binncheol
  • 1,393
  • 4
  • 22
  • 38

2 Answers2

2

Well, that's not a 1:1 port from the code in Playing system sound without importing your own, but this should do the work:

var path = NSBundle.FromIdentifier ("com.Apple.UIKit").PathForResource ("Tock", "aiff");
using (var systemSound = new SystemSound (NSUrl.FromFilename (path))) {
    systemSound.PlaySystemSound ();
}

SystemSound is defined in MonoTouch.AudioToolbox. Make sure to also look at MonoTouch Play System Sound and MonoTouch: Playing sound

Community
  • 1
  • 1
Stephane Delcroix
  • 16,134
  • 5
  • 57
  • 85
2

With iOS 7.0 and higher, Stephane Delcroix answer seems not to work anymore...

But you can easily find the path to all the sounds in this nice project: https://github.com/TUNER88/iOSSystemSoundsLibrary

Here is the code I used und iOS 7 (take care, it might not work with the simulator!)

    private const string NotificationSoundPath = @"/System/Library/Audio/UISounds/New/Fanfare.caf";

    public static void TriggerSoundAndViber()
    {
        SystemSound notificationSound = SystemSound.FromFile(NotificationSoundPath);
        notificationSound.AddSystemSoundCompletion(SystemSound.Vibrate.PlaySystemSound);
        notificationSound.PlaySystemSound();
    }

Also the using() construct in the answer above caused trouble in my case... it seems like it released the sound too early, I only can hear it (and even then not complete with viber) with a breakpoint on PlaySystemSound().

Ursin Brunner
  • 2,310
  • 1
  • 24
  • 24