Enable WebAudio to play even when the mute toggle switch is ON, closes #201 (#221)

This commit is contained in:
Huang Xin
2025-01-23 00:52:44 +01:00
committed by GitHub
parent c678d8fa44
commit fb4853a9d4
3 changed files with 15 additions and 1 deletions
@@ -4,7 +4,7 @@ import { useBookDataStore } from '@/store/bookDataStore';
import { useReaderStore } from '@/store/readerStore';
import { useTranslation } from '@/hooks/useTranslation';
import { useResponsiveSize } from '@/hooks/useResponsiveSize';
import { TTSController } from '@/services/tts/TTSController';
import { TTSController, SILENCE_DATA } from '@/services/tts';
import { getPopupPosition, Position } from '@/utils/sel';
import { eventDispatcher } from '@/utils/event';
import { parseSSMLLang } from '@/utils/ssml';
@@ -38,6 +38,16 @@ const TTSControl = () => {
const iconRef = useRef<HTMLDivElement>(null);
const ttsControllerRef = useRef<TTSController | null>(null);
// this enables WebAudio to play even when the mute toggle switch is ON
const unblockAudio = () => {
const audio = document.createElement('audio');
audio.setAttribute('x-webkit-airplay', 'deny');
audio.preload = 'auto';
audio.loop = false;
audio.src = SILENCE_DATA;
audio.play();
};
useEffect(() => {
return () => {
if (ttsControllerRef.current) {
@@ -82,6 +92,7 @@ const TTSControl = () => {
setShowIndicator(true);
try {
unblockAudio();
const ttsController = new TTSController(view);
await ttsController.init();
await ttsController.initViewTTS();