Asem75 commited on
Commit
1f01001
·
verified ·
1 Parent(s): 12f71db

Create background_audio.js

Browse files
Files changed (1) hide show
  1. background_audio.js +44 -0
background_audio.js ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* طبقة عامة للصوت في الخلفية: الراديو والقرآن وأي <audio>.
2
+ 1) تعريف الجلسة (Media Session) ليتعامل معها الهاتف كتشغيل وسائط ويعرض التحكم على شاشة القفل.
3
+ 2) إن أوقف المتصفح الصوت لحظة إخفاء الصفحة (لا المستخدم ولا كود التطبيق) يُستأنف مرة واحدة.
4
+ يجب تحميله قبل بقية السكربتات. للوحدات الأخرى: MuslimBackgroundAudio.describe(audioElement,{title,artist}) */
5
+ (function () {
6
+ 'use strict';
7
+ const session = navigator.mediaSession, tracked = new WeakSet(), intentional = new WeakSet(), info = new WeakMap();
8
+ const nativePlay = HTMLMediaElement.prototype.play, nativePause = HTMLMediaElement.prototype.pause;
9
+ let current = null, hiddenAt = -Infinity;
10
+ const RESUME_WINDOW_MS = 2000; // الإيقاف الذي يلي الإخفاء مباشرة فقط يُعدّ إيقافًا من المتصفح
11
+ const RESUME_DELAY_MS = 300;
12
+
13
+ function publish(el) {
14
+ if (!session || typeof MediaMetadata === 'undefined') return;
15
+ const d = info.get(el) || {};
16
+ session.metadata = new MediaMetadata({
17
+ title: d.title || el.dataset.title || el.getAttribute('aria-label') || el.title || document.title || '',
18
+ artist: d.artist || el.dataset.artist || '', album: d.album || '', artwork: d.artwork || []
19
+ });
20
+ }
21
+ function track(el) {
22
+ if (tracked.has(el)) return;
23
+ tracked.add(el);
24
+ el.addEventListener('playing', () => { current = el; intentional.delete(el); publish(el); if (session) session.playbackState = 'playing'; });
25
+ el.addEventListener('pause', () => {
26
+ if (current !== el) return;
27
+ if (session) session.playbackState = 'paused';
28
+ const justHidden = document.hidden && performance.now() - hiddenAt < RESUME_WINDOW_MS;
29
+ if (justHidden && !el.ended && !intentional.has(el)) setTimeout(() => { if (el.paused && !el.ended) nativePlay.call(el).catch(() => {}); }, RESUME_DELAY_MS);
30
+ });
31
+ el.addEventListener('ended', () => { if (current === el && session) session.playbackState = 'none'; });
32
+ }
33
+ HTMLMediaElement.prototype.play = function () { track(this); return nativePlay.apply(this, arguments); };
34
+ HTMLMediaElement.prototype.pause = function () { intentional.add(this); return nativePause.apply(this, arguments); };
35
+ document.addEventListener('visibilitychange', () => { if (document.hidden) hiddenAt = performance.now(); });
36
+
37
+ if (session) {
38
+ const set = (action, fn) => { try { session.setActionHandler(action, fn); } catch (_) {} };
39
+ set('play', () => { if (current) { intentional.delete(current); nativePlay.call(current).catch(() => {}); } });
40
+ set('pause', () => { if (current) { intentional.add(current); nativePause.call(current); } });
41
+ set('stop', () => { if (current) { intentional.add(current); nativePause.call(current); current.currentTime = 0; } });
42
+ }
43
+ window.MuslimBackgroundAudio = Object.freeze({ describe(el, data) { info.set(el, data || {}); if (current === el) publish(el); } });
44
+ })();