Page 2 of 3

Re: Sending Player info to a window

Posted: Mon Jul 04, 2022 4:47 am
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

Re: Sending Player info to a window

Posted: Mon Jul 04, 2022 2:39 pm
by drakinite
Haven't tested to make sure, but both should fire with nowPlayingModified: https://www.mediamonkey.com/docs/api/fi ... ngModified

Re: Sending Player info to a window

Posted: Mon Jul 04, 2022 3:17 pm
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

Re: Sending Player info to a window

Posted: Thu Jul 21, 2022 8:02 am
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

Re: Sending Player info to a window

Posted: Thu Jul 21, 2022 11:14 am
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

Re: Sending Player info to a window

Posted: Thu Jul 21, 2022 2:00 pm
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)

Re: Sending Player info to a window

Posted: Thu Jul 21, 2022 2:14 pm
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!

Re: Sending Player info to a window

Posted: Thu Jul 21, 2022 2:33 pm
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');
    }
})

Re: Sending Player info to a window

Posted: Thu Jul 21, 2022 3:08 pm
by marceros
Thank you!

I think I need to do some homework.

Re: Sending Player info to a window

Posted: Fri Aug 12, 2022 1:01 pm
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!

Re: Sending Player info to a window

Posted: Wed Aug 17, 2022 3:40 am
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

Re: Sending Player info to a window

Posted: Sat Aug 20, 2022 2:19 pm
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...

Re: Sending Player info to a window

Posted: Sat Aug 20, 2022 2:36 pm
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

Re: Sending Player info to a window

Posted: Wed Sep 21, 2022 5:43 pm
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

Re: Sending Player info to a window

Posted: Thu Sep 22, 2022 10:18 am
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.