In 2022, Delphi Rapid Application Development is still the most efficient. Implementing an Apple Music API Client and more…

In 2022, Delphi Rapid Application Development is still the most efficient. Implementing an Apple Music API Client and more…

I have been a Delphi developer since 1996. At that time, Borland Delphi was the fastest, most efficient way to build Windows 16-Bit and 32-Bit applications. Today, it still is!

Yes, it really is. Here’s a very precise example for a task I had to deal with in recent days.

Music API?

I wanted to access the Apple Music API which is now no longer restricted to Apple devices. You can use the Web services to get all sorts of information about artists and their creative works. Included right now are music albums, playlists, and music videos. The only sore point is that you are not able to authenticate as an Apple Music subscriber using this API – yet. That means any personal information like your playlists and library is not accessible. I am sure it is a “yet” as Apple does provide code samples for JavaScript and Android. I think it will only be a matter of time until you can use your Apple Music subscription with this API to query your personal library and play full music tracks.

The non-subscriber API provides access to previews of all audio tracks, though, which makes it a great resource to feed your own personal music database, look at album covers, etc.

That’s right, album covers and artist pictures can be accessed!

Let’s get started!

So, let’s connect to this fantastic API to lookup information:

  1. Looking at the documentation the biggest roadblock at first is that the API requires a signed JWT. From TMS XData I knew that there is an active JWT project called Delphi JOSE JWT on GitHub run by Paolo Rossi: https://github.com/paolo-rossi/delphi-jose-jwt. A few lines of code, done! Looking up and finding the information in your Apple Developer Profile is actually more difficult than writing the code.
  2. Processing JSON. Delphi comes with great support for JSON out of the box as my recent How it Works with Holger episodes revealed. However, looking at GitHub, you can find an even easier way to process JSON. One option is to use Paolo Rossi’s Neon project https://github.com/paolo-rossi/delphi-neon or the Neslib JSON library . I decided to go with the latter as it allows fast navigation of JSON content: https://github.com/neslib/Neslib.Json
  3. Downloading image and audio information in the background. End-users won’t accept long delays when downloading images and audio blocking their user experience. Delphi offers an easy way to circumvent this issue using the TTask class from System.Threading. However, it is still a challenge if you do not abide by some important multi-threading rules.

Downloading and playing audio

I will present item number 3 in detail. The Apple Music API allows us to download audio previews. These are encoded in MPEG files. The JSON retrieved from the Web service contains a download link for these audio files. As said, we can download these files pretty quickly using TMS Sparkle as an HTTP client and TTask for the background process. Here’s an incomplete snippet of the implementation that also loads the audio into a media player control using the TMS Sparkle HTTP client:

// get the current track
LTrack := dsTracks.Current<TTrack>;
LUrl := LTrack.UrlPreview;

// download audio in background
TTask.Run(
    procedure
    var
      LClient: THttpClient;
      LRes: THttpResponse;
      LFn: String;

    begin
      LRes := nil;
      LClient := THttpClient.Create;
      try
        LRes := LClient.Get( LUrl );

        if LRes.StatusCode = 200 then
        begin
          try
            // create temporary file and assign to player 
            if LGuard.IsDismantled = False then
            begin
              LFn := TPath.GetTempFileName + 
                          TPath.GetExtension( LTrack.UrlPreview );

              TFile.WriteAllBytes( LFn, LRes.ContentAsBytes );
              // code: update player in synchronized thread or queue
            end;
          finally
            LGuard := nil;
          end;
        end;

      finally
        LRes.Free;
        LClient.Free;
      end;
    end
  );

Wow! That is a lot of code! And I even left out the tricky part to load the audio into the player. Also, the Delphi media player control only supports loading from files in the VCL (different in FireMonkey). So, in addition to creating these temporary files, we also need to clean them up. A lot of work for a very simple process.

Thankfully, TMS recently introduced a new component library that uses Web technology and makes them accessible from Delphi! The WX Component Pack includes a dedicated audio player:

With that control, we can reduce the above code to this:

  LTrack := dsTracks.Current<TTrack>;
  LUrl := LTrack.UrlPreview;

  Player.AutoPlay := True;
  Player.URL := LUrl;

This even includes the assignment to the control as the control handles the non-blocking download in the background.

Cover Art and other images

Handling the images is just as easy as handling the download for audio. TMS FNC Cloud Pack contains an image control that just needs a URL and it will handle the download for you. Of course, your user interface will never be blocked either.

So, downloading images from the Internet and displaying them in an image control can be drilled down to a single line of code:

CoverAlbum.URL := LUrlCover;

One tool for all the platforms

I think these are two pretty convincing examples that Delphi and its component infrastructure provide true Rapid Application Development with a robust object oriented language. Best of all, today, it not only generates an executable for Windows. With frameworks like FireMonkey, TMS FNC, and TMS WEB Core, Delphi can be used for development for all important platforms be it desktop or mobile. You can provide a solution for Microsoft Windows, Apple macOS, Linux, and mobile platforms Apple iOS and Android.

There’s more!

For more Delphi goodness with Web services, have a look at my books (here) and my 14-hour brand-new video course about Web service development with Delphi and TMS XData. Use this link to get 20% discount!

More to come!

Tags: , , , , , , ,
One comment on “In 2022, Delphi Rapid Application Development is still the most efficient. Implementing an Apple Music API Client and more…
  1. Martin Pelletier says:

    Very cool.

    I did use Delphi a long time ago, I should have stayed with Delphi. I drank the C# coolaid and now I regret it. I am done with Microsoft Devs tools.

    So now going back to Delphi. Trying to relearn everything. Got all your books. This will help a lot.

    Also watching your How-To videos on Youtube.

    You are helping an old guy to get back to his roots. Thank you 🙂

Partnerships




Top