Recent

Author Topic: AudioServicesPlayAlertSound  (Read 1897 times)

Gilles

  • New Member
  • *
  • Posts: 37
AudioServicesPlayAlertSound
« on: May 23, 2018, 01:07:52 pm »
Hello

who knows where is function AudioServicesPlayAlertSound () ?
I found it in AudioService.h
But there is no file AudioService.pas

and may I declare it like :

Code: Pascal  [Select][+][-]
  1. procedure AudioServicesPlayAlertSound (inSystemSoundID: SystemSoundID);
  2. external name '_AudioServicesPlayAlertSound';

?

(And also : why do they hate so much Carbon at Apple ?  :'( )
« Last Edit: May 23, 2018, 01:16:32 pm by Gilles »
TurboPascal -> ThinkPascal -> CodeWarrior -> XCode with FPC -> (Well-deserved) Retirement (but not yet, not yet…)

Hansaplast

  • Hero Member
  • *****
  • Posts: 607
  • Tweaking4All.com
    • Tweaking4All
Re: AudioServicesPlayAlertSound
« Reply #1 on: May 23, 2018, 03:10:44 pm »


I have been playing a bit with DiskArbitration.h, and I'm definitely not an expert, but this seems to work:


1) Add the AudioToolbox framework with
Code: Pascal  [Select][+][-]
  1. {$linkframework AudioToolbox}


2) Declare the procedure like you did:
Code: Pascal  [Select][+][-]
  1. procedure AudioServicesPlayAlertSound (inSystemSoundID: TSystemSoundID); external name '_AudioServicesPlayAlertSound';
(note that I added a "T" in front of the SystemSoundID)


3) Declare the sound ID:
Code: Pascal  [Select][+][-]
  1. TSystemSoundID = UInt32;


4) And finally call:
Code: Pascal  [Select][+][-]
  1. AudioServicesPlayAlertSound(1);




Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3.  
  4. {$mode objfpc}{$H+}
  5.  
  6.  
  7. interface
  8. {$linkframework AudioToolbox}
  9.  
  10.  
  11. uses
  12.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, MacOSAll, CocoaInt;
  13.  
  14.  
  15. type
  16.  
  17.  
  18.   { TForm1 }
  19.  
  20.  
  21.   TForm1 = class(TForm)
  22.     Button1: TButton;
  23.     procedure Button1Click(Sender: TObject);
  24.   private
  25.  
  26.  
  27.   public
  28.  
  29.  
  30.   end;
  31.  
  32.  
  33.   TSystemSoundID = UInt32;
  34.  
  35.  
  36.   procedure AudioServicesPlayAlertSound (inSystemSoundID: TSystemSoundID); external name '_AudioServicesPlayAlertSound';
  37. var
  38.   Form1: TForm1;
  39.  
  40.  
  41. implementation
  42.  
  43.  
  44. {$R *.lfm}
  45.  
  46.  
  47. { TForm1 }
  48.  
  49.  
  50. procedure TForm1.Button1Click(Sender: TObject);
  51. begin
  52.   AudioServicesPlayAlertSound(1);
  53.   AudioServicesPlayAlertSound(2);
  54. end;
  55.  
  56.  
  57. end.
  58.  


Hansaplast

  • Hero Member
  • *****
  • Posts: 607
  • Tweaking4All.com
    • Tweaking4All
Re: AudioServicesPlayAlertSound
« Reply #2 on: May 23, 2018, 04:02:46 pm »
Now that you got me to try this, maybe this is helpful as well, it allows playback of a sound file:



Code: Pascal  [Select][+][-]
  1.  
  2. unit Unit1;
  3.  
  4.  
  5. {$mode objfpc}{$H+}
  6.  
  7.  
  8. interface
  9. {$linkframework AudioToolbox}
  10.  
  11.  
  12. uses
  13.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, MacOSAll, CocoaInt;
  14.  
  15.  
  16. type
  17.  
  18.  
  19.   { TForm1 }
  20.  
  21.  
  22.   TForm1 = class(TForm)
  23.     Button1: TButton;
  24.     procedure Button1Click(Sender: TObject);
  25.   private
  26.  
  27.  
  28.   public
  29.  
  30.  
  31.   end;
  32.  
  33.  
  34.   TSystemSoundID = UInt32;
  35.   pSystemSoundID = ^TSystemSoundID;
  36.  
  37.  
  38.   procedure AudioServicesPlayAlertSound (inSystemSoundID: TSystemSoundID); external name '_AudioServicesPlayAlertSound';
  39.   function AudioServicesCreateSystemSoundID(inFileURL: CFURLRef; outSystemSoundID: pSystemSoundID):OSStatus; external name '_AudioServicesCreateSystemSoundID';
  40.  
  41.  
  42. var
  43.   Form1: TForm1;
  44.  
  45.  
  46. implementation
  47.  
  48.  
  49. {$R *.lfm}
  50.  
  51.  
  52. { TForm1 }
  53.  
  54.  
  55. procedure TForm1.Button1Click(Sender: TObject);
  56. var newSoundID:TSystemSoundID;
  57.     filePathURL: CFURLRef;
  58.     filePathCFStringRef: CFStringRef;
  59.     filePathStr: Str255; // = string
  60. begin
  61.   // Standard sound:
  62.   // AudioServicesPlayAlertSound(newSoundID);
  63.  
  64.  
  65.   // Play sound from file
  66.   filePathStr := '/Users/hans/Dropbox/Projects/Lazarus Projects/MacOS Sound Test/project1.app/Contents/Resources/notify.wav';
  67.   filePathCFStringRef := CFStringCreateWithPascalString(kCFAllocatorDefault, filePathStr, CFStringGetSystemEncoding);
  68.   filePathURL := CFURLCreateWithFileSystemPath(kCFAllocatorDefault, filePathCFStringRef, kCFURLPOSIXPathStyle, false);
  69.  
  70.  
  71.   AudioServicesCreateSystemSoundID(filePathURL, @newSoundID);
  72.   AudioServicesPlayAlertSound(newSoundID);
  73. end;
  74.  
  75.  
  76. end.
  77.  


Again the side note that I'm no expert ... but it does seem to work.
Obviously, you'll need to grab a soundfile somewhere.

Gilles

  • New Member
  • *
  • Posts: 37
Re: AudioServicesPlayAlertSound
« Reply #3 on: May 23, 2018, 05:40:59 pm »
great

I love this forum
TurboPascal -> ThinkPascal -> CodeWarrior -> XCode with FPC -> (Well-deserved) Retirement (but not yet, not yet…)

 

TinyPortal © 2005-2018