apm-react-audio-player 1.0.0

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 (3) hide show
  1. package/README.md +107 -0
  2. package/dist/index.js +221 -0
  3. package/package.json +68 -0
package/README.md ADDED
@@ -0,0 +1,107 @@
1
+ # apm-react-audio-player
2
+
3
+ This is a light react audio player that is wrapped around a HTML5 audio tag, created for use on American Public Media and Minnesota Public Radio's websites.
4
+
5
+ The library was designed to add a audio player to a body of a story which will not trigger the static audio player.
6
+
7
+ ![audio player img](/public/audioPlayerImg.jpg)
8
+
9
+ ## Table of Contents
10
+
11
+ [Dependencies](#dependencies)
12
+
13
+ [Installation](#installation)
14
+ - [NPM](#npm)
15
+ - [YARN](#yarn)
16
+
17
+ [Importing](#importing)
18
+ - [ES6 Import](#es6-import)
19
+
20
+ [Usage](#usage)
21
+ - [Props](#props)
22
+ - [Example](#example)
23
+
24
+ [License] (#License)
25
+
26
+
27
+ ## Dependencies
28
+
29
+ As of version 1.0.0, this library has no dependencies for usage.
30
+
31
+ ## Installation
32
+
33
+ There are several ways to install APM Player on your site.
34
+
35
+ ### NPM
36
+
37
+ ```sh
38
+ npm install apm-react-audio-player
39
+ ```
40
+
41
+ or to use yarn:
42
+
43
+ ### YARN
44
+
45
+ ```sh
46
+ yarn add apm-react-audio-player
47
+ ```
48
+
49
+ ## Importing
50
+
51
+ ### ES6 Import
52
+
53
+ The easiest way to include this in modern javascript, assuming you are using something like and Babel, is to use an `import` statement.
54
+ The library uses named exports for all modules.
55
+
56
+ To import the player module:
57
+
58
+ ```javascript
59
+ import { ReactAudioPlayer } from 'apm-react-audio-player';
60
+ ```
61
+ ## Usage
62
+
63
+ ### Props
64
+ See the [audio tag documentation](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio) for detailed explanations of these attributes.
65
+
66
+ Prop | Type | Default | Notes
67
+ --- | --- | --- | ---
68
+ `title` | String | *empty string* | ---
69
+ `description` | String | *empty string* | ---
70
+ `url` | String | *empty string* | ---
71
+ `customStyles` | Object | --- | ---
72
+ `forwardBackward` | Boolean | false | ---
73
+
74
+ ### Example
75
+
76
+ ```javascript
77
+ class Example extends Component {
78
+ const url='example.mp3'
79
+ const audioStyles = {
80
+ audioPlayer: {
81
+ width: '1200px',
82
+ backgroundColor: 'lightBlue'
83
+ },
84
+ playPause: {
85
+ background: 'blue'
86
+ },
87
+ duration: {
88
+ color: '#26c9c3'
89
+ }
90
+ };
91
+ render() {
92
+ return (
93
+ <ReactAudioPlayer
94
+ title={'title'}
95
+ description={'description'}
96
+ audioSrc={url}
97
+ audioStyles={audioStyles}
98
+ forwardBackward={true}
99
+ />
100
+ )
101
+ }
102
+ }
103
+ ```
104
+
105
+ ## License
106
+
107
+ MIT © [Phanx091](https://github.com/Phanx091)
package/dist/index.js ADDED
@@ -0,0 +1,221 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
+
7
+ var React = require('react');
8
+ var React__default = _interopDefault(React);
9
+ var bs = require('react-icons/bs');
10
+ var fa = require('react-icons/fa');
11
+
12
+ function _iterableToArrayLimit(arr, i) {
13
+ var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"];
14
+ if (null != _i) {
15
+ var _s,
16
+ _e,
17
+ _x,
18
+ _r,
19
+ _arr = [],
20
+ _n = !0,
21
+ _d = !1;
22
+ try {
23
+ if (_x = (_i = _i.call(arr)).next, 0 === i) {
24
+ if (Object(_i) !== _i) return;
25
+ _n = !1;
26
+ } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0);
27
+ } catch (err) {
28
+ _d = !0, _e = err;
29
+ } finally {
30
+ try {
31
+ if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return;
32
+ } finally {
33
+ if (_d) throw _e;
34
+ }
35
+ }
36
+ return _arr;
37
+ }
38
+ }
39
+ function _slicedToArray(arr, i) {
40
+ return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
41
+ }
42
+ function _arrayWithHoles(arr) {
43
+ if (Array.isArray(arr)) return arr;
44
+ }
45
+ function _unsupportedIterableToArray(o, minLen) {
46
+ if (!o) return;
47
+ if (typeof o === "string") return _arrayLikeToArray(o, minLen);
48
+ var n = Object.prototype.toString.call(o).slice(8, -1);
49
+ if (n === "Object" && o.constructor) n = o.constructor.name;
50
+ if (n === "Map" || n === "Set") return Array.from(o);
51
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
52
+ }
53
+ function _arrayLikeToArray(arr, len) {
54
+ if (len == null || len > arr.length) len = arr.length;
55
+ for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
56
+ return arr2;
57
+ }
58
+ function _nonIterableRest() {
59
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
60
+ }
61
+
62
+ var useAudioPlayer = function useAudioPlayer(audioRef, progressBarRef) {
63
+ var _useState = React.useState(false),
64
+ _useState2 = _slicedToArray(_useState, 2),
65
+ isPlaying = _useState2[0],
66
+ setIsPlaying = _useState2[1];
67
+ var _useState3 = React.useState(0),
68
+ _useState4 = _slicedToArray(_useState3, 2),
69
+ duration = _useState4[0],
70
+ setDuration = _useState4[1];
71
+ var _useState5 = React.useState(0),
72
+ _useState6 = _slicedToArray(_useState5, 2),
73
+ currentTime = _useState6[0],
74
+ setCurrentTime = _useState6[1];
75
+ var animationRef = React.useRef(); // reference the animation
76
+
77
+ var onLoadedMetadata = function onLoadedMetadata() {
78
+ var seconds = Math.floor(audioRef.current.duration);
79
+ setDuration(seconds);
80
+ progressBarRef.current.max = seconds;
81
+ };
82
+ var updateCurrentTime = function updateCurrentTime() {
83
+ setCurrentTime(progressBarRef.current.value);
84
+ };
85
+ var whilePlaying = function whilePlaying() {
86
+ progressBarRef.current.value = Math.floor(audioRef.current.currentTime);
87
+ progressBarRef.current.style.setProperty('--seek-before-width', "".concat(progressBarRef.current.value / duration * 100, "%"));
88
+ updateCurrentTime();
89
+
90
+ // when you reach the end of the song
91
+ if (progressBarRef.current.value === duration) {
92
+ restart();
93
+ return;
94
+ }
95
+ animationRef.current = window.requestAnimationFrame(whilePlaying);
96
+ };
97
+ var pause = function pause() {
98
+ audioRef.current.pause();
99
+ window.cancelAnimationFrame(animationRef.current);
100
+ };
101
+ var restart = function restart() {
102
+ progressBarRef.current.value = 0;
103
+ updateCurrentTime();
104
+ pause();
105
+ };
106
+ var play = function play() {
107
+ audioRef.current.play();
108
+ animationRef.current = window.requestAnimationFrame(whilePlaying);
109
+ };
110
+ var calculateTime = function calculateTime(secs) {
111
+ var minutes = Math.floor(secs / 60);
112
+ var returnedMinutes = minutes < 10 ? "0".concat(minutes) : "".concat(minutes);
113
+ var seconds = Math.floor(secs % 60);
114
+ var returnedSeconds = seconds < 10 ? "0".concat(seconds) : "".concat(seconds);
115
+ return "".concat(returnedMinutes, ":").concat(returnedSeconds);
116
+ };
117
+ var togglePlaying = function togglePlaying() {
118
+ var prevState = isPlaying;
119
+ setIsPlaying(!prevState);
120
+ if (!prevState) {
121
+ play();
122
+ } else {
123
+ pause();
124
+ }
125
+ };
126
+ var changePlayerCurrentTime = function changePlayerCurrentTime() {
127
+ audioRef.current.currentTime = progressBarRef.current.value;
128
+ setCurrentTime(progressBarRef.current.value);
129
+ progressBarRef.current.style.setProperty('--seek-before-width', "".concat(progressBarRef.current.value / duration * 100, "%"));
130
+ };
131
+ var timeTravel = function timeTravel(newTime) {
132
+ progressBarRef.current.value = newTime;
133
+ updateCurrentTime();
134
+ changePlayerCurrentTime();
135
+ };
136
+ var backThirty = function backThirty() {
137
+ timeTravel(Number(progressBarRef.current.value) - 30);
138
+ };
139
+ var forwardThirty = function forwardThirty() {
140
+ timeTravel(Number(progressBarRef.current.value) + 30);
141
+ };
142
+ return {
143
+ onLoadedMetadata: onLoadedMetadata,
144
+ calculateTime: calculateTime,
145
+ togglePlaying: togglePlaying,
146
+ changePlayerCurrentTime: changePlayerCurrentTime,
147
+ backThirty: backThirty,
148
+ forwardThirty: forwardThirty,
149
+ isPlaying: isPlaying,
150
+ currentTime: currentTime,
151
+ duration: duration
152
+ };
153
+ };
154
+
155
+ var ReactAudioPlayer = function ReactAudioPlayer(props) {
156
+ // references
157
+ var audioPlayer = React.useRef(); // reference our audio component
158
+ var progressBar = React.useRef(); // reference our progress bar
159
+
160
+ var customStyles = props ? props.style : '';
161
+ var title = props.title,
162
+ description = props.description,
163
+ audioSrc = props.audioSrc,
164
+ forwardBackward = props.forwardBackward;
165
+
166
+ // hooks
167
+ var _useAudioPlayer = useAudioPlayer(audioPlayer, progressBar),
168
+ onLoadedMetadata = _useAudioPlayer.onLoadedMetadata,
169
+ calculateTime = _useAudioPlayer.calculateTime,
170
+ togglePlaying = _useAudioPlayer.togglePlaying,
171
+ changePlayerCurrentTime = _useAudioPlayer.changePlayerCurrentTime,
172
+ backThirty = _useAudioPlayer.backThirty,
173
+ forwardThirty = _useAudioPlayer.forwardThirty,
174
+ isPlaying = _useAudioPlayer.isPlaying,
175
+ currentTime = _useAudioPlayer.currentTime,
176
+ duration = _useAudioPlayer.duration;
177
+ return audioSrc && /*#__PURE__*/React__default.createElement("div", {
178
+ className: "audioPlayer",
179
+ style: customStyles && customStyles.audioPlayer
180
+ }, /*#__PURE__*/React__default.createElement("audio", {
181
+ ref: audioPlayer,
182
+ src: audioSrc,
183
+ preload: "metadata",
184
+ onLoadedMetadata: onLoadedMetadata
185
+ }), forwardBackward && /*#__PURE__*/React__default.createElement("button", {
186
+ className: "forwardBackward",
187
+ onClick: backThirty
188
+ }, "30", /*#__PURE__*/React__default.createElement(bs.BsArrowLeftShort, null)), /*#__PURE__*/React__default.createElement("div", {
189
+ className: "playPauseContainer"
190
+ }, /*#__PURE__*/React__default.createElement("button", {
191
+ onClick: togglePlaying,
192
+ className: "playPause",
193
+ style: customStyles && customStyles.playPause
194
+ }, isPlaying ? /*#__PURE__*/React__default.createElement(fa.FaPause, null) : /*#__PURE__*/React__default.createElement(fa.FaPlay, {
195
+ className: "play"
196
+ }))), forwardBackward && /*#__PURE__*/React__default.createElement("button", {
197
+ className: "forwardBackward",
198
+ onClick: forwardThirty
199
+ }, /*#__PURE__*/React__default.createElement(bs.BsArrowRightShort, null), "30"), /*#__PURE__*/React__default.createElement("div", {
200
+ className: "currentTime"
201
+ }, calculateTime(currentTime)), /*#__PURE__*/React__default.createElement("div", {
202
+ className: "progressBarContainer"
203
+ }, /*#__PURE__*/React__default.createElement("input", {
204
+ type: "range",
205
+ className: "progressBar",
206
+ defaultValue: "0",
207
+ ref: progressBar,
208
+ onChange: changePlayerCurrentTime
209
+ })), /*#__PURE__*/React__default.createElement("div", {
210
+ className: "duration",
211
+ style: customStyles && customStyles.duration
212
+ }, duration && !isNaN(duration) && calculateTime(duration)), /*#__PURE__*/React__default.createElement("div", {
213
+ className: "textBox"
214
+ }, /*#__PURE__*/React__default.createElement("div", {
215
+ className: "textBoxTitle"
216
+ }, title || 'Audio Player missing title'), /*#__PURE__*/React__default.createElement("div", {
217
+ className: "textBoxDescription"
218
+ }, description || 'Audio Player missing description')));
219
+ };
220
+
221
+ exports.ReactAudioPlayer = ReactAudioPlayer;
package/package.json ADDED
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "apm-react-audio-player",
3
+ "version": "1.0.0",
4
+ "author": "Jason Phan <jphan@mpr.org>",
5
+ "license": "MIT",
6
+ "private": false,
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "APMG/apm-react-audio-player.git"
10
+ },
11
+ "main": "dist/index.js",
12
+ "module": "dist/index.modern.js",
13
+ "source": "src/index.js",
14
+ "engines": {
15
+ "node": ">=10"
16
+ },
17
+ "scripts": {
18
+ "build": "rollup -c",
19
+ "dev": "rollup -c -w",
20
+ "test": "jest --watch",
21
+ "test:ci": "jest",
22
+ "test:coverage": "jest --coverage",
23
+ "eslint": "eslint .",
24
+ "prettier": "prettier --check '**/**.js'",
25
+ "prettier:fix": "prettier --check '**/**.js' --write",
26
+ "clean": "rm -rf node_modules dist package-lock.json yarn.lock"
27
+ },
28
+ "peerDependencies": {
29
+ "react": "^16.0.0"
30
+ },
31
+ "dependencies": {
32
+ "react-icons": "^4.7.1",
33
+ "rollup": "^1.17.0",
34
+ "rollup-plugin-babel": "^4.3.3"
35
+ },
36
+ "devDependencies": {
37
+ "babel-eslint": "^10.0.3",
38
+ "@babel/plugin-proposal-class-properties": "^7.5.5",
39
+ "@babel/plugin-proposal-optional-chaining": "^7.2.0",
40
+ "@babel/plugin-syntax-dynamic-import": "^7.2.0",
41
+ "@babel/preset-env": "^7.5.5",
42
+ "@babel/preset-react": "^7.0.0",
43
+ "babel-polyfill": "^6.26.0",
44
+ "@testing-library/react": "^8.0.5",
45
+ "cross-env": "^7.0.2",
46
+ "eslint": "^6.8.0",
47
+ "eslint-config-prettier": "^6.7.0",
48
+ "eslint-config-standard": "^14.1.0",
49
+ "eslint-config-standard-react": "^9.2.0",
50
+ "eslint-plugin-import": "^2.18.2",
51
+ "eslint-plugin-node": "^11.0.0",
52
+ "eslint-plugin-prettier": "^3.1.1",
53
+ "eslint-plugin-promise": "^4.2.1",
54
+ "eslint-plugin-react": "^7.17.0",
55
+ "eslint-plugin-standard": "^4.0.1",
56
+ "prettier": "^2.0.4",
57
+ "react": "^16.13.1",
58
+ "react-dom": "^16.13.1",
59
+ "react-scripts": "^3.4.1",
60
+ "rollup-plugin-json": "^4.0.0",
61
+ "jest": "^24.8.0",
62
+ "jest-environment-jsdom-fourteen": "^0.1.0",
63
+ "jest-prop-type-error": "^1.1.0"
64
+ },
65
+ "files": [
66
+ "dist"
67
+ ]
68
+ }