apm-react-audio-player 1.1.0 → 1.1.2

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/dist/index.js CHANGED
@@ -27,15 +27,15 @@ function _iterableToArrayLimit(r, l) {
27
27
  i,
28
28
  u,
29
29
  a = [],
30
- f = true,
31
- o = false;
30
+ f = !0,
31
+ o = !1;
32
32
  try {
33
33
  if (i = (t = t.call(r)).next, 0 === l) {
34
34
  if (Object(t) !== t) return;
35
35
  f = !1;
36
36
  } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);
37
37
  } catch (r) {
38
- o = true, n = r;
38
+ o = !0, n = r;
39
39
  } finally {
40
40
  try {
41
41
  if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return;
@@ -104,6 +104,13 @@ var useAudioPlayer = function useAudioPlayer(audioRef, progressBarRef, volumeCtr
104
104
  progressBarRef.current.max = seconds;
105
105
  }
106
106
  };
107
+ var resetDuration = function resetDuration() {
108
+ setDuration(undefined);
109
+ setCurrentTime(0);
110
+ if (progressBarRef.current) {
111
+ progressBarRef.current.value = 0;
112
+ }
113
+ };
107
114
  var updateCurrentTime = function updateCurrentTime() {
108
115
  if (progressBarRef.current) {
109
116
  setCurrentTime(progressBarRef.current.value);
@@ -114,9 +121,10 @@ var useAudioPlayer = function useAudioPlayer(audioRef, progressBarRef, volumeCtr
114
121
  if (!progressBarRef.current || !audioRef.current) {
115
122
  return;
116
123
  }
117
- if (audioRef.current.duration !== Infinity) {
124
+ var liveDuration = audioRef.current.duration;
125
+ if (liveDuration !== Infinity) {
118
126
  progressBarRef.current.value = Math.floor(audioRef.current.currentTime);
119
- progressBarRef.current.style.setProperty('--seek-before-width', "".concat(progressBarRef.current.value / duration * 100, "%"));
127
+ progressBarRef.current.style.setProperty('--seek-before-width', liveDuration > 0 ? "".concat(progressBarRef.current.value / liveDuration * 100, "%") : '0%');
120
128
  }
121
129
  updateCurrentTime();
122
130
 
@@ -143,6 +151,17 @@ var useAudioPlayer = function useAudioPlayer(audioRef, progressBarRef, volumeCtr
143
151
  var play = function play() {
144
152
  setIsPlaying(true);
145
153
  setIsFinishedPlaying(false);
154
+
155
+ // For live streams (duration === Infinity), reset the audio element before
156
+ // playing. The audioSrc useEffect in ReactAudioPlayerInner calls load() on
157
+ // mount, which causes the browser to pre-buffer the HLS stream. By the time
158
+ // the user clicks play the manifest's seekable window has moved forward and
159
+ // old segments are gone, so the browser auto-seeks to the earliest available
160
+ // position (e.g. 20s in), causing ads to start mid-stream. Calling load()
161
+ // here reconnects to the current live edge and gets a fresh manifest.
162
+ if (duration === Infinity) {
163
+ audioRef.current.load();
164
+ }
146
165
  audioRef.current.play();
147
166
 
148
167
  // Only start RAF loop for non-live streams with valid duration
@@ -172,7 +191,8 @@ var useAudioPlayer = function useAudioPlayer(audioRef, progressBarRef, volumeCtr
172
191
  if (!progressBarRef.current || !audioRef.current) return;
173
192
  audioRef.current.currentTime = progressBarRef.current.value;
174
193
  setCurrentTime(progressBarRef.current.value);
175
- progressBarRef.current.style.setProperty('--seek-before-width', "".concat(progressBarRef.current.value / duration * 100, "%"));
194
+ var liveDuration = audioRef.current.duration;
195
+ progressBarRef.current.style.setProperty('--seek-before-width', liveDuration > 0 ? "".concat(progressBarRef.current.value / liveDuration * 100, "%") : '0%');
176
196
  };
177
197
  var changeRange = function changeRange() {
178
198
  if (!progressBarRef.current || !audioRef.current) return;
@@ -215,6 +235,7 @@ var useAudioPlayer = function useAudioPlayer(audioRef, progressBarRef, volumeCtr
215
235
  };
216
236
  return {
217
237
  onLoadedMetadata: onLoadedMetadata,
238
+ resetDuration: resetDuration,
218
239
  calculateTime: calculateTime,
219
240
  togglePlaying: togglePlaying,
220
241
  changePlayerCurrentTime: changePlayerCurrentTime,
@@ -306,6 +327,7 @@ var ReactAudioPlayerInner = function ReactAudioPlayerInner(props) {
306
327
  playBtnClass = props.playBtnClass,
307
328
  customHtml = props.customHtml,
308
329
  onLoadedMetadata = props.onLoadedMetadata,
330
+ resetDuration = props.resetDuration,
309
331
  calculateTime = props.calculateTime,
310
332
  togglePlaying = props.togglePlaying,
311
333
  changePlayerCurrentTime = props.changePlayerCurrentTime,
@@ -327,6 +349,7 @@ var ReactAudioPlayerInner = function ReactAudioPlayerInner(props) {
327
349
  // Use JSON.stringify to handle array comparisons by value instead of reference
328
350
  React.useEffect(function () {
329
351
  if (audioPlayerRef.current && audioSrc) {
352
+ resetDuration === null || resetDuration === void 0 ? void 0 : resetDuration();
330
353
  try {
331
354
  audioPlayerRef.current.load();
332
355
  } catch (err) {
@@ -25,15 +25,15 @@ function _iterableToArrayLimit(r, l) {
25
25
  i,
26
26
  u,
27
27
  a = [],
28
- f = true,
29
- o = false;
28
+ f = !0,
29
+ o = !1;
30
30
  try {
31
31
  if (i = (t = t.call(r)).next, 0 === l) {
32
32
  if (Object(t) !== t) return;
33
33
  f = !1;
34
34
  } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);
35
35
  } catch (r) {
36
- o = true, n = r;
36
+ o = !0, n = r;
37
37
  } finally {
38
38
  try {
39
39
  if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return;
@@ -102,6 +102,13 @@ var useAudioPlayer = function useAudioPlayer(audioRef, progressBarRef, volumeCtr
102
102
  progressBarRef.current.max = seconds;
103
103
  }
104
104
  };
105
+ var resetDuration = function resetDuration() {
106
+ setDuration(undefined);
107
+ setCurrentTime(0);
108
+ if (progressBarRef.current) {
109
+ progressBarRef.current.value = 0;
110
+ }
111
+ };
105
112
  var updateCurrentTime = function updateCurrentTime() {
106
113
  if (progressBarRef.current) {
107
114
  setCurrentTime(progressBarRef.current.value);
@@ -112,9 +119,10 @@ var useAudioPlayer = function useAudioPlayer(audioRef, progressBarRef, volumeCtr
112
119
  if (!progressBarRef.current || !audioRef.current) {
113
120
  return;
114
121
  }
115
- if (audioRef.current.duration !== Infinity) {
122
+ var liveDuration = audioRef.current.duration;
123
+ if (liveDuration !== Infinity) {
116
124
  progressBarRef.current.value = Math.floor(audioRef.current.currentTime);
117
- progressBarRef.current.style.setProperty('--seek-before-width', "".concat(progressBarRef.current.value / duration * 100, "%"));
125
+ progressBarRef.current.style.setProperty('--seek-before-width', liveDuration > 0 ? "".concat(progressBarRef.current.value / liveDuration * 100, "%") : '0%');
118
126
  }
119
127
  updateCurrentTime();
120
128
 
@@ -141,6 +149,17 @@ var useAudioPlayer = function useAudioPlayer(audioRef, progressBarRef, volumeCtr
141
149
  var play = function play() {
142
150
  setIsPlaying(true);
143
151
  setIsFinishedPlaying(false);
152
+
153
+ // For live streams (duration === Infinity), reset the audio element before
154
+ // playing. The audioSrc useEffect in ReactAudioPlayerInner calls load() on
155
+ // mount, which causes the browser to pre-buffer the HLS stream. By the time
156
+ // the user clicks play the manifest's seekable window has moved forward and
157
+ // old segments are gone, so the browser auto-seeks to the earliest available
158
+ // position (e.g. 20s in), causing ads to start mid-stream. Calling load()
159
+ // here reconnects to the current live edge and gets a fresh manifest.
160
+ if (duration === Infinity) {
161
+ audioRef.current.load();
162
+ }
144
163
  audioRef.current.play();
145
164
 
146
165
  // Only start RAF loop for non-live streams with valid duration
@@ -170,7 +189,8 @@ var useAudioPlayer = function useAudioPlayer(audioRef, progressBarRef, volumeCtr
170
189
  if (!progressBarRef.current || !audioRef.current) return;
171
190
  audioRef.current.currentTime = progressBarRef.current.value;
172
191
  setCurrentTime(progressBarRef.current.value);
173
- progressBarRef.current.style.setProperty('--seek-before-width', "".concat(progressBarRef.current.value / duration * 100, "%"));
192
+ var liveDuration = audioRef.current.duration;
193
+ progressBarRef.current.style.setProperty('--seek-before-width', liveDuration > 0 ? "".concat(progressBarRef.current.value / liveDuration * 100, "%") : '0%');
174
194
  };
175
195
  var changeRange = function changeRange() {
176
196
  if (!progressBarRef.current || !audioRef.current) return;
@@ -213,6 +233,7 @@ var useAudioPlayer = function useAudioPlayer(audioRef, progressBarRef, volumeCtr
213
233
  };
214
234
  return {
215
235
  onLoadedMetadata: onLoadedMetadata,
236
+ resetDuration: resetDuration,
216
237
  calculateTime: calculateTime,
217
238
  togglePlaying: togglePlaying,
218
239
  changePlayerCurrentTime: changePlayerCurrentTime,
@@ -304,6 +325,7 @@ var ReactAudioPlayerInner = function ReactAudioPlayerInner(props) {
304
325
  playBtnClass = props.playBtnClass,
305
326
  customHtml = props.customHtml,
306
327
  onLoadedMetadata = props.onLoadedMetadata,
328
+ resetDuration = props.resetDuration,
307
329
  calculateTime = props.calculateTime,
308
330
  togglePlaying = props.togglePlaying,
309
331
  changePlayerCurrentTime = props.changePlayerCurrentTime,
@@ -325,6 +347,7 @@ var ReactAudioPlayerInner = function ReactAudioPlayerInner(props) {
325
347
  // Use JSON.stringify to handle array comparisons by value instead of reference
326
348
  useEffect(function () {
327
349
  if (audioPlayerRef.current && audioSrc) {
350
+ resetDuration === null || resetDuration === void 0 ? void 0 : resetDuration();
328
351
  try {
329
352
  audioPlayerRef.current.load();
330
353
  } catch (err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apm-react-audio-player",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "author": "Jason Phan <jphan@mpr.org>",
5
5
  "license": "MIT",
6
6
  "private": false,
@@ -27,9 +27,6 @@
27
27
  "prettier:fix": "prettier --check '**/**.js' --write",
28
28
  "clean": "rm -rf node_modules dist package-lock.json yarn.lock"
29
29
  },
30
- "assert": {
31
- "type": "json"
32
- },
33
30
  "peerDependencies": {
34
31
  "react": "^16.0.0"
35
32
  },
@@ -85,6 +82,8 @@
85
82
  ],
86
83
  "jest": {
87
84
  "testEnvironment": "jsdom",
88
- "setupFilesAfterEnv": ["<rootDir>/jest.setup.js"]
85
+ "setupFilesAfterEnv": [
86
+ "<rootDir>/jest.setup.js"
87
+ ]
89
88
  }
90
89
  }