Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!
Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.
Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!
[Solved] Lazarus - MacOS - How to play a sound file in Lazarus
Delphi, Lazarus, Free Pascal
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter
May 23, 2018 4:06 AM
Just a quick example of how you can play a soundfile on MacOS X using Lazarus Pascal:
unit Unit1;
{$mode objfpc}{$H+}
interface
{$linkframework AudioToolbox}
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, MacOSAll, CocoaInt;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
public
end;
TSystemSoundID = UInt32;
pSystemSoundID = ^TSystemSoundID;
procedure AudioServicesPlayAlertSound (inSystemSoundID: TSystemSoundID); external name '_AudioServicesPlayAlertSound';
function AudioServicesCreateSystemSoundID(inFileURL: CFURLRef; outSystemSoundID: pSystemSoundID):OSStatus; external name '_AudioServicesCreateSystemSoundID';
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
var newSoundID:TSystemSoundID;
filePathURL: CFURLRef;
filePathCFStringRef: CFStringRef;
filePathStr: Str255; // = string
begin
// Standard sound:
// AudioServicesPlayAlertSound(newSoundID);
// Play sound from file
filePathStr := '/Users/hans/Dropbox/Projects/Lazarus Projects/MacOS Sound Test/project1.app/Contents/Resources/notify.wav';
filePathCFStringRef := CFStringCreateWithPascalString(kCFAllocatorDefault, filePathStr, CFStringGetSystemEncoding);
filePathURL := CFURLCreateWithFileSystemPath(kCFAllocatorDefault, filePathCFStringRef, kCFURLPOSIXPathStyle, false);
AudioServicesCreateSystemSoundID(filePathURL, @newSoundID);
AudioServicesPlayAlertSound(newSoundID);
end;
end.
Please note that you will need to get a sound file yourself of course and ... I'm no expert so things may be done more efficient or more correct.