Detailed guide for creating a music player with #Itoo

Music Player

This guide showcases the use of #Itoo for creating a foreground music player application

Blocks Summary

Above is the summary of the project, the blocks inside the border are of the foreground service, i.e. they run independently (closing/reopening app would not have any affect on them)

Explanation

Component 2(1)

We can categorize services into two types: background (invisible to the user) and foreground (user-visible).

We've chosen to use a foreground service. This is because it allows us to keep certain parts of the application running while the user interacts with and controls the music.

CreateProcess block when called, would initiate a foreground service and call the procedure. Now, closing the application won't have any affect on the procedure (it'll keep running).


The above blocks shows how the foreground service is initiated. We first store the music name that is to be played using StoreProperty (Itoo's special storage) then initiate the service using CreateProcess


image

Since we're making a music player app, we need to allow the user perform basic actions like pause/resume/stop, but how would the foreground service know when to perform these actions?

Since the normal application session and a foreground service is different over here, we need to find a way to communicate b/w services.

Here we use the Broadcast messaging to solve the problem, a message could be sent from the app session into foreground using the Broadcast block and then the service can listen to the message using the RegisterBroadcast block.

Register broadcast block would listen to an event name and call the procedure mentioned.


We had mentioned bgMusic in the CreateProcess, so it'll run in the background.
There are a few constraints we have to follow:

  • We cannot access any UI elements
  • We cannot access/modify any global variable

Note that this procedure has an argument "x", its important to have it for it to work.

Here is how we use the RegisterBroadcast, we first create a list of pairs, i.e event names corresponding to their procedure names. We loop over it and register all of them.

Then we retrieve the previously stored property (music name) to play the music.

Itoo's StoreProperty and FetchProperty are nothing but TinyDB but for foreground/background services.


After registering the event names, when a message is sent, we would receive them to these procedures.

Note that all procedures here have an "x" argument, any data sent through the Broadcast block would appear here.


These are the control blocks above who send messages like pause/resume/stop to the foreground session using the Broadcast block.

You would notice that the names of these blocks are same as the event names we had registered before in the bgMusic block.


After compiling all the things we get a music player application that can run in the background even if the app is closed :)

Let me know how it was, any suggestions or queries are welcomed :slight_smile:

ItooMusic.aia (4.2 MB)

9 Likes

A post was merged into an existing topic: [FREE] Background Tasks: Itoo

:+1: No idea how I missed that. Thanks.

1 Like