Skip to content

Commit 015dd4b

Browse files
committed
refactor: correct audio player
1 parent a3762e5 commit 015dd4b

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/root/contexts/audio-player/index.tsx

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createContext, FC, memo, ReactElement, useCallback, useEffect, useState } from 'react';
22
import { AudioType, ContextType } from './types/context.type.ts';
33
import { FileExtensionEnum } from '../../types/files/types.ts';
4+
import image from '../../../../public/assets/icons/512.png';
45

56
export const AudioPlayerContext = createContext<ContextType | null>(null);
67

@@ -45,26 +46,29 @@ export const AudioPlayer: FC<{ children: ReactElement }> = memo(({ children }) =
4546
navigator.mediaSession.metadata = new MediaMetadata({
4647
title,
4748
artist: 'PassimX',
48-
artwork: [{ src: '/assets/icons/512.png', sizes: '512x512', type: 'image/png' }],
49+
artwork: [{ src: image, sizes: '512x512', type: 'image/png' }],
4950
});
5051

5152
navigator.mediaSession.setActionHandler('play', play);
5253
navigator.mediaSession.setActionHandler('pause', pause);
5354

5455
navigator.mediaSession.setActionHandler('seekto', (details) => {
5556
if (!audioEl) return;
56-
57-
// если аудио на паузе — включаем, перематываем, потом оставляем на паузе (для ios)
58-
const wasPaused = audioEl.paused;
59-
if (wasPaused) {
60-
const originalVolume = audioEl.volume;
61-
audioEl.volume = 0;
62-
audioEl?.play().then(() => {
63-
audioEl!.currentTime = details.seekTime!;
64-
if (wasPaused) audioEl!.pause();
65-
audioEl!.volume = originalVolume;
66-
});
67-
} else audioEl.currentTime = details.seekTime!;
57+
const originalVolume = audioEl.volume;
58+
try {
59+
// если аудио на паузе — включаем, перематываем, потом оставляем на паузе (для ios)
60+
const wasPaused = audioEl.paused;
61+
if (wasPaused) {
62+
audioEl.volume = 0;
63+
audioEl?.play().then(() => {
64+
audioEl!.currentTime = details.seekTime!;
65+
if (wasPaused) audioEl!.pause();
66+
audioEl!.volume = originalVolume;
67+
});
68+
} else audioEl.currentTime = details.seekTime!;
69+
} catch (e) {
70+
audioEl!.volume = originalVolume;
71+
}
6872
});
6973

7074
navigator.mediaSession.setActionHandler('previoustrack', () => {

0 commit comments

Comments
 (0)