Добавить проверку звука в браузере

This commit is contained in:
AidarKC
2026-11-30 11:55:36 +03:00
parent 3f3e93c87d
commit c175a54a01
6 changed files with 130 additions and 65 deletions
+3 -62
View File
@@ -25,6 +25,7 @@ import { dmNotificationTag, dmUnreadLabel } from './services/dm-push-notificatio
import { initCallUiOverlay } from './services/call-ui-service.js';
import { showToast } from './services/channels-ux.js';
import { toUserMessage } from './services/ui-error-texts.js';
import { unlockDmAudio, playDmSignal } from './services/dm-audio.js';
import { KeyRotationClient } from './services/key-rotation-service.js';
import {
handleCallPushAction,
@@ -123,7 +124,6 @@ import * as contactSearchView from './pages/contact-search-view.js';
import * as chatView from './pages/chat-view.js?v=202611170124';
import * as chatMediaView from './pages/chat-media-view.js';
import { isDmChatMuted } from './services/dm-chat-prefs.js';
import { getDevicePref } from './services/device-prefs.js';
import { isDeveloperToolsEnabled } from './services/feature-settings.js';
import * as userProfileView from './pages/user-profile-view.js?v=20261110-pwa-avatar';
import * as userProfileListView from './pages/user-profile-list-view.js';
@@ -281,8 +281,6 @@ let uiUpdateReloadScheduled = false;
let pwaUpdateCheckAttempted = false;
let uiVersionCheckInFlight = false;
let uiVersionPeriodicIntervalId = null;
let hiddenDmAudioContext = null;
let hiddenDmAudioUnlocked = false;
let initialConnectionCompleted = false;
let orientationLockInFlight = false;
let currentChromeCleanup = null;
@@ -666,63 +664,6 @@ function clearKeepAliveEntries() {
}
}
async function unlockHiddenDmAudio() {
try {
const Ctx = window.AudioContext || window.webkitAudioContext;
if (!Ctx) return false;
if (!hiddenDmAudioContext) {
hiddenDmAudioContext = new Ctx();
}
if (hiddenDmAudioContext.state === 'suspended') {
await hiddenDmAudioContext.resume();
}
hiddenDmAudioUnlocked = hiddenDmAudioContext.state === 'running';
return hiddenDmAudioUnlocked;
} catch {
return false;
}
}
async function playDmSignal({ extended = false } = {}) {
try {
// «Звук в открытом приложении» выключен в настройках уведомлений.
if (getDevicePref('dmSound', true) === false) return false;
if (!hiddenDmAudioUnlocked || !hiddenDmAudioContext) return false;
if (hiddenDmAudioContext.state === 'suspended') {
await hiddenDmAudioContext.resume();
}
if (hiddenDmAudioContext.state !== 'running') return false;
const now = hiddenDmAudioContext.currentTime;
const gain = hiddenDmAudioContext.createGain();
gain.connect(hiddenDmAudioContext.destination);
gain.gain.setValueAtTime(0.0001, now);
const pulse = (offsetSec, freqHz, durationSec, peakGain) => {
const osc = hiddenDmAudioContext.createOscillator();
osc.type = 'sine';
osc.frequency.setValueAtTime(freqHz, now + offsetSec);
osc.connect(gain);
gain.gain.exponentialRampToValueAtTime(peakGain, now + offsetSec + 0.01);
gain.gain.exponentialRampToValueAtTime(0.0001, now + offsetSec + durationSec);
osc.start(now + offsetSec);
osc.stop(now + offsetSec + durationSec + 0.02);
};
if (extended) {
pulse(0, 880, 0.18, 0.032);
pulse(0.24, 1174, 0.2, 0.026);
pulse(0.52, 1567, 0.24, 0.02);
} else {
pulse(0, 1046, 0.12, 0.028);
pulse(0.17, 1318, 0.14, 0.022);
}
return true;
} catch {
return false;
}
}
async function tryLockPortraitOrientation() {
if (orientationLockInFlight) return false;
@@ -2160,10 +2101,10 @@ async function init() {
renderApp();
});
document.addEventListener('pointerdown', () => {
void unlockHiddenDmAudio();
void unlockDmAudio();
}, { passive: true });
document.addEventListener('keydown', () => {
void unlockHiddenDmAudio();
void unlockDmAudio();
}, { passive: true });
document.addEventListener('visibilitychange', () => {
if (document.visibilityState !== 'visible') return;