Difference between revisions of "macOS Play Alert Sound"

From Free Pascal wiki
Jump to navigationJump to search
m (Added heading)
m (→‎Playing a sound from a file: Updated code to do error checking :-)
Line 66: Line 66:
 
<syntaxhighlight lang="pascal">
 
<syntaxhighlight lang="pascal">
 
unit Unit1;
 
unit Unit1;
 +
  
 
{$mode objfpc}{$H+}
 
{$mode objfpc}{$H+}
Line 71: Line 72:
  
 
interface
 
interface
 +
  
 
uses
 
uses
Line 78: Line 80:
 
  Controls,
 
  Controls,
 
  StdCtrls,
 
  StdCtrls,
 +
Dialogs,
 
  MacOSAll;
 
  MacOSAll;
  
Line 95: Line 98:
  
 
  end;
 
  end;
 +
  
 
var
 
var
 
  Form1: TForm1;
 
  Form1: TForm1;
 +
  
 
implementation
 
implementation
 +
  
 
{$R *.lfm}
 
{$R *.lfm}
 +
  
 
{ TForm1 }
 
{ TForm1 }
  
 
+
// Sound procedure and function declarations
// Sound procedure declaration
 
 
procedure AudioServicesPlayAlertSound (inSystemSoundID: TSystemSoundID); external name '_AudioServicesPlayAlertSound';
 
procedure AudioServicesPlayAlertSound (inSystemSoundID: TSystemSoundID); external name '_AudioServicesPlayAlertSound';
 
// Sound function declaration
 
 
function AudioServicesCreateSystemSoundID(inFileURL: CFURLRef; outSystemSoundID: pSystemSoundID):OSStatus; external name '_AudioServicesCreateSystemSoundID';
 
function AudioServicesCreateSystemSoundID(inFileURL: CFURLRef; outSystemSoundID: pSystemSoundID):OSStatus; external name '_AudioServicesCreateSystemSoundID';
  
 
// Play sound from file
 
 
procedure TForm1.Button1Click(Sender: TObject);
 
procedure TForm1.Button1Click(Sender: TObject);
 
var
 
var
Line 120: Line 122:
 
   filePathCFStringRef: CFStringRef;
 
   filePathCFStringRef: CFStringRef;
 
   filePathStr: String;
 
   filePathStr: String;
 +
  status: Integer = -1;
 
begin
 
begin
  filePathStr := '/usr/local/share/fpcsrc/fpc-3.0.4/packages/libndsfpc/examples/audio/maxmod/basic_sound/audio/Ambulance.wav';
+
// Play sound from file
  filePathCFStringRef := CFStringCreateWithPascalString(kCFAllocatorDefault, filePathStr, CFStringGetSystemEncoding);
+
filePathStr := '/usr/local/share/fpcsrc/fpc-3.0.4/packages/libndsfpc/examples/audio/maxmod/basic_sound/audio/Ambulance';
  filePathURL := CFURLCreateWithFileSystemPath(kCFAllocatorDefault, filePathCFStringRef, kCFURLPOSIXPathStyle, false);
+
filePathCFStringRef := CFStringCreateWithPascalString(kCFAllocatorDefault, filePathStr, CFStringGetSystemEncoding);
 +
filePathURL := CFURLCreateWithFileSystemPath(kCFAllocatorDefault, filePathCFStringRef, kCFURLPOSIXPathStyle, false);
 +
 
 +
status := AudioServicesCreateSystemSoundID(filePathURL, @newSoundID);
  
  AudioServicesCreateSystemSoundID(filePathURL, @newSoundID);
+
if(status = 0) then
   Button1.Caption := IntToStr(newSoundID);
+
   begin
  AudioServicesPlayAlertSound(newSoundID);
+
      Button1.Caption := IntToStr(newSoundID);
 +
      AudioServicesPlayAlertSound(newSoundID);
 +
  end
 +
else
 +
  ShowMessage('Error in AudioServicesCreateSystemSoundID()');
 
end;
 
end;
  
end.
+
end.  
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Revision as of 11:10, 25 December 2019

English (en)

macOSlogo.png

This article applies to macOS only.

See also: Multiplatform Programming Guide

The Apple macOS operating system has many possible alert sounds which you can play to alert your user to various situation that need their attention.

The example application code below shows how to play those alert sounds. In this example the alert sound is being played by choosing a menu item which is just for the purposes of the demonstration.

Note that the choice of 2 for the alert sound to play in this example is purely arbitrary. You should choose the number for the appropriate sound you want to play. Sound number 4096 is particularly interesting for deaf users. Try it :-)

unit unit1;

{$mode objfpc}{$H+}
{$linkframework AudioToolbox}

interface

uses
  Forms,
  Menus;

type

  { TForm1 }

  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    MenuItem1: TMenuItem;
    procedure MenuItemPlaySoundClick(Sender: TObject);

  private

  public

  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

// Sound procedure declaration 
Procedure AudioServicesPlayAlertSound (inSystemSoundID: UInt32)
   external name '_AudioServicesPlayAlertSound';

// Menu item OnClick Event
procedure TForm1.MenuItemPlaySoundClick(Sender: TObject);
begin
  AudioServicesPlayAlertSound(2);  // Use the appropriate value for the sound you want
end;

end.


Playing a sound from a file

Not only can you play the existing operating system alert sounds, you can also play your own .wav sound files. The example application below demonstrates this. The Button1 OnClick event handler plays your sound from the specified file and updates the button caption with the number assigned to the sound.


unit Unit1;


{$mode objfpc}{$H+}
{$linkframework AudioToolbox}

interface


uses
 Classes,
 SysUtils,
 Forms,
 Controls,
 StdCtrls,
 Dialogs,
 MacOSAll;

type

 TSystemSoundID = UInt32;
 pSystemSoundID = ^TSystemSoundID;

 { TForm1 }

 TForm1 = class(TForm)
   Button1: TButton;
   procedure Button1Click(Sender: TObject);
 private

 public

 end;


var
 Form1: TForm1;


implementation


{$R *.lfm}


{ TForm1 }

// Sound procedure and function declarations
procedure AudioServicesPlayAlertSound (inSystemSoundID: TSystemSoundID); external name '_AudioServicesPlayAlertSound';
function AudioServicesCreateSystemSoundID(inFileURL: CFURLRef; outSystemSoundID: pSystemSoundID):OSStatus; external name '_AudioServicesCreateSystemSoundID';

procedure TForm1.Button1Click(Sender: TObject);
var
   newSoundID:TSystemSoundID;
   filePathURL: CFURLRef;
   filePathCFStringRef: CFStringRef;
   filePathStr: String;
   status: Integer = -1;
begin
 // Play sound from file
 filePathStr := '/usr/local/share/fpcsrc/fpc-3.0.4/packages/libndsfpc/examples/audio/maxmod/basic_sound/audio/Ambulance';
 filePathCFStringRef := CFStringCreateWithPascalString(kCFAllocatorDefault, filePathStr, CFStringGetSystemEncoding);
 filePathURL := CFURLCreateWithFileSystemPath(kCFAllocatorDefault, filePathCFStringRef, kCFURLPOSIXPathStyle, false);

 status := AudioServicesCreateSystemSoundID(filePathURL, @newSoundID);

 if(status = 0) then
   begin
      Button1.Caption := IntToStr(newSoundID);
      AudioServicesPlayAlertSound(newSoundID);
   end
 else
   ShowMessage('Error in AudioServicesCreateSystemSoundID()');
end;

end.