apm-react-audio-player 1.0.2 → 1.0.3

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.
Files changed (2) hide show
  1. package/dist/index.js +62 -9
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -7,6 +7,35 @@ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'defau
7
7
  var React = require('react');
8
8
  var React__default = _interopDefault(React);
9
9
 
10
+ var Play = function Play() {
11
+ return /*#__PURE__*/React__default.createElement("svg", {
12
+ stroke: "currentColor",
13
+ fill: "currentColor",
14
+ strokeWidth: "0",
15
+ viewBox: "0 0 448 512",
16
+ className: "play",
17
+ height: "1em",
18
+ width: "1em",
19
+ xmlns: "http://www.w3.org/2000/svg"
20
+ }, /*#__PURE__*/React__default.createElement("path", {
21
+ d: "M424.4 214.7L72.4 6.6C43.8-10.3 0 6.1 0 47.9V464c0 37.5 40.7 60.1 72.4 41.3l352-208c31.4-18.5 31.5-64.1 0-82.6z"
22
+ }));
23
+ };
24
+
25
+ var Pause = function Pause() {
26
+ return /*#__PURE__*/React__default.createElement("svg", {
27
+ stroke: "currentColor",
28
+ fill: "currentColor",
29
+ strokeWidth: "0",
30
+ viewBox: "0 0 448 512",
31
+ height: "1em",
32
+ width: "1em",
33
+ xmlns: "http://www.w3.org/2000/svg"
34
+ }, /*#__PURE__*/React__default.createElement("path", {
35
+ d: "M144 479H48c-26.5 0-48-21.5-48-48V79c0-26.5 21.5-48 48-48h96c26.5 0 48 21.5 48 48v352c0 26.5-21.5 48-48 48zm304-48V79c0-26.5-21.5-48-48-48h-96c-26.5 0-48 21.5-48 48v352c0 26.5 21.5 48 48 48h96c26.5 0 48-21.5 48-48z"
36
+ }));
37
+ };
38
+
10
39
  function _iterableToArrayLimit(arr, i) {
11
40
  var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"];
12
41
  if (null != _i) {
@@ -70,6 +99,10 @@ var useAudioPlayer = function useAudioPlayer(audioRef, progressBarRef) {
70
99
  _useState6 = _slicedToArray(_useState5, 2),
71
100
  currentTime = _useState6[0],
72
101
  setCurrentTime = _useState6[1];
102
+ var _useState7 = React.useState(false),
103
+ _useState8 = _slicedToArray(_useState7, 2),
104
+ isFinishedPlaying = _useState8[0],
105
+ setIsFinishedPlaying = _useState8[1];
73
106
  var animationRef = React.useRef(); // reference the animation
74
107
 
75
108
  var onLoadedMetadata = function onLoadedMetadata() {
@@ -88,11 +121,13 @@ var useAudioPlayer = function useAudioPlayer(audioRef, progressBarRef) {
88
121
  // when you reach the end of the song
89
122
  if (progressBarRef.current.value === duration) {
90
123
  restart();
124
+ setIsFinishedPlaying(true);
91
125
  return;
92
126
  }
93
127
  animationRef.current = window.requestAnimationFrame(whilePlaying);
94
128
  };
95
129
  var pause = function pause() {
130
+ setIsPlaying(false);
96
131
  audioRef.current.pause();
97
132
  window.cancelAnimationFrame(animationRef.current);
98
133
  };
@@ -102,6 +137,7 @@ var useAudioPlayer = function useAudioPlayer(audioRef, progressBarRef) {
102
137
  pause();
103
138
  };
104
139
  var play = function play() {
140
+ setIsPlaying(true);
105
141
  audioRef.current.play();
106
142
  animationRef.current = window.requestAnimationFrame(whilePlaying);
107
143
  };
@@ -113,9 +149,7 @@ var useAudioPlayer = function useAudioPlayer(audioRef, progressBarRef) {
113
149
  return "".concat(returnedMinutes, ":").concat(returnedSeconds);
114
150
  };
115
151
  var togglePlaying = function togglePlaying() {
116
- var prevState = isPlaying;
117
- setIsPlaying(!prevState);
118
- if (!prevState) {
152
+ if (!isPlaying) {
119
153
  play();
120
154
  } else {
121
155
  pause();
@@ -145,15 +179,17 @@ var useAudioPlayer = function useAudioPlayer(audioRef, progressBarRef) {
145
179
  backThirty: backThirty,
146
180
  forwardThirty: forwardThirty,
147
181
  isPlaying: isPlaying,
182
+ isFinished: isFinishedPlaying,
148
183
  currentTime: currentTime,
149
184
  duration: duration
150
185
  };
151
186
  };
152
187
 
153
188
  var ReactAudioPlayer = function ReactAudioPlayer(props) {
189
+ var _props$audioPlayerRef, _props$progressBarRef;
154
190
  // references
155
- var audioPlayer = React.useRef(); // reference our audio component
156
- var progressBar = React.useRef(); // reference our progress bar
191
+ var audioPlayerRef = (_props$audioPlayerRef = props.audioPlayerRef) !== null && _props$audioPlayerRef !== void 0 ? _props$audioPlayerRef : React.useRef(); // reference our audio component
192
+ var progressBarRef = (_props$progressBarRef = props.progressBarRef) !== null && _props$progressBarRef !== void 0 ? _props$progressBarRef : React.useRef(); // reference our progress bar
157
193
 
158
194
  var customStyles = props ? props.style : '';
159
195
  var title = props.title,
@@ -161,15 +197,31 @@ var ReactAudioPlayer = function ReactAudioPlayer(props) {
161
197
  audioSrc = props.audioSrc;
162
198
 
163
199
  // hooks
164
- var _useAudioPlayer = useAudioPlayer(audioPlayer, progressBar),
200
+ var _useAudioPlayer = useAudioPlayer(audioPlayerRef, progressBarRef),
201
+ onLoadedMetadata = _useAudioPlayer.onLoadedMetadata,
165
202
  calculateTime = _useAudioPlayer.calculateTime,
203
+ togglePlaying = _useAudioPlayer.togglePlaying,
166
204
  changePlayerCurrentTime = _useAudioPlayer.changePlayerCurrentTime,
205
+ isPlaying = _useAudioPlayer.isPlaying,
167
206
  currentTime = _useAudioPlayer.currentTime,
168
207
  duration = _useAudioPlayer.duration;
169
208
  return audioSrc && /*#__PURE__*/React__default.createElement("div", {
170
209
  className: "audioPlayer",
171
210
  style: customStyles && customStyles.audioPlayer
172
- }, "hi", /*#__PURE__*/React__default.createElement("div", {
211
+ }, /*#__PURE__*/React__default.createElement("audio", {
212
+ ref: audioPlayerRef,
213
+ src: audioSrc,
214
+ preload: "metadata",
215
+ onLoadedMetadata: onLoadedMetadata
216
+ }), /*#__PURE__*/React__default.createElement("div", {
217
+ className: "playPauseContainer"
218
+ }, /*#__PURE__*/React__default.createElement("button", {
219
+ onClick: togglePlaying,
220
+ className: "playPause",
221
+ style: customStyles && customStyles.playPause
222
+ }, isPlaying ? /*#__PURE__*/React__default.createElement(Pause, null) : /*#__PURE__*/React__default.createElement(Play, {
223
+ className: "play"
224
+ }))), /*#__PURE__*/React__default.createElement("div", {
173
225
  className: "currentTime"
174
226
  }, calculateTime(currentTime)), /*#__PURE__*/React__default.createElement("div", {
175
227
  className: "progressBarContainer"
@@ -177,7 +229,7 @@ var ReactAudioPlayer = function ReactAudioPlayer(props) {
177
229
  type: "range",
178
230
  className: "progressBar",
179
231
  defaultValue: "0",
180
- ref: progressBar,
232
+ ref: progressBarRef,
181
233
  onChange: changePlayerCurrentTime
182
234
  })), /*#__PURE__*/React__default.createElement("div", {
183
235
  className: "duration",
@@ -188,7 +240,8 @@ var ReactAudioPlayer = function ReactAudioPlayer(props) {
188
240
  className: "textBoxTitle"
189
241
  }, " ", title || '', " "), /*#__PURE__*/React__default.createElement("div", {
190
242
  className: "textBoxDescription"
191
- }, " ", description || '', " ")), ' ', "*/}");
243
+ }, " ", description || '', " ")));
192
244
  };
193
245
 
194
246
  exports.ReactAudioPlayer = ReactAudioPlayer;
247
+ exports.useAudioPlayer = useAudioPlayer;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apm-react-audio-player",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "author": "Jason Phan <jphan@mpr.org>",
5
5
  "license": "MIT",
6
6
  "private": false,