Prevent Media Session Handling on MPV (#1212)

* Remove MediaSession Handling on MPV

* Add playbackType to config.json for Main Thread Access

* Disabling settings without Hiding
This commit is contained in:
Xudong Zhou 2025-11-02 09:26:16 +08:00 committed by GitHub
parent 0ca9eb0fcd
commit 829c27a5e9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 24 additions and 7 deletions

View file

@ -3,6 +3,8 @@ import { BrowserWindow, globalShortcut, systemPreferences } from 'electron';
import { isMacOS, isWindows } from '../../../utils'; import { isMacOS, isWindows } from '../../../utils';
import { store } from '../settings'; import { store } from '../settings';
import { PlaybackType } from '/@/shared/types/types';
export const enableMediaKeys = (window: BrowserWindow | null) => { export const enableMediaKeys = (window: BrowserWindow | null) => {
if (isMacOS()) { if (isMacOS()) {
const shouldPrompt = store.get('should_prompt_accessibility', true) as boolean; const shouldPrompt = store.get('should_prompt_accessibility', true) as boolean;
@ -24,7 +26,9 @@ export const enableMediaKeys = (window: BrowserWindow | null) => {
} }
const enableWindowsMediaSession = store.get('mediaSession', false) as boolean; const enableWindowsMediaSession = store.get('mediaSession', false) as boolean;
if (!(enableWindowsMediaSession && isWindows())) { const playbackType = store.get('playbackType', PlaybackType.WEB) as PlaybackType;
if (!enableWindowsMediaSession || !isWindows() || playbackType !== PlaybackType.WEB) {
globalShortcut.register('MediaStop', () => { globalShortcut.register('MediaStop', () => {
window?.webContents.send('renderer-player-stop'); window?.webContents.send('renderer-player-stop');
}); });

View file

@ -38,7 +38,7 @@ import {
} from './utils'; } from './utils';
import './features'; import './features';
import { TitleTheme } from '/@/shared/types/types'; import { PlaybackType, TitleTheme } from '/@/shared/types/types';
export default class AppUpdater { export default class AppUpdater {
constructor() { constructor() {
@ -549,7 +549,9 @@ async function createWindow(first = true): Promise<void> {
} }
const enableWindowsMediaSession = store.get('mediaSession', false) as boolean; const enableWindowsMediaSession = store.get('mediaSession', false) as boolean;
const shouldDisableMediaFeatures = !isWindows() || !enableWindowsMediaSession; const playbackType = store.get('playbackType', PlaybackType.WEB) as PlaybackType;
const shouldDisableMediaFeatures =
!isWindows() || !enableWindowsMediaSession || playbackType !== PlaybackType.WEB;
if (shouldDisableMediaFeatures) { if (shouldDisableMediaFeatures) {
app.commandLine.appendSwitch( app.commandLine.appendSwitch(
'disable-features', 'disable-features',

View file

@ -7,6 +7,7 @@ import {
} from '/@/renderer/features/settings/components/settings-section'; } from '/@/renderer/features/settings/components/settings-section';
import { useHotkeySettings, usePlaybackSettings, useSettingsStoreActions } from '/@/renderer/store'; import { useHotkeySettings, usePlaybackSettings, useSettingsStoreActions } from '/@/renderer/store';
import { Switch } from '/@/shared/components/switch/switch'; import { Switch } from '/@/shared/components/switch/switch';
import { PlaybackType } from '/@/shared/types/types';
const localSettings = isElectron() ? window.api.localSettings : null; const localSettings = isElectron() ? window.api.localSettings : null;
const isWindows = isElectron() ? window.api.utils.isWindows() : false; const isWindows = isElectron() ? window.api.utils.isWindows() : false;
@ -15,14 +16,19 @@ export const WindowHotkeySettings = () => {
const { t } = useTranslation(); const { t } = useTranslation();
const settings = useHotkeySettings(); const settings = useHotkeySettings();
const { setSettings } = useSettingsStoreActions(); const { setSettings } = useSettingsStoreActions();
const { mediaSession: enableWindowsMediaSession } = usePlaybackSettings(); const { mediaSession: enableWindowsMediaSession, type: playbackType } = usePlaybackSettings();
const options: SettingOption[] = [ const options: SettingOption[] = [
{ {
control: ( control: (
<Switch <Switch
defaultChecked={settings.globalMediaHotkeys} defaultChecked={settings.globalMediaHotkeys}
disabled={!isElectron() || (enableWindowsMediaSession && isWindows)} disabled={
!isElectron() ||
(enableWindowsMediaSession &&
isWindows &&
playbackType === PlaybackType.WEB)
}
onChange={(e) => { onChange={(e) => {
setSettings({ setSettings({
hotkeys: { hotkeys: {
@ -44,7 +50,7 @@ export const WindowHotkeySettings = () => {
context: 'description', context: 'description',
postProcess: 'sentenceCase', postProcess: 'sentenceCase',
}), }),
isHidden: !isElectron() || (enableWindowsMediaSession && isWindows), isHidden: !isElectron(),
title: t('setting.globalMediaHotkeys', { postProcess: 'sentenceCase' }), title: t('setting.globalMediaHotkeys', { postProcess: 'sentenceCase' }),
}, },
]; ];

View file

@ -15,6 +15,8 @@ import { Switch } from '/@/shared/components/switch/switch';
import { toast } from '/@/shared/components/toast/toast'; import { toast } from '/@/shared/components/toast/toast';
import { CrossfadeStyle, PlaybackStyle, PlaybackType, PlayerStatus } from '/@/shared/types/types'; import { CrossfadeStyle, PlaybackStyle, PlaybackType, PlayerStatus } from '/@/shared/types/types';
const ipc = isElectron() ? window.api.ipc : null;
const getAudioDevice = async () => { const getAudioDevice = async () => {
const devices = await navigator.mediaDevices.enumerateDevices(); const devices = await navigator.mediaDevices.enumerateDevices();
return (devices || []).filter((dev: MediaDeviceInfo) => dev.kind === 'audiooutput'); return (devices || []).filter((dev: MediaDeviceInfo) => dev.kind === 'audiooutput');
@ -62,6 +64,7 @@ export const AudioSettings = ({ hasFancyAudio }: { hasFancyAudio: boolean }) =>
disabled={status === PlayerStatus.PLAYING} disabled={status === PlayerStatus.PLAYING}
onChange={(e) => { onChange={(e) => {
setSettings({ playback: { ...settings, type: e as PlaybackType } }); setSettings({ playback: { ...settings, type: e as PlaybackType } });
ipc?.send('settings-set', { property: 'playbackType', value: e });
if (isElectron() && e === PlaybackType.LOCAL) { if (isElectron() && e === PlaybackType.LOCAL) {
const queueData = usePlayerStore.getState().actions.getPlayerData(); const queueData = usePlayerStore.getState().actions.getPlayerData();
setQueue(queueData); setQueue(queueData);

View file

@ -7,6 +7,7 @@ import {
} from '/@/renderer/features/settings/components/settings-section'; } from '/@/renderer/features/settings/components/settings-section';
import { usePlaybackSettings, useSettingsStoreActions } from '/@/renderer/store/settings.store'; import { usePlaybackSettings, useSettingsStoreActions } from '/@/renderer/store/settings.store';
import { Switch } from '/@/shared/components/switch/switch'; import { Switch } from '/@/shared/components/switch/switch';
import { PlaybackType } from '/@/shared/types/types';
const isWindows = isElectron() ? window.api.utils.isWindows() : null; const isWindows = isElectron() ? window.api.utils.isWindows() : null;
const isDesktop = isElectron(); const isDesktop = isElectron();
@ -14,7 +15,7 @@ const ipc = isElectron() ? window.api.ipc : null;
export const MediaSessionSettings = () => { export const MediaSessionSettings = () => {
const { t } = useTranslation(); const { t } = useTranslation();
const { mediaSession } = usePlaybackSettings(); const { mediaSession, type: playbackType } = usePlaybackSettings();
const { toggleMediaSession } = useSettingsStoreActions(); const { toggleMediaSession } = useSettingsStoreActions();
function handleMediaSessionChange() { function handleMediaSessionChange() {
@ -29,6 +30,7 @@ export const MediaSessionSettings = () => {
<Switch <Switch
aria-label="Toggle media Session" aria-label="Toggle media Session"
defaultChecked={mediaSession} defaultChecked={mediaSession}
disabled={!isWindows || !isDesktop || playbackType !== PlaybackType.WEB}
onChange={handleMediaSessionChange} onChange={handleMediaSessionChange}
/> />
), ),