Randomise Playlist

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

Moderators: jiri, drakinite, Addon Administrators

mmain
Posts: 19
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: 965
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
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.
mmain
Posts: 19
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: 56465
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.
Andre_H
Posts: 415
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.4.2690 (non-portable, shared DB & files) on Windows 2016 # only essential addons # my 24/7 media server
- MMW MMW 5.0.4.2690 (non-portable, shared DB & files) on Windows 10 # playing, testing skins & addons # my desktop app
- MMA Pro (2.0.0.1063) on Android 10, 11, 12 Phones & Tabs # WiFi Sync # playing

- MP3Tag, MP3Diags, MP3DirectCut, IrfanView
Ludek
Posts: 4945
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: 19
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: 965
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
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.
mmain
Posts: 19
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: 965
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
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.
plain
Posts: 94
Joined: Mon Apr 18, 2011 4:49 pm

Re: Randomise Playlist

Post by plain »

Oh Wow! This is great! Thanks drakinite! I don't know if I have the skill to do this but knowing it is possible and how to do it is going to be a really nice encouragement to try! Thanks!
telecore
Posts: 57
Joined: Thu Jan 28, 2021 10:20 pm

Re: Randomise Playlist

Post by telecore »

Would someone be able to show me how to expand upon this code a bit (?): I am trying to re-write a MM4 VBS script that I developed which does the following:

(1) create a new non-auto playlist with the same name (+ "*" at the end), in the same location as the specified playlist (either auto or non-auto) - (I have this part basically working)
(2) iterate through the tracklist of the specified playlist and copy tracks determined to be unique (non-duplicate) per a programmatic criteria to the new playlist (cannot seem to do this)
(3) Note: the criteria for uniqueness is basically that the artist and title strings are different from all other tracks in the playlist, however, this will be expanded upon programmatically for some string cleanup prior to comparison and a selection criteria would eventually be added to select the highest bitrate version of any duplicated tracks for inclusion in the new playlist

I use this script all the time on auto-playlists that contain a lot of duplicates in MM4 but I am having difficulties getting some basic things to work in this new environment
shani
Posts: 8
Joined: Sat May 15, 2021 12:16 am

Randomize stattic playlist

Post by shani »

In version 4 there was a script that randomize a static playlist. is There something familiar in version 5?
drakinite
Posts: 965
Joined: Tue May 12, 2020 10:06 am
Contact:

Re: Randomise Playlist

Post by drakinite »

I've now put the script up on the site as a sample addon here: https://www.mediamonkey.com/addons/brow ... laylist-1/ :slight_smile:
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.
Poobslag
Posts: 10
Joined: Sun Mar 30, 2014 10:37 pm

Re: Randomise Playlist

Post by Poobslag »

As an alternative way to randomize a playlist without a script:

1. Right-click your playlist, and select "Play Shuffled -> Play Now". (The playlist will play in a random order.)
2. Delete the contents of your playlist. (The playlist is now empty.)
3. In the "Playing" panel, press "CTRL + A" and "CTRL + C" to copy all playing items.
4. Right-click your playlist and select "Paste". (The playlist is now in random order.)
Post Reply