event.target.setVolume(0); // doesn't work by itself
In fact, in the next line
t.getVolume(); // spits 100, always
But when it is placed inside an event handler, it does. Why?
scroll.addEventListener('wheel', function (e) { changeVolume(e, this) });
function changeVolume(event, el)
{
const dir = Math.sign(event.deltaY) * -1;
const parsed = parseInt(el.value, 10);
const value = Number(dir + parsed);
t.setVolume(value); // TARGET
// OH wait! Now - it does work. Why?
}
Ok. So the iframe needs to be loaded to change -this-one-property-, everything else works fine - set - get - : PlaybackRate, sekto, unMute, mute... EVERYTHING!!
Within the target proto: store a closure with the desired volume and protect yourself with a guard clause.
function onPlayerReady(event)
{
const t = event.target;
function printVol()
{
console.log(
{
getVol: t.getVolume(), //nope
volume: t.__proto__.newVol
}
);
}
function readyToChangeVolumeOnce()
{
if (!t.__proto__.changedVolumeOnce)
{
t.__proto__.changedVolumeOnce = true;
t.setVolume(t.__proto__.newVol);
printVol(); // works as expected... right. Should I be doing this?
}
}
// javascript is crazy
t.__proto__.changedVolumeOnce = false;
t.__proto__.readyToChangeVolumeOnce = readyToChangeVolumeOnce;
t.__proto__.newVol = validVolume(); // 20 for example
}
Once the iframe (API) knows it's alright to play, then call the function inside the proto.
function onStateChange(state)
{
const t = state.target;
if (state.data === YT.PlayerState.PLAYING)
{
t.__proto__.readyToChangeVolumeOnce(); //man...
}
}
It's a workaround, I don't think it is an answer per se. I'm happy it works though.
I also noticed that t.unmute() assigns a low volume if the volume was 0.
Again, everybody, all of this could be a MAJOR misunderstanding form my end. I don't know.