Skip to content

Commit 1a616a5

Browse files
committed
Add audio element native props
1 parent ba9344d commit 1a616a5

File tree

5 files changed

+66
-31
lines changed

5 files changed

+66
-31
lines changed

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,20 @@ const Player = () => {
4646
}
4747

4848
```
49-
49+
## Audio element native props
50+
| Props | Description | Default | Optional |
51+
| :------------ |:--------------- |:--------------- | :--------------- |
52+
| **`src`** | The address or URL of the a audio resource that is to be considered | N/A | No |
53+
| **`volume`** | Initial volume level for the audio, minimum being `0`, maximum being `1` | `0.75` | Yes |
54+
| **`loop`** | Sets a flag to specify whether playback should restart after it completes | `false` | Yes |
55+
| **`muted`** | Sets a flag that indicates whether the audio is muted | `false` | Yes |
56+
| **`autoplay`** | Sets a value that indicates whether to start playing the media automatically | `false` | Yes |
57+
| **`crossOrigin`** | The CORS setting for this media element. [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/crossOrigin) | `null` | Yes |
58+
| **`autoplay`** | Sets a value indicating what data should be preloaded, if any. Allowed values `""`, `"none"`, `"metadata"`, `"auto"` | `""` | Yes |
59+
---
60+
## Audio player props props
5061
| Props | Description | Default | Optional |
5162
| :------------ |:--------------- |:--------------- | :--------------- |
52-
| **`src`** | Source for the audio file that needs to be played | N/A | No |
5363
| **`minimal`** | Displays a minimal version of the audio player, with only the play/pause button, track bar and timestamp | `false` | Yes |
5464
| **`width`** | Width of the audio player | N/A | No |
5565
| **`barWidth`** | Width of each individual bar in the visualization | `2` | Yes |
@@ -61,8 +71,6 @@ const Player = () => {
6171
| **`barPlayedColor`** | Color for the bars that have been played | `"rgb(160, 198, 255)""` | Yes |
6272
| **`allowSkip`** | Represents whether the skip forward/backward options should be displayed | `true` | Yes |
6373
| **`skipDuration`** | The number of seconds to skip forward/backward | `5` | Yes |
64-
| **`initialVolume`** | Initial volume for the audio, minimum being `0`, maximum being `1` | `0.75` | Yes |
65-
| **`loop`** | Setting this to `true` will keep playing the audio in a loop | `false` | Yes |
6674
| **`showLoopOption`** | Represents whether to show the loop options | `true` | Yes |
6775
| **`showVolumeControl`** | Represents whether the volume control should be shown | `true` | Yes |
6876
| **`seekBarColor`** | Color for the audio seek bar | `rgba(140, 140, 140)` | Yes |

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@
3434
"build": "tsc && vite build",
3535
"preview": "vite preview",
3636
"test": "vitest test.tsx",
37+
"tf": "vitest",
3738
"test:browser": "karma start",
3839
"coverage": "vitest run --coverage",
3940
"lint": "eslint src/**/*.ts{,x}",
4041
"lint:fix": "npm run lint -- --fix"
4142
},
4243
"dependencies": {
43-
"react-audio-visualize": "^1.0.0"
44+
"react-audio-visualize": "^1.1.0"
4445
},
4546
"devDependencies": {
4647
"@testing-library/dom": "^9.3.0",

src/components/AudioPlayer.tsx

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,39 @@ import repeatSVG from "../icons/repeat.svg";
1010
import repeatOneSVG from "../icons/repeat-one.svg";
1111
import Timer from "./Timer";
1212

13-
interface AudioPlayerProps {
13+
interface AudioElementNativeProps {
1414
/**
15-
* Source for the audio file that needs to be played
15+
* The address or URL of the a audio resource that is to be considered.
1616
*/
1717
src: string;
18+
/**
19+
* Sets a flag to specify whether playback should restart after it completes. Defaults to `false`
20+
*/
21+
loop?: boolean;
22+
/**
23+
* Initial volume level for the audio, minimum being `0`, maximum being `1`. Defaults to `0.75`
24+
*/
25+
volume?: number;
26+
/**
27+
* Sets a flag that indicates whether the audio is muted. Defaults to `false`
28+
*/
29+
muted?: boolean;
30+
/**
31+
* Sets a value that indicates whether to start playing the media automatically. Defaults to `false`
32+
*/
33+
autoplay?: boolean;
34+
/**
35+
* The CORS setting for this media element. {@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/crossOrigin MDN Reference}. Defaults to `null`
36+
*/
37+
crossOrigin?: string;
38+
/**
39+
* Sets a value indicating what data should be preloaded, if any. Defaults to empty string
40+
*/
41+
preload?: "" | "none" | "metadata" | "auto";
42+
}
43+
44+
interface AudioPlayerProps {
45+
1846
/**
1947
* Displays a minimal version of the audio player, with only the play/pause button, track bar and timestamp. Defaults to `false`
2048
*/
@@ -31,7 +59,6 @@ interface AudioPlayerProps {
3159
* Gap between each individual bar in the visualization. Defaults to `1`
3260
*/
3361
gap?: number;
34-
3562
/**
3663
* Represents whether the audio visualization (waveform) should be displayed. Defaults to `true`
3764
*/
@@ -52,7 +79,6 @@ interface AudioPlayerProps {
5279
* Bar color for the bars that have been played
5380
*/
5481
barPlayedColor?: string;
55-
5682
/**
5783
* Represents whether the skip forward/backward options should be displayed. Defaults to `true`. Not applicable when `minimal` is set to `true`
5884
*/
@@ -61,14 +87,6 @@ interface AudioPlayerProps {
6187
* The number of seconds to skip forward/backward. Defaults to `5`
6288
*/
6389
skipDuration?: number;
64-
/**
65-
* Initial volume for the audio, minimum being `0`, maximum being `1`. Defaults to `0.75`
66-
*/
67-
initialVolume?: number;
68-
/**
69-
* Setting this to `true` will keep playing the audio in a loop. Defaults to `false`
70-
*/
71-
loop?: boolean;
7290
/**
7391
* Represents whether to show the loop options. Defaults to `true`
7492
*/
@@ -95,8 +113,16 @@ interface AudioPlayerProps {
95113
hideSeekKnobWhenPlaying?: boolean;
96114
}
97115

98-
const AudioPlayer: React.FC<AudioPlayerProps> = ({
116+
const AudioPlayer: React.FC<AudioElementNativeProps & AudioPlayerProps> = ({
117+
// native props
99118
src,
119+
loop = false,
120+
volume = 0.75,
121+
muted = false,
122+
autoplay = false,
123+
crossOrigin = null,
124+
preload = "",
125+
// Audio player props
100126
minimal = false,
101127
width,
102128
trackHeight = 75,
@@ -108,8 +134,6 @@ const AudioPlayer: React.FC<AudioPlayerProps> = ({
108134
barPlayedColor,
109135
allowSkip = true,
110136
skipDuration = 5,
111-
initialVolume = 0.75,
112-
loop = false,
113137
showLoopOption = true,
114138
showVolumeControl = true,
115139
seekBarColor,
@@ -122,7 +146,7 @@ const AudioPlayer: React.FC<AudioPlayerProps> = ({
122146
const [currentTime, setCurrentTime] = useState<number>(0);
123147
const [duration, setDuration] = useState<number>(0);
124148
const [isPlaying, setIsPlaying] = useState<boolean>(false);
125-
const [volume, setVolume] = useState<number>(initialVolume);
149+
const [internalVolume, setVolume] = useState<number>(volume);
126150
const [isMuted, setIsMuted] = useState<boolean>(false);
127151
const [isLoop, setIsLoop] = useState<boolean>(loop);
128152

@@ -131,8 +155,12 @@ const AudioPlayer: React.FC<AudioPlayerProps> = ({
131155
.then(async (response): Promise<Blob> => {
132156
const blob = await response.blob();
133157
audio.src = URL.createObjectURL(blob);
134-
audio.volume = initialVolume;
158+
audio.volume = volume;
135159
audio.loop = loop;
160+
audio.muted = muted;
161+
audio.autoplay = autoplay;
162+
audio.crossOrigin = crossOrigin;
163+
audio.preload = preload;
136164
setBlob(blob);
137165
return blob;
138166
})
@@ -345,7 +373,7 @@ const AudioPlayer: React.FC<AudioPlayerProps> = ({
345373
}}
346374
/>
347375
<TrackBar
348-
current={volume}
376+
current={internalVolume}
349377
total={1}
350378
setCurrent={setAudioVolume}
351379
color={volumeControlColor}

tests/AudioPlayer.test.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import React from "react";
22
import { expect, test, vi } from "vitest";
33
import renderer from "react-test-renderer"
4-
import userEvent from "@testing-library/user-event";
5-
import { render, act, screen } from "@testing-library/react";
64
import { AudioPlayer } from "../src/components/AudioPlayer";
75

86
describe("Test AudioPlayer", () => {

0 commit comments

Comments
 (0)