[SOLVED] NSSound Delegate implementation lazarus / 33-macOS - Mac OS X ---------------------------------------------------------------------- trev · 2020-06-07 13:56 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  NSSound = objcclass external (NSObject)  public      ...    procedure setDelegate(newValue: NSObject); message 'setDelegate:';    function delegate: NSSoundDelegateProtocol; message 'delegate';        ...  end; NSSoundDelegateProtocol = objcprotocol external name 'NSSoundDelegate' (NSObjectProtocol)  optional    procedure sound_didFinishPlaying (sound: NSSound; aBool: ObjCBOOL);        message 'sound:didFinishPlaying:';  end; NSSoundFinishedDelegate = objcclass(NSObject)  public    procedure didFinishPlaying(Sound: NSSound; FinishedPlaying: Boolean);       message 'sound:didFinishPlaying:';  end;  implementation var  SoundFinishedDelegate: NSSoundFinishedDelegate; procedure NSSoundFinishedDelegate.didFinishPlaying(Sound: NSSound; FinishedPlaying: Boolean);begin  if(FinishedPlaying) then     ShowMessage('Sound ' + CFStringToStr(CFStringRef(Sound.Name)) + ' finished'); end;                                            ...Initialization  SoundFinishedDelegate := NSSoundFinishedDelegate.alloc.init;  Sound.setDelegate(SoundFinishedDelegate);Finalization  Sound.setDelegate(Nil);  SoundFinishedDelegate.release;end. Where did I go wrong? All my play/pause/resume procedures work, but my didFinishPlaying procedure never triggers when the sound finishes playing ---------------------------------------------------------------------- skalogryz · 2020-06-07 18:07 since you're using initialization and finalization sections, I'm assuming you're trying to use them in the console application? I did a small UI test and it works just fine. (attached). (The path to the sound is hardcoded! you need to change it in the code!) From the nature of the playing sounds, there's always a problem of synchronization of events. I.e. "end of sounds" can always be reported from a separate thread. However, the test shows that "end of sounds" is reported to the main thread. That certainly involves having a running event loops. And such loop typically doesn't exist, if you're writing a console application. ---------------------------------------------------------------------- trev · 2020-06-08 02:50 Thanks for your working example Dmitry. Despite 8 errors(!) during the compilation, it succeeds and the application runs without issue... QuoteCompile Project, Target: project1: Success, Errors: 8 Error: :1052:10: warning: section "__datacoal_nt" is deprecated Error: .section __DATA, __datacoal_nt, coalesced Error: :1052:10: note: change section name to "__data" Error: .section __DATA, __datacoal_nt, coalesced Error: :1092:10: warning: section "__datacoal_nt" is deprecated Error: .section __DATA, __datacoal_nt, coalesced Error: :1092:10: note: change section name to "__data" Error: .section __DATA, __datacoal_nt, coalesced It was in fact a GUI application, not a console application. The initialisation/finalization clauses seemed the way to go, but obviously not! I've now modified it along the lines of your example and... it works! ---------------------------------------------------------------------- skalogryz · 2020-06-08 11:07 Quote from: trev on June 08, 2020, 04:50:31 amIt was in fact a GUI application, not a console application. here's another thing to try. In your initial example, class NSSoundFinishedDelegate does implement the method 'sound:didFinishPlaying:', but doesn't implement the protocol. (even though the protocol consists of a single method). Try your original code, but declare the delegate as implementing the protocol: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i NSSoundFinishedDelegate = objcclass(NSObject, NSSoundDelegateProtocol)  public    procedure sound_didFinishPlaying(Sound: NSSound; FinishedPlaying: Boolean);   end; ---------------------------------------------------------------------- trev · 2020-06-08 12:27 That works if I move Sound.setDelegate(SoundFinishedDelegate); from the Initialisation clause to the play procedure. ---------------------------------------------------------------------- skalogryz · 2020-06-08 12:56 Quote from: trev on June 08, 2020, 02:27:32 pmThat works if I move Sound.setDelegate(SoundFinishedDelegate); from the Initialisation clause to the play procedure. when does Sound.initWithXXX occurs? before setting the delegate of after? ---------------------------------------------------------------------- trev · 2020-06-08 13:34 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.PlayButtonClick(Sender: TObject);var  audioFilePath: NSString;  status: Boolean;begin  audioFilePath := NSStringUTF8('/Applications/iP hoto.app/Contents/Resources/Music/Barbeque Blues.m4a');  Sound := NSSound.alloc.initWithContentsOfFile_byReference(audioFilePath, True); // File system file   Sound.setDelegate(SoundFinishedDelegate);   status := Sound.play;  if(status) then    //  else    ShowMessage('Not OK');end; Before ... I just tried after and that doesn't trigger the delegate. Above code works.