Randomise Playlist

To discuss development of addons / skins / customization of MediaMonkey.

Moderators: jiri, drakinite, Addon Administrators

mmain
Posts: 17
Joined: Sun Jan 01, 2012 8:21 am

Randomise Playlist

Post by mmain »

With MMW4 I had a script which allowed me to randomise a playlist. Is there anyway I can recreate a similar script under MMW5?
drakinite
Posts: 471
Joined: Tue May 12, 2020 10:06 am
Contact:

Re: Randomise Playlist

Post by drakinite »

When you say randomize playlist, do you mean playing it in shuffle mode? Or to actually rearrange the order of tracks on the playlist?
Image
Student electrical-computer engineer, web programmer, part-time MediaMonkey developer, full-time MediaMonkey enthusiast
mmain
Posts: 17
Joined: Sun Jan 01, 2012 8:21 am

Re: Randomise Playlist

Post by mmain »

Not shuffle, randomise order of playlist. I used to have a script in MMW4 which did this, but scripting seems to have gone from MMW5
Lowlander
Posts: 49819
Joined: Sat Sep 06, 2003 5:53 pm
Location: MediaMonkey 5

Re: Randomise Playlist

Post by Lowlander »

Scripting is still possible with MediaMonkey. However MediaMonkey 4 scripts are incompatible as MediaMonkey was rewritten to be cross-platform compatible instead of just a Windows application. It will take time till more Addons are created, it did so for MediaMonkey 3/4 too.
Lowlander (MediaMonkey user since 2003)
Andre_H
Posts: 76
Joined: Thu Jan 21, 2021 2:04 pm
Location: Germany

Re: Randomise Playlist

Post by Andre_H »

There's a built-in functionality for shuffle, at least at the "smart playlists": in the filter options, you can define sort-options, and there are some "random song (automatic refresh)" options. (i run the german version, so i can only guess what it's named in english, just have a look).

the refresh/shuffle definitly works on "click on playlist" in MediaMonkey, and from what i see when syncing to my mobile device, there seems to bee some kind of automatic/timed refresh/shuffle, but i'm not sure how that works in detail. maybe one of the devs could explain?
MMW 5.0.1.2412 on Windows 2016 Std.
MMA Pro on Android 10 Phone and Android 11 Tab

MP3Tag, MP3Diags, IrfanView
Ludek
Posts: 4022
Joined: Fri Mar 09, 2007 9:00 am

Re: Randomise Playlist

Post by Ludek »

As for using 'Playlist is' condition in an auto-playlist and using [x] Limit ... selected by [Random (refresh-all)]
Yes, this can be used as workaround without a need for scripting.

But I guess that user "mmmain" is asking for a script that randomizes static playlist directly (without a need using auto-playlists) ?

Then I think that code like this should work:

Code: Select all

var tracks = playlist.getTracklist();
tracks.whenLoaded().then(()=> {
  tracks.randomize();
  playlist.reorderAsync(tracks);
});
The true is that neither the randomize and reorderAsync are documented atm. I'll update the docs.
mmain
Posts: 17
Joined: Sun Jan 01, 2012 8:21 am

Re: Randomise Playlist

Post by mmain »

Is there some guidance on how to add this as a script?
drakinite
Posts: 471
Joined: Tue May 12, 2020 10:06 am
Contact:

Re: Randomise Playlist

Post by drakinite »

Yep! You can find a step by step guide on how to create an addon/script here: https://www.mediamonkey.com/wiki/Gettin ... d_(Addons)

One thing you'd have to decide is where exactly you want the "randomise playlist" action to be accessible.
If you want it to be accessible in that little context menu, here are the steps I'd take to figure out how to make the script into an addon:

1) To find out exactly where in the code is responsible for that context menu, I can do a text search (in the installation directory, through all the .js files) for one of the items: in this case, "Remove duplicates".
2) I then find that the text "Remove duplicates" is defined in actions.playlistRemoveDuplicates. Good! Now do a text search for playlistRemoveDuplicates, to see where that action is called.
3) Bingo! There's one inside playlistHeader.js, where it creates this.btnMenu.controlClass.menuArray. That's what we're looking for!

So what we need to do now is:
- Create a new "action", similar to actions.playlistRemoveDuplicates, which randomizes the playlist
- Add that "action" to the menu defined in playlistHeader.js.

4) So to create a new action, let's create a new file in our project folder (where you placed info.json; take a look at the Hello World example in the getting started guide) named actions_add.js. We then define, let's call it , "playlistRandomise".

To start with, let's define actions.playlistRandomise:

Code: Select all

actions.playlistRandomise = {  }
In fact, actions.playlistRemoveDuplicates is a very good baseline, and we can keep most of its properties. We just need to change its title, and preferably its icon. Let's go with shuffle. Also, the contents of its execute function is pretty close to what we need already. It gets the associated playlist object, and even loads its tracklist. So let's put Ludek's code inside the whenLoaded callback, careful to rename the variables so that we don't encounter an error:

Code: Select all

actions.playlistRandomise = {
    title: function () {
        return 'Randomise Playlist'
    },
    icon: 'shuffle',
    visible: function () {
        if (!window.uitools.getCanEdit())
            return false;
        else {
            var pl = resolveToValue(this.boundObject);
            return (pl.parent != undefined && !pl.isAutoPlaylist); // to exclude root playlists node and auto-playlists
        }
    },
    execute: function () {
        var playlist = resolveToValue(this.boundObject);
        var tracks = playlist.getTracklist();
        tracks.whenLoaded().then(() => {
            tracks.randomize();
            playlist.reorderAsync(tracks);
        });
    }
};
5) Now, we need to add it to the playlist header menu that we talked about before. To do this, we'll use the Override method as discussed here: https://www.mediamonkey.com/wiki/Import ... )#Override So here, we will create playlistHeader_add.js (within a "controls" subfolder).

To start with, this is the format you need to do to override the PlaylistHeader's _initButtons method, inside controls/playlistHeader_add.js:

Code: Select all

PlaylistHeader.prototype.override({
    _initButtons: function ($super) {
        $super();
        // now we do our own stuff
    }
});
If we want our item to be ordered directly after "Remove duplicates", we'll want to order it correctly. "Remove Duplicates" has a grouporder of 10 and an order of 20, and "Pin it" has a grouporder of 10 and an order of 30. So we'll want our order to be between those two; so we'll do 25.

Now, as we were using playlistRemoveDuplicates as a baseline, you can see that it uses a function called bindAction. This is so that the actions can figure out which playlist it's supposed to be editing (and that's where the "resolveToValue(this.boundObject)" comes in to play). So let's do the same for our new menu item:

Code: Select all

PlaylistHeader.prototype.override({
    _initButtons: function ($super) {
        $super();
        var _this = this;
        this.btnMenu.controlClass.menuArray.push({
            action: bindAction(window.actions.playlistRandomise, () => {
                return _this._playlist;
            }),
            order: 25,
            grouporder: 10
        });
    }
});
6) Now, pack your MMIP, install it, and test it! https://lambda.sx/IdwS.mp4
Image
Student electrical-computer engineer, web programmer, part-time MediaMonkey developer, full-time MediaMonkey enthusiast
mmain
Posts: 17
Joined: Sun Jan 01, 2012 8:21 am

Re: Randomise Playlist

Post by mmain »

Thanks for your support and guidance, works a treat :D
drakinite
Posts: 471
Joined: Tue May 12, 2020 10:06 am
Contact:

Re: Randomise Playlist

Post by drakinite »

You're welcome - that's great to hear :)
Image
Student electrical-computer engineer, web programmer, part-time MediaMonkey developer, full-time MediaMonkey enthusiast
Post Reply