AVMidiPlayer not working - Might be one for you, Trev ;-) lazarus / 33-macOS - Mac OS X ---------------------------------------------------------------------- carl_caulkett · 2021-11-07 21:48 As an experiment, I'm trying to get the example at https://wiki.freepascal.org/macOS_MIDI_Player to run. I've managed to build a runnable piece of code, but something is amis. The code is failing at the point just after the AVMIDIPlayer external class is "created" and is then tested to see if it is assigned. 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       myMidiPlayer := AVMidiPlayer.alloc.initWithContentsOfURL_error(url, Nil, @err);       // Save file duration      fileDuration := myMidiPlayer.duration;       if Assigned(myMidiPlayer) then  // *** FAILS THIS TEST ***        begin          myMidiPlayer.setRate(myRate);          myMidiPlayer.prepareToPlay;        Form1.SecondsLabel.Caption := '0 of ' + FormatFloat('#', fileDuration)             + ' seconds';          Form1.ProgressBar.Max:= Trunc(fileDuration);          myMidiPlayer.play(@myCompletionHandler);        end      else        // Use the Applications > Utilities > Console application to find error messages        NSLog(NSStr('Error in procedure PlayMidi(): %@'), err);  I've attached a zip file with the entire source. Any ideas what the problem is? [Edited to add code tags.] ---------------------------------------------------------------------- carl_caulkett · 2021-11-07 22:42 I'm wondering if there is a problem with the filename of the MIDI file in question. This is the start of the PlayMidi function... 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 PlayMidi(midiFileName : NSString);var  path: NSString;  url : NSURL;  err : NSError;begin  // Do nothing if already playing a midi file  if(myMidiPlayer.IsPlaying) then    exit;   // If player has not been paused  if(filePOS = 0) then    begin      // Path to your application bundle's resource directory      // with the midi filename appended    path := NSBundle.mainBundle.resourcePath.stringByAppendingPathCompon ent(midiFileName);      url  := NSURL.fileURLWithPath(path); I thought I would try to monitor the state of path and url, because url is used in the creation of the AVMidiPlayer object. 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       if Form1.PathLabel.Caption='PathLabel' then;         Form1.PathLabel.Caption:=path;      if Form1.URLLabel.Caption='URLLabel' then;         Form1.URLLabel.Caption:=url; This, of course, doesn't work because path is an NSString and url is an NSUrl. How do I convert them to TLabel.Caption-friendly strings? [Edited to add code tags.] ---------------------------------------------------------------------- trev · 2021-11-07 22:54 I just checked my example still compiles and runs in Monterey. I'll take a look at your project later today when I get a moment. ---------------------------------------------------------------------- trev · 2021-11-07 23:00 Quote from: carl_caulkett on November 07, 2021, 11:42:19 pmThis, of course, doesn't work because path is an NSString and url is an NSUrl. How do I convert them to TLabel.Caption-friendly strings? Add CocoaUtils to the Uses clause, then: 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 ShowMessage(NSStringToString(url.absoluteString)); ---------------------------------------------------------------------- carl_caulkett · 2021-11-07 23:02 Thanks Trev! I've made some progress. I changed the code to...       if Form1.PathLabel.Caption='PathLabel' then;          Form1.PathLabel.Caption:=StrPas(path.cString);       if Form1.URLLabel.Caption='URLLabel' then;          Form1.URLLabel.Caption:=StrPas(url.path.cString); This revealed that the program was looking for the code in the depths of the macOS app... Should be able to mangle the MIDI path accordingly. ---------------------------------------------------------------------- carl_caulkett · 2021-11-07 23:18 Cool! We have MIDI!! Revised code attached... Of course the next problem is that the MIDI contains drums, but the app is automatically playing piano. What I would like ideally is for the app to detect whether the MIDI contain melodic or drum content and play accordingly. The purpose of all of this is to find a way around the inability of macOS to support auditioning MIDI samples. It's been this way since Yosemite days, I understand. Now to read I=up about channel 10... 😉 ---------------------------------------------------------------------- trev · 2021-11-07 23:28 I think you will find that you have all of your tracks assigned to midi instrument #1 which just happens to be a grand piano. ---------------------------------------------------------------------- carl_caulkett · 2021-11-07 23:30 Trev, it seems I was using some deprecated code with the use of the cString. I've now converted the code to use your suggestion. Thanks! That CocoaUtils unit looks really handy.