track.loadCoverListAsync() not working properly

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

Moderators: jiri, drakinite, Addon Administrators

bdshasta
Posts: 22
Joined: Wed Sep 29, 2021 10:43 pm

track.loadCoverListAsync() not working properly

Post by bdshasta »

Please consider the following code snippet. I have this piece of code to grab the first track's cover art and save to a file. Outside of this code I have same being performed for other track metadata. The other track metadata *always* works. The below snippet works always when I manually select a new track and play it, but it only works around 5% of the time when the track switches in a playlist. Again, the non-cover art metadata always fires properly in both manual plays and playlists.

I've narrowed it down to var covers = await track.loadCoverListAsync() not actually returning >0 for tracks when they are part of a playlist. Then again, 5% of the time they do return >0. And again they return >0 100% of the time when I play those same tracks manually.

Any idea where to debug this next?

Code: Select all

	
	saveCoverToFile: async function(track, path){
		// exports first cover of track
		var _this = this;
		var covers = await track.loadCoverListAsync()
		console.debug(`covers.count= ${covers.count}`);

		if(covers.count > 0){
			await covers.whenLoaded(await covers.locked(async () =>{
				var exportCover = await covers.getValue(0);
				var ext = await _this.getCoverFileType(exportCover.pictureType);
				var exportPath = `${path}\.${ext}`;
				await exportCover.saveToFile(exportPath);
			}))
		}
	},
drakinite
Posts: 965
Joined: Tue May 12, 2020 10:06 am
Contact:

Re: track.loadCoverListAsync() not working properly

Post by drakinite »

1 - track.loadCoverListAsync() is actually poorly named. It does not return a promise; instead, it returns the CoverList immediately. So when you're awaiting track.loadCoverListAsync(), the await does nothing. (If you await a non-Promise variable, it'll just return that variable instantly.)
2 - list.count only becomes valid after you await list.whenLoaded(). If you do not wait until it is loaded, sometimes the count will be zero and sometimes it will be correct (or even perhaps a number in between 0 and the correct value).
3 - covers.locked(), covers.getValue, and exportCover.saveToFile() are all synchronous. Do not await them (same reason as #1).


Should look something akin to this:

Code: Select all

        saveCoverToFile: async function(track, path){
            // exports first cover of track
            var _this = this;
            var covers = track.loadCoverListAsync();
            await covers.whenLoaded();
            console.debug(`covers.count= ${covers.count}`);
    
            if(covers.count > 0){
                covers.locked(async () =>{
                    var exportCover = covers.getValue(0);
                    var ext = await _this.getCoverFileType(exportCover.pictureType);
                    var exportPath = `${path}\.${ext}`;
                    exportCover.saveToFile(exportPath);
                });
            }
        },

If you prefer one-liners, the following snippet is also valid (because you're awaiting the promise that whenLoaded() returns, not the list returned by loadCoverListAsync()):

Code: Select all

var covers = await track.loadCoverListAsync().whenLoaded();
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.
bdshasta
Posts: 22
Joined: Wed Sep 29, 2021 10:43 pm

Re: track.loadCoverListAsync() not working properly

Post by bdshasta »

That worked, problem solved, thank you!
Post Reply