@@ -54,9 +54,9 @@ window.addEventListener('beforeunload', () => {
5454 if ( lyricVideoIsOpen ) setPopupState ( false ) ;
5555} ) ;
5656
57- export const audioPromise = new Promise < HTMLAudioElement > ( ( resolveAudio ) => {
58- let audio : HTMLAudioElement | null = null ;
57+ let audio : HTMLAudioElement | null = null ;
5958
59+ const firstAudioPromise = new Promise < HTMLAudioElement > ( ( resolveAudio ) => {
6060 const createElement : typeof document . createElement = document . createElement . bind ( document ) ;
6161
6262 const elementCountOverrideBefore = document . querySelectorAll ( '*' ) . length ;
@@ -70,11 +70,11 @@ export const audioPromise = new Promise<HTMLAudioElement>((resolveAudio) => {
7070 ) {
7171 const element = createElement ( tagName , options ) ;
7272 // Spotify: <video>
73- // Deezer: <audio>
7473 if ( ( tagName === 'video' || tagName === 'audio' ) && ! audio ) {
75- if ( ! isProd ) console . log ( 'capture_audio_element' , audio ) ;
74+ if ( ! isProd ) console . log ( 'capture_audio_element' , element ) ;
7675 performance . measure ( 'capture_audio_element' ) ;
7776 audio = element as HTMLAudioElement ;
77+ handleAudio ( audio ) ;
7878 resolveAudio ( audio ) ;
7979 }
8080 return element ;
@@ -88,13 +88,29 @@ export const audioPromise = new Promise<HTMLAudioElement>((resolveAudio) => {
8888 const element = querySelector ( AUDIO_SELECTOR ) ;
8989 if ( element ) {
9090 audio = element as HTMLAudioElement ;
91+ handleAudio ( audio ) ;
9192 resolveAudio ( audio ) ;
9293 } else {
9394 setTimeout ( queryAudio , 100 ) ;
9495 }
9596 } ;
9697 queryAudio ( ) ;
9798
99+ if ( currentPlatform === 'DEEZER' ) {
100+ const A = Audio ;
101+ ( window as any ) . Audio = class {
102+ constructor ( src ?: string ) {
103+ const element = new A ( src ) ;
104+ element . addEventListener ( 'play' , ( ) => {
105+ audio = element ;
106+ handleAudio ( audio ) ;
107+ resolveAudio ( audio ) ;
108+ } ) ;
109+ return element ;
110+ }
111+ } ;
112+ }
113+
98114 // Apple music does not insert audio elements by default
99115 if ( currentPlatform !== 'APPLE' ) {
100116 // Check if audio is found normally
@@ -118,8 +134,15 @@ export const audioPromise = new Promise<HTMLAudioElement>((resolveAudio) => {
118134 }
119135} ) ;
120136
121- audioPromise . then ( ( audio ) => {
122- let reported = false ;
137+ /**
138+ * Some platforms (such as Deezer) keep changing Audio
139+ */
140+ export async function getCurrentAudio ( ) {
141+ return audio || firstAudioPromise ;
142+ }
143+
144+ let reported = false ;
145+ function handleAudio ( audio : HTMLAudioElement ) {
123146 audio . addEventListener ( 'playing' , async ( ) => {
124147 const isMusic = audio . duration && audio . duration > 2.6 * 60 && audio . duration < 4 * 60 ;
125148 if ( ! reported && isMusic && ! ( await getLyricsBtn ( ) ) ) {
@@ -134,30 +157,29 @@ audioPromise.then((audio) => {
134157 }
135158 } ) ;
136159
137- // safari not support media session, pip contorl video
138- // safari turning off pip will also cause the video to pause
139- let time = performance . now ( ) ;
140- lyricVideo . addEventListener ( 'pause' , ( ) => {
141- const now = performance . now ( ) ;
142- if ( now - time > 300 ) {
143- time = now ;
144- audio . pause ( ) ;
145- }
146- } ) ;
147- lyricVideo . addEventListener ( 'play' , ( ) => {
148- // video need't seek, because it is stream
149- audio . play ( ) ;
160+ audio . addEventListener ( 'play' , ( ) => {
161+ lyricVideo . play ( ) ;
162+ navigator . mediaSession . playbackState = 'playing' ;
150163 } ) ;
151164
152- if ( navigator . mediaSession ) {
153- const mediaSession = navigator . mediaSession ;
154- audio . addEventListener ( 'play' , ( ) => {
155- lyricVideo . play ( ) ;
156- mediaSession . playbackState = 'playing' ;
157- } ) ;
158- audio . addEventListener ( 'pause' , ( ) => {
159- lyricVideo . pause ( ) ;
160- mediaSession . playbackState = 'paused' ;
161- } ) ;
165+ audio . addEventListener ( 'pause' , ( ) => {
166+ lyricVideo . pause ( ) ;
167+ navigator . mediaSession . playbackState = 'paused' ;
168+ } ) ;
169+ }
170+
171+ // safari not support media session, pip control video
172+ // safari turning off pip will also cause the video to pause
173+ let time = performance . now ( ) ;
174+ lyricVideo . addEventListener ( 'pause' , ( ) => {
175+ const now = performance . now ( ) ;
176+ if ( now - time > 300 ) {
177+ time = now ;
178+ audio ?. pause ( ) ;
162179 }
163180} ) ;
181+
182+ lyricVideo . addEventListener ( 'play' , ( ) => {
183+ // video need't seek, because it is stream
184+ audio ?. play ( ) ;
185+ } ) ;
0 commit comments