Skip to content

Fix volume slider always at max when using custom volume output #2900

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 48 additions & 38 deletions js&css/web-accessible/www.youtube.com/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,8 +502,8 @@ BATTERY FEATURES; PLAYER QUALITY BASED ON POWER STATUS
------------------------------------------------------------------------------*/
ImprovedTube.batteryFeatures = async function () {
if (ImprovedTube.storage.qualityWhenRunningOnBattery
|| ImprovedTube.storage.pauseWhileIUnplugTheCharger
|| ImprovedTube.storage.whenBatteryIslowDecreaseQuality) {
|| ImprovedTube.storage.pauseWhileIUnplugTheCharger
|| ImprovedTube.storage.whenBatteryIslowDecreaseQuality) {
const updateQuality = async (battery, charging) => {
if (battery) {
if (!battery.charging) {
Expand Down Expand Up @@ -543,53 +543,63 @@ ImprovedTube.batteryFeatures = async function () {
FORCED VOLUME
------------------------------------------------------------------------------*/
ImprovedTube.playerVolume = function () {
if (this.storage.player_forced_volume === true) {
var volume = this.storage.player_volume;

if (!this.isset(volume)) {
volume = 100;
} else {
volume = Number(volume);
}

if (!this.audioContextGain && volume <= 100) {
if (this.audioContext) {
this.audioContext.close();
}

this.elements.player.setVolume(volume);
} else {
if (!this.audioContext) {
this.audioContext = new AudioContext();
if (this.storage.player_forced_volume === true) {
var volume = this.storage.player_volume;
var userVolume = this.storage.user_preferred_volume || 100;

if (!this.isset(volume)) {
volume = 100;
} else {
volume = Number(volume);
}

if (!this.audioContextGain && volume <= 100) {
if (this.audioContext) {
this.audioContext.close();
}
this.elements.player.setVolume(userVolume);
} else {
if (!this.audioContext) {
this.audioContext = new AudioContext();
this.audioContextSource = this.audioContext.createMediaElementSource(document.querySelector('video'));
this.audioContextGain = this.audioContext.createGain();
this.audioContextGain.gain.value = 1;
this.audioContextSource.connect(this.audioContextGain);
this.audioContextGain.connect(this.audioContext.destination)
}
// Set the player volume to the user's preferred level
this.elements.player.setVolume(userVolume);
// Apply gain amplification based on both the forced volume and user's preferred volume
this.audioContextGain.gain.value = (volume / 100) * (userVolume / 100);
}
}
};

this.audioContextSource = this.audioContext.createMediaElementSource(document.querySelector('video'));
this.audioContextGain = this.audioContext.createGain();
// Add a function to save the user's preferred volume
ImprovedTube.saveUserPreferredVolume = function(value) {
this.storage.user_preferred_volume = value;
};

this.audioContextGain.gain.value = 1;
this.audioContextSource.connect(this.audioContextGain);
this.audioContextGain.connect(this.audioContext.destination)
}
if (this.elements.player.getVolume() !== 100) { this.elements.player.setVolume(100);}
this.audioContextGain.gain.value = volume / 100;
}
}
// Modify the volume change handler
ImprovedTube.onvolumechange = function () {
if (document.querySelector('.ytp-volume-panel')) {
var volume = Number(document.querySelector('.ytp-volume-panel').getAttribute('aria-valuenow'));
this.volume = volume / 100;
// Save the user's preferred volume when they change it manually
this.saveUserPreferredVolume(volume);
}
};

/*------------------------------------------------------------------------------
LOUDNESS NORMALIZATION
------------------------------------------------------------------------------*/
ImprovedTube.onvolumechange = function () {
if (document.querySelector('.ytp-volume-panel') && ImprovedTube.storage.player_loudness_normalization === false) {
var volume = Number(document.querySelector('.ytp-volume-panel').getAttribute('aria-valuenow'));

this.volume = volume / 100;
}
};

ImprovedTube.playerLoudnessNormalization = function () {
var video = this.elements.video;

if (video) {
video.removeEventListener('volumechange', this.onvolumechange);

// Add a new event listener for volume change
video.addEventListener('volumechange', this.onvolumechange);
}

Expand Down