Problem when combining sync with async function

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

Moderators: jiri, drakinite, Addon Administrators

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

Problem when combining sync with async function

Post by marceros »

Hello,

I have written a script that, when the button is clicked, the volume is slowly lowered and then the player plays the next song and brings the voluma back to its previous level.

Code: Select all

const volumeToSet = 0
const durationToSet = 3 // 3 secs to lower the volume to 'volumeToSet' and start next track
const stepDuration = 0.050 // 50 ms between steps

if(app.player.isPlaying){
				
				initialVolume = app.player.volume;
				volumeStep = initialVolume / (durationToSet / stepDuration)
				actualVolume = app.player.volume;
				
				let timerId = setInterval(() => {
					if(actualVolume > 0){
						actualVolume = Math.max((actualVolume - volumeStep), 0)
						app.player.volume = actualVolume
						console.log(actualVolume)
						//console.log(actualVolume)
					}else{
						// app.player.stopAsync();
						clearInterval(timerId);
						app.player.nextAsync(); // pasa al proximo tema
						app.player.volume = initialVolume; // devuelve el volumen al valor anterior
					}
				}, (stepDuration * 1000));

			}else{
				app.player.nextAsync(); // pasa DIRECTAMENTE al proximo tema
			}
It worked good for some time and now I hear a noise, just for a moment, before the player moves to the next song. I'm not sure about this but I think the noise is due to a delay in the nextAsync() method that actually happens after the volume goes back to its original level.

I'll be happy to hear your suggestions.

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

Re: Problem when combining sync with async function

Post by drakinite »

Try:

Code: Select all

app.player.nextAsync().then(() => {
   app.player.volume = initialVolume; 
   clearInterval(timerId);
});
What's probably happening is:

step 1. The player is requested to *eventually* skip to the next track (which realistically will execute in some number of milliseconds, but NOT instantly)
step 2. The player volume jumps back to its original value BEFORE the song changes, causing the noise
step 3. the song then changes
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: Problem when combining sync with async function

Post by marceros »

Of course! Promises... Have to get used to JavaScript...

It seems to work perfectly now. Thank you!
drakinite
Posts: 965
Joined: Tue May 12, 2020 10:06 am
Contact:

Re: Problem when combining sync with async function

Post by drakinite »

No prob! Async functions can be annoying to work with, but it's essential in JS to keep the UI operating smoothly :)
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