···170170 hls.currentLevel = -1;
171171 hls.loadLevel = -1;
172172 }
173173- // Only emit quality when we have a valid level index (>= 0)
174174- // When automaticQuality is true, currentLevel is -1, so we wait for LEVEL_SWITCHED event
175175- if (hls.currentLevel >= 0) {
176176- const quality = hlsLevelToQuality(hls.levels[hls.currentLevel]);
177177- emit("changedquality", quality);
178178- }
173173+ // For manual quality selection, wait for LEVEL_SWITCHED to emit quality
174174+ // to avoid showing intermediate states when HLS switches away from unplayable levels
175175+ // For automatic quality, currentLevel is -1, so we wait for LEVEL_SWITCHED event
179176 }
180177181178 function setupSource(vid: HTMLVideoElement, src: LoadableSource) {
···313310 // Don't process level switched events during debounced quality changes
314311 if (qualityChangeTimeout) return;
315312313313+ const currentLevel = hls.levels[hls.currentLevel];
314314+ const currentQuality = hlsLevelToQuality(currentLevel);
315315+316316 if (automaticQuality) {
317317 // Only emit quality changes when automatic quality is enabled
318318- const quality = hlsLevelToQuality(hls.levels[hls.currentLevel]);
319319- emit("changedquality", quality);
318318+ emit("changedquality", currentQuality);
320319 } else {
321321- // When automatic quality is disabled, check if current level matches preferred quality
322322- const currentQuality = hlsLevelToQuality(
323323- hls.levels[hls.currentLevel],
324324- );
325325- const preferredQualityLevel = getPreferredQuality(
326326- hlsLevelsToQualities(hls.levels),
327327- {
328328- lastChosenQuality: preferenceQuality,
329329- automaticQuality: false,
330330- },
331331- );
332332- // Only re-lock if the current level doesn't match our preferred quality
333333- if (currentQuality !== preferredQualityLevel) {
334334- setupQualityForHls();
335335- } else {
336336- // Emit the quality change since we're now at the correct level
337337- emit("changedquality", currentQuality);
338338- }
320320+ // For manual quality selection, emit the user's preferred quality
321321+ // This ensures the UI shows the selected quality, not the actual playing quality
322322+ emit("changedquality", preferenceQuality);
339323 }
340324 });
341325 hls.on(Hls.Events.SUBTITLE_TRACK_LOADED, () => {