Problem when combining sync with async function

Post a reply

Smilies
:D :) :( :o :-? 8) :lol: :x :P :oops: :cry: :evil: :roll: :wink:

BBCode is ON
[img] is ON
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Problem when combining sync with async function

Re: Problem when combining sync with async function

by drakinite » Sat Sep 24, 2022 6:11 pm

No prob! Async functions can be annoying to work with, but it's essential in JS to keep the UI operating smoothly :)

Re: Problem when combining sync with async function

by marceros » Sat Sep 24, 2022 2:59 pm

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

It seems to work perfectly now. Thank you!

Re: Problem when combining sync with async function

by drakinite » Sat Sep 24, 2022 2:11 pm

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

Problem when combining sync with async function

by marceros » Fri Sep 23, 2022 8:18 pm

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

Top