Detailed guide for creating a background 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)

11 Likes

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

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

1 Like

hello why it cant play music? nothing happen

Did you update the extension?

1 Like

oh thank you Kumaraswamy

1 Like

Hi, can we use the URL of an external music file instead of an internal music file? @Kumaraswamy

Sure, why not, try it out, just put in the URL in the Source block.

Actually, I used the broadcast address of a radio station with internet broadcasting, but I couldn't get any sound.

Can you show us that? Also perhaps can you provide the address of radio station so that I can have a look?

Url: http://api-tv5.yayin.com.tr:8002/mp3

Thanks a lot

You are forgetting a very small thing.

image
image

1 Like

:man_facepalming: thanks a lot again :blush:

1 Like

Hello, I'm having some problems with getting this, or rather, anything of background tasks to work. My main problem is this-I'm using your .aia file you gave, for the music player. I have an A15 phone, android 15, and it doesn't work whether as it's own app or as the companion. The best I get is that the companion keeps stopping or that the app crashes so often to put it in deep sleep to preserve the phone

itoo projects generally do not work in the companion
also this example was writteh in October 2023. Nowadays we have to target sdk 35 so the example project needs to be adjusted.
.
There have been changes concerning foreground services, see here https://developer.android.com/develop/background-work/services/fgs/service-types which makes it more complex.

The latest version of Itoo 4.4.2 already provides the new property foreground service type, but this update is not sufficient (as I found out in another project) because depending on the foreground service type different settings have to be done in the manifest and this is not possible dynamically for extensions

for example for a background music player you would set foreground service type to mediaPlaypack and the extension has to modify the manifest accordingly

unknown

let's wait for feedback from @Kumaraswamy
Taifun

Hi @V0id, the example listed here needs to be updated with the latest Itoo extension and it's changed behavior.


Based on my earlier experiences, setting the foreground flag dynamically should suffice and should work equally in behavior.

Could you please share me a sample project that I could test? If setting the flags dynamically isn't enough, maybe we could create some manifest generator site for Itoo...

PS:
After a few tests, it really seems to be the issue, we cannot just pass the foregroundServiceType flag dynamically at runtime. An appropriate version needs to be declared in the Manifiest as well.

2 Likes

Don't know if you were talking to taifun or me, and especially now that you finished testing I'm still not sure whether to send this, but I figured I'd err on the side of caution.
Emily.aia (4.4 MB) I essentially need two things-push notifications, and a music player, both of which need to be running when the app is closed. I'm trying to create an everything in one kind of app.

Check here for Push Notifications with Itoo:

Feel free to correct me if I'm wrong, but doesn't those examples only push a notification when something is changed? I'm looking for something that could fire a push notification even when the message remains the exact same, and preferably free to use.