From a9af1e91d437d22fdbf8a80ebc197dfd5036c470 Mon Sep 17 00:00:00 2001 From: Tarulia <556162+mihawk90@users.noreply.github.com> Date: Wed, 22 Oct 2025 06:48:09 +0200 Subject: [PATCH] Disable Media Keys with MediaSession on Windows (#1207) In f07393c8 we enabled the MediaSession API, which from Chromium's side brings its own native way of handling Global Media Keys. However, it turns out having this enabled seemingly conflicts with Windows 11's SMTC implementation when we also bind the Media Keys using Electron's Global Hotkeys API (Windows 10 is apparently fine, but now EOL). Globally passing `HardwareMediaKeyHandling` to `disable-features` was considered, however using the MediaSession API requires `HardwareMediaKeyHandling` to be enabled, so this is not an option. Instead, with MediaSession enabled we need to let Chromium handle the Media Keys, while without MediaSession we bind our own Global Hot Keys for users that have them enabled in the settings. Co-authored-by: Xudong Zhou --- src/main/features/core/player/media-keys.ts | 29 ++++++++++--------- src/main/index.ts | 2 +- .../hotkeys/window-hotkey-settings.tsx | 8 +++-- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/main/features/core/player/media-keys.ts b/src/main/features/core/player/media-keys.ts index dfed0c88..815944da 100644 --- a/src/main/features/core/player/media-keys.ts +++ b/src/main/features/core/player/media-keys.ts @@ -1,6 +1,6 @@ import { BrowserWindow, globalShortcut, systemPreferences } from 'electron'; -import { isMacOS } from '../../../utils'; +import { isMacOS, isWindows } from '../../../utils'; import { store } from '../settings'; export const enableMediaKeys = (window: BrowserWindow | null) => { @@ -23,21 +23,24 @@ export const enableMediaKeys = (window: BrowserWindow | null) => { } } - globalShortcut.register('MediaStop', () => { - window?.webContents.send('renderer-player-stop'); - }); + const enableWindowsMediaSession = store.get('mediaSession', false) as boolean; + if (!(enableWindowsMediaSession && isWindows())) { + globalShortcut.register('MediaStop', () => { + window?.webContents.send('renderer-player-stop'); + }); - globalShortcut.register('MediaPlayPause', () => { - window?.webContents.send('renderer-player-play-pause'); - }); + globalShortcut.register('MediaPlayPause', () => { + window?.webContents.send('renderer-player-play-pause'); + }); - globalShortcut.register('MediaNextTrack', () => { - window?.webContents.send('renderer-player-next'); - }); + globalShortcut.register('MediaNextTrack', () => { + window?.webContents.send('renderer-player-next'); + }); - globalShortcut.register('MediaPreviousTrack', () => { - window?.webContents.send('renderer-player-previous'); - }); + globalShortcut.register('MediaPreviousTrack', () => { + window?.webContents.send('renderer-player-previous'); + }); + } }; export const disableMediaKeys = () => { diff --git a/src/main/index.ts b/src/main/index.ts index 24ddd934..3eabbf73 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -549,7 +549,7 @@ async function createWindow(first = true): Promise { } const enableWindowsMediaSession = store.get('mediaSession', false) as boolean; -const shouldDisableMediaFeatures = process.platform !== 'win32' || !enableWindowsMediaSession; +const shouldDisableMediaFeatures = !isWindows() || !enableWindowsMediaSession; if (shouldDisableMediaFeatures) { app.commandLine.appendSwitch( 'disable-features', diff --git a/src/renderer/features/settings/components/hotkeys/window-hotkey-settings.tsx b/src/renderer/features/settings/components/hotkeys/window-hotkey-settings.tsx index 2a307232..a93edfdc 100644 --- a/src/renderer/features/settings/components/hotkeys/window-hotkey-settings.tsx +++ b/src/renderer/features/settings/components/hotkeys/window-hotkey-settings.tsx @@ -5,22 +5,24 @@ import { SettingOption, SettingsSection, } from '/@/renderer/features/settings/components/settings-section'; -import { useHotkeySettings, useSettingsStoreActions } from '/@/renderer/store'; +import { useHotkeySettings, usePlaybackSettings, useSettingsStoreActions } from '/@/renderer/store'; import { Switch } from '/@/shared/components/switch/switch'; const localSettings = isElectron() ? window.api.localSettings : null; +const isWindows = isElectron() ? window.api.utils.isWindows() : false; export const WindowHotkeySettings = () => { const { t } = useTranslation(); const settings = useHotkeySettings(); const { setSettings } = useSettingsStoreActions(); + const { mediaSession: enableWindowsMediaSession } = usePlaybackSettings(); const options: SettingOption[] = [ { control: ( { setSettings({ hotkeys: { @@ -42,7 +44,7 @@ export const WindowHotkeySettings = () => { context: 'description', postProcess: 'sentenceCase', }), - isHidden: !isElectron(), + isHidden: !isElectron() || (enableWindowsMediaSession && isWindows), title: t('setting.globalMediaHotkeys', { postProcess: 'sentenceCase' }), }, ];