Sending Player info to a window

To discuss development of addons / skins / customization of MediaMonkey v5 / v2024

Moderators: jiri, drakinite, Addon Administrators

marceros
Posts: 36
Joined: Sun Apr 21, 2013 1:54 am

Re: Sending Player info to a window

Post by marceros »

Hello!

I'm dealing with catching changes in the 'Now playing' list as events so I can change my display accordingly.

The playbackState fires when one track ends and the next one starts playing. The nowPlayingModified fires when I add a new track to the player. My problem is that I cannot find which events will fire when I remove a track or I move a track within the player. Are there events for those situations?

Thank you in advance,
Marcelo
drakinite
Posts: 977
Joined: Tue May 12, 2020 10:06 am
Contact:

Re: Sending Player info to a window

Post by drakinite »

Haven't tested to make sure, but both should fire with nowPlayingModified: https://www.mediamonkey.com/docs/api/fi ... ngModified
Image
Student electrical-computer engineer, web programmer, part-time MediaMonkey developer, full-time MediaMonkey enthusiast
I uploaded many addons to MM's addon page, but not all of those were created by me. "By drakinite, Submitted by drakinite" means I made it on my own time. "By Ventis Media, Inc., Submitted by drakinite" means it may have been made by me or another MediaMonkey developer, so instead of crediting/thanking me, please thank the team. You can still ask me for support on any of our addons.
marceros
Posts: 36
Joined: Sun Apr 21, 2013 1:54 am

Re: Sending Player info to a window

Post by marceros »

drakinite wrote: Mon Jul 04, 2022 2:39 pm Haven't tested to make sure, but both should fire with nowPlayingModified: https://www.mediamonkey.com/docs/api/fi ... ngModified
Thank you for the prompt response. I think it doesn't work. I hope I'm wrong. I'll try again.

Meanwhile, I'm using SetTimeout to send a function that calls itself recursively and checks for changes in the player.

Thanks,
Marcelo
Ludek
Posts: 5028
Joined: Fri Mar 09, 2007 9:00 am

Re: Sending Player info to a window

Post by Ludek »

Hi Marcelo,
thanks for reporting, it really fails to work for re-order and deletions.

To be fixed as https://www.ventismedia.com/mantis/view.php?id=19266

Workaround is to listen 'change' event on app.player.getSongList()
https://www.mediamonkey.com/docs/api/cl ... etSongList
marceros
Posts: 36
Joined: Sun Apr 21, 2013 1:54 am

Re: Sending Player info to a window

Post by marceros »

Thank you for the response!

Can you show me how the workaround line looks?

Maybe something like:

app.listen(app.player.getSongList(), 'change', eventFunction);

Thanks,
Marcelo
drakinite
Posts: 977
Joined: Tue May 12, 2020 10:06 am
Contact:

Re: Sending Player info to a window

Post by drakinite »

Yep. Though it may be a better idea to store the list as a variable first.

Code: Select all

let thisSongList = app.player.getSongList();
app.listen(thisSongList, 'change', function (changeType) {
	// do stuff
});
(I haven't tested that code, but it should work)
Image
Student electrical-computer engineer, web programmer, part-time MediaMonkey developer, full-time MediaMonkey enthusiast
I uploaded many addons to MM's addon page, but not all of those were created by me. "By drakinite, Submitted by drakinite" means I made it on my own time. "By Ventis Media, Inc., Submitted by drakinite" means it may have been made by me or another MediaMonkey developer, so instead of crediting/thanking me, please thank the team. You can still ask me for support on any of our addons.
marceros
Posts: 36
Joined: Sun Apr 21, 2013 1:54 am

Re: Sending Player info to a window

Post by marceros »

Maybe I understand now. So I'm actually checking the change in a list, not an event as I'm used to. Is it right?

And in that case, I have to set a loop where the list variable gets the new list and the "change event" does the rest.

Am I seeing it right?

Thank you!
drakinite
Posts: 977
Joined: Tue May 12, 2020 10:06 am
Contact:

Re: Sending Player info to a window

Post by drakinite »

marceros wrote: Thu Jul 21, 2022 2:14 pm And in that case, I have to set a loop where the list variable gets the new list and the "change event" does the rest.
No, no need for a loop. I was just giving a general coding suggestion. app.listen(app.player.getSongList(), ...) is still valid; but if you want to access thisSongList inside your event handler, you won't have to do app.player.getSongList() multiple times.

Code: Select all

app.listen(app.player.getSongList(), 'change', function (changeType) {
    if (changeType === undefined) {
        console.log('Song list has been rearranged');
    }
})
Image
Student electrical-computer engineer, web programmer, part-time MediaMonkey developer, full-time MediaMonkey enthusiast
I uploaded many addons to MM's addon page, but not all of those were created by me. "By drakinite, Submitted by drakinite" means I made it on my own time. "By Ventis Media, Inc., Submitted by drakinite" means it may have been made by me or another MediaMonkey developer, so instead of crediting/thanking me, please thank the team. You can still ask me for support on any of our addons.
marceros
Posts: 36
Joined: Sun Apr 21, 2013 1:54 am

Re: Sending Player info to a window

Post by marceros »

Thank you!

I think I need to do some homework.
marceros
Posts: 36
Joined: Sun Apr 21, 2013 1:54 am

Re: Sending Player info to a window

Post by marceros »

I figured out how to use the 'change' event on the 'getSongList' method. I'm using the events with the parameter 'delete' and with no parameter and everything works fine besides the fact that it triggers the event several unnecessary times. I assume the triggers will be more accurate when the

But meanwhile, I found another strange behavior with the app.player.playListPos.

Let's say I have 10 songs in the player and the fifth song (playListPos=6) is now playing. Then, I move the sixth song to the place before the playing song so the moved song got the fifth place in the list and the playing song got the sixth place. The playListPos now should show 7 but it still shows 6.

Then I realized that I was reading the playListPos outside the tracklist locked function. Once I moved it into the function, it started working good.

Thank you!
marceros
Posts: 36
Joined: Sun Apr 21, 2013 1:54 am

Re: Sending Player info to a window

Post by marceros »

Hello!

My program looks stable and does the main logics of the job. Before I write the next question/problem, I'd like to explain in a line or two what the program does.

I added a menu in the MM5 main window that opens a second window. Following is the code in the actions_add.js

Code: Select all

actions.openDanceFloorDisplay = {
    title: _('Dance floor display') + '...',
    hotkeyAble: true,
    // icon: 'openDanceFloorDisplay',
    // disabled: uitools.notMediaListSelected,
    visible: window.uitools.getCanEdit,
    execute: async function () {
        var dlg = uitools.openDialog('dlgDanceFloorDisplay', {
			left: 100,
			top: 100,
			width: 500,
			height: 500,
            show: true,
            modal: false,
            title: _('Dance floor display'),
            // tracks: list
        });
    }
}
That window goes to a second screen and displays information (to the people in the dance floor) about the theme that is playing and the following themes. All the logic is done in the JS of the open dialog and that's the part that I'm happy with.

Now I need to control the dialog from outside - from the main MM menu. Let's say that the second screen is different than expected and I have to change some internal parameters that control the text size and the height of the lines. Or I might want to change the text of the first line (title) showing on the second screen.

I guess the question is how I send data to the dialog JS (dlgDanceFloorDisplay.js) and how I run an update function in that JS, everything initiated from a menu in the main MM window.

Thank you in advance,
Marcelo
drakinite
Posts: 977
Joined: Tue May 12, 2020 10:06 am
Contact:

Re: Sending Player info to a window

Post by drakinite »

There are many ways you can do it, but here's an example with custom events. You can pass data through the detail parameter of createNewCustomEvent(). See: https://developer.mozilla.org/en-US/doc ... ustomEvent

Main window:
Image

Dialog window:
Image

Quick explainer on the first two lines: app.dialogs.getMainWindow() returns a SharedWindow object (from our Delphi code), but its _window property gives you access to the regular JS Window object, allowing you to listen to custom events.

Regarding the playListPos shenanigans: I forgot to check that out. Let's see...
Image
Student electrical-computer engineer, web programmer, part-time MediaMonkey developer, full-time MediaMonkey enthusiast
I uploaded many addons to MM's addon page, but not all of those were created by me. "By drakinite, Submitted by drakinite" means I made it on my own time. "By Ventis Media, Inc., Submitted by drakinite" means it may have been made by me or another MediaMonkey developer, so instead of crediting/thanking me, please thank the team. You can still ask me for support on any of our addons.
drakinite
Posts: 977
Joined: Tue May 12, 2020 10:06 am
Contact:

Re: Sending Player info to a window

Post by drakinite »

marceros wrote: Fri Aug 12, 2022 1:01 pm Let's say I have 10 songs in the player and the fifth song (playListPos=6) is now playing. Then, I move the sixth song to the place before the playing song so the moved song got the fifth place in the list and the playing song got the sixth place. The playListPos now should show 7 but it still shows 6.

Then I realized that I was reading the playListPos outside the tracklist locked function. Once I moved it into the function, it started working good.

Thank you!
I assume you mean sixth song (playlistPos=5), since the list positions are zero indexed?

I can reproduce the problem, tracked as https://www.ventismedia.com/mantis/view.php?id=19319
Image
Student electrical-computer engineer, web programmer, part-time MediaMonkey developer, full-time MediaMonkey enthusiast
I uploaded many addons to MM's addon page, but not all of those were created by me. "By drakinite, Submitted by drakinite" means I made it on my own time. "By Ventis Media, Inc., Submitted by drakinite" means it may have been made by me or another MediaMonkey developer, so instead of crediting/thanking me, please thank the team. You can still ask me for support on any of our addons.
marceros
Posts: 36
Joined: Sun Apr 21, 2013 1:54 am

Re: Sending Player info to a window

Post by marceros »

drakinite wrote: Sat Aug 20, 2022 2:19 pm There are many ways you can do it, but here's an example with custom events. You can pass data through the detail parameter of createNewCustomEvent(). See: https://developer.mozilla.org/en-US/doc ... ustomEvent

Main window:
Image

Dialog window:
Image

Quick explainer on the first two lines: app.dialogs.getMainWindow() returns a SharedWindow object (from our Delphi code), but its _window property gives you access to the regular JS Window object, allowing you to listen to custom events.

Regarding the playListPos shenanigans: I forgot to check that out. Let's see...
Thank you drakinite!

I've just started to try your suggestion. I have a question: where should I define the custom event variable and function so it is recognized in the _window property of the "Main window". I guess I mean to ask: where is the "main window" source where I should define the custom event? Is it in the init.js?

Thanks,
Marcelo
drakinite
Posts: 977
Joined: Tue May 12, 2020 10:06 am
Contact:

Re: Sending Player info to a window

Post by drakinite »

Essentially, any script that's NOT running inside a separate dialog window is running inside the main window.

Examples of scripts that run inside the main window:
  • addon's init.js
  • actions.js
  • mainwindow.js
  • templates.js
Examples of scripts that do NOT run inside the main window:
  • addon's config.js
  • Anything inside the "dialogs" folder
Scripts inside the "controls" folder may run either inside the main window OR inside a dialog window, depending on where it's initiated from. For example, Tools -> Equalizer has several Slider controls which exist inside the Equalizer dialog window, but the volume bar at the bottom of the main window exist, as you can probably guess, inside the main window.
Image
Student electrical-computer engineer, web programmer, part-time MediaMonkey developer, full-time MediaMonkey enthusiast
I uploaded many addons to MM's addon page, but not all of those were created by me. "By drakinite, Submitted by drakinite" means I made it on my own time. "By Ventis Media, Inc., Submitted by drakinite" means it may have been made by me or another MediaMonkey developer, so instead of crediting/thanking me, please thank the team. You can still ask me for support on any of our addons.
Post Reply