l-min-components 1.0.1149 → 1.0.1151

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "l-min-components",
3
- "version": "1.0.1149",
3
+ "version": "1.0.1151",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "src/assets",
@@ -1,121 +1,38 @@
1
- import { useCallback, useContext, useEffect, useRef, useState } from "react";
1
+ import {
2
+ useCallback,
3
+ useContext,
4
+ useEffect,
5
+ useMemo,
6
+ useRef,
7
+ useState,
8
+ } from "react";
2
9
  import Hls from "hls.js";
3
10
  import { OutletContext } from "../AppMainLayout";
4
11
 
5
12
  const useAudioPlayer = ({ src, streamSrc }) => {
6
- const audioRef = useRef(null);
7
13
  const [isPlaying, setIsPlaying] = useState(false);
8
14
  const [isBuffering, setIsBuffering] = useState(false);
9
15
  const [isReady, setIsReady] = useState(false);
10
16
  const [error, setError] = useState(null);
11
17
  const [progress, setProgress] = useState({});
12
18
 
19
+ const audioRef = useRef(null);
20
+
13
21
  const { accessToken, generalData } = useContext(OutletContext);
14
22
 
15
23
  const accountId = generalData?.selectedAccount?.id;
16
24
 
17
- useEffect(() => {
18
- if (!audioRef.current) return;
19
- let hls;
20
-
21
- if (Hls?.isSupported() && streamSrc && !src) {
22
- hls = new Hls({
23
- xhrSetup: function (xhr, url) {
24
- const modifiedURL = `${url}?_account=${accountId}`;
25
- xhr.open("GET", modifiedURL, true);
26
- xhr.setRequestHeader("Authorization", `Bearer ${accessToken}`);
27
- },
28
- lowLatencyMode: true,
29
- });
30
-
31
- hls.loadSource(streamSrc);
32
- hls?.attachMedia(audioRef.current);
33
-
34
- hls.on(Hls?.Events?.MANIFEST_PARSED, () => {
35
- setIsReady(true);
36
- setError(null);
37
- });
38
- hls.on(Hls?.Events?.BUFFER_STALLED, () => {
39
- setIsBuffering(true);
40
- });
41
-
42
- hls.on(Hls?.Events?.BUFFER_APPENDED, () => {
43
- setIsBuffering(false);
44
- });
45
-
46
- hls.on(Hls.Events.ERROR, (event, data) => {
47
- setError(data);
48
- });
49
- } else if (
50
- !src &&
51
- audioRef.current.canPlayType("application/vnd.apple.mpegurl")
52
- ) {
53
- audioRef.current.src = streamSrc;
54
- audioRef.current.addEventListener("loadedmetadata", () => {
55
- setIsReady(true);
56
- });
57
- } else {
58
- if (audioRef.current && src) {
59
- audioRef.current.src = src;
60
- audioRef.current.load();
61
- setIsReady(true);
62
- }
63
- }
64
- const handleEnded = () => setIsPlaying(false);
65
-
66
- const handlePlaying = () => {
67
- setIsPlaying(true);
68
- };
69
- const handlePause = () => {
70
- setIsPlaying(false);
71
- };
72
-
73
- audioRef.current.addEventListener("ended", handleEnded);
74
- audioRef.current.addEventListener("playing", handlePlaying);
75
- audioRef.current.addEventListener("pause", handlePause);
76
-
77
- const handleProgress = () => {
78
- if (audioRef.current) {
79
- const currentTime = audioRef.current.currentTime;
80
- const duration = audioRef.current.duration;
81
-
82
- setProgress({ currentTime, duration });
83
- }
84
- };
85
- audioRef.current.addEventListener("timeupdate", handleProgress);
86
- audioRef.current.addEventListener("loadedmetadata", handleProgress);
87
- audioRef.current.addEventListener("waiting", () => setIsBuffering(true));
88
- audioRef.current.addEventListener("canplay", () => setIsBuffering(false));
89
-
90
- return () => {
91
- if (hls) {
92
- hls.destroy();
93
- }
94
- if (audioRef.current) {
95
- audioRef.current.removeEventListener("ended", handleEnded);
96
- audioRef.current.removeEventListener("playing", handlePlaying);
97
- audioRef.current.removeEventListener("pause", handlePause);
98
- audioRef.current.removeEventListener("timeupdate", handleProgress);
99
- audioRef.current.removeEventListener("loadedmetadata", handleProgress);
100
- audioRef.current.removeEventListener("waiting", () =>
101
- setIsBuffering(true)
102
- );
103
- audioRef.current.removeEventListener("canplay", () =>
104
- setIsBuffering(false)
105
- );
106
- }
107
- };
108
- }, [src, streamSrc, accessToken, accountId]);
109
-
110
25
  const play = useCallback(() => {
111
26
  if (audioRef.current) {
112
27
  audioRef.current.play();
28
+ setIsPlaying(true);
113
29
  }
114
30
  }, []);
115
31
 
116
32
  const pause = useCallback(() => {
117
33
  if (audioRef.current) {
118
34
  audioRef.current.pause();
35
+ setIsPlaying(false);
119
36
  }
120
37
  }, []);
121
38
 
@@ -123,9 +40,87 @@ const useAudioPlayer = ({ src, streamSrc }) => {
123
40
  if (audioRef.current) {
124
41
  audioRef.current.pause();
125
42
  audioRef.current.currentTime = 0;
43
+ setIsPlaying(false);
126
44
  }
127
45
  }, []);
128
46
 
47
+ const url = useMemo(() => src || streamSrc, [src, streamSrc]);
48
+
49
+ useEffect(() => {
50
+ let hls;
51
+
52
+ if (audioRef.current && accountId) {
53
+ if (Hls?.isSupported() && streamSrc && !src) {
54
+ hls = new Hls({
55
+ xhrSetup: function (xhr, url) {
56
+ const modifiedURL = `${url}?_account=${accountId}`;
57
+ xhr.open("GET", modifiedURL, true);
58
+ xhr.setRequestHeader("Authorization", `Bearer ${accessToken}`);
59
+ },
60
+ lowLatencyMode: true,
61
+ });
62
+
63
+ hls.loadSource(url);
64
+ hls?.attachMedia(audioRef.current);
65
+
66
+ hls.on(Hls?.Events?.MANIFEST_PARSED, () => {
67
+ setIsReady(true);
68
+ setError(null);
69
+ });
70
+ hls.on(Hls?.Events?.BUFFER_STALLED, () => {
71
+ setIsBuffering(true);
72
+ });
73
+
74
+ hls.on(Hls?.Events?.BUFFER_APPENDED, () => {
75
+ setIsBuffering(false);
76
+ });
77
+
78
+ hls.on(Hls.Events.ERROR, (event, data) => {
79
+ setError(data);
80
+ });
81
+ } else if (
82
+ !src &&
83
+ audioRef.current.canPlayType("application/vnd.apple.mpegurl")
84
+ ) {
85
+ audioRef.current.src = url;
86
+ audioRef.current.addEventListener("loadedmetadata", () => {
87
+ setIsReady(true);
88
+ });
89
+ } else {
90
+ if (audioRef.current && src) {
91
+ audioRef.current.src = src;
92
+ audioRef.current.load();
93
+ setIsReady(true);
94
+ }
95
+ }
96
+ const handleEnded = () => setIsPlaying(false);
97
+
98
+ audioRef.current.addEventListener("ended", handleEnded);
99
+
100
+ const handleProgress = () => {
101
+ if (audioRef.current) {
102
+ const currentTime = audioRef.current.currentTime;
103
+ const duration = audioRef.current.duration;
104
+
105
+ setProgress({ currentTime, duration });
106
+ }
107
+ };
108
+ audioRef.current?.addEventListener("timeupdate", handleProgress);
109
+ audioRef.current?.addEventListener("loadedmetadata", handleProgress);
110
+ audioRef.current?.addEventListener("waiting", () => setIsBuffering(true));
111
+ audioRef.current?.addEventListener("canplay", () =>
112
+ setIsBuffering(false)
113
+ );
114
+ }
115
+ return () => {
116
+ if (hls) {
117
+ hls.destroy();
118
+ }
119
+
120
+ setIsReady(false);
121
+ };
122
+ }, [url, accessToken, accountId, audioRef.current]);
123
+
129
124
  return {
130
125
  stop,
131
126
  audioRef,