Skip to content

Commit 23c6562

Browse files
committed
feat: audio tag stub
1 parent 1f342f3 commit 23c6562

File tree

8 files changed

+192
-0
lines changed

8 files changed

+192
-0
lines changed

packages/react-native-audio-api/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"types": "lib/typescript/index.d.ts",
1313
"files": [
1414
"src/",
15+
"unstable/react/",
1516
"lib/",
1617
"common/",
1718
"android/src/main/AndroidManifest.xml",
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React from 'react';
2+
3+
import type { AudioProps } from './types';
4+
import { useStableAudioProps } from './utils';
5+
6+
const Audio: React.FC<AudioProps> = (inProps) => {
7+
const {
8+
autoPlay,
9+
controls,
10+
loop,
11+
muted,
12+
preload,
13+
source,
14+
playbackRate,
15+
preservesPitch,
16+
volume,
17+
} = useStableAudioProps(inProps);
18+
19+
return null;
20+
};
21+
22+
export default Audio;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from 'react';
2+
import type { AudioProps } from './types';
3+
4+
const Audio: React.FC<AudioProps> = (props) => {
5+
return null;
6+
};
7+
8+
export default Audio;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export { default } from './Audio';
2+
3+
export type { AudioProps, AudioSource, TimeRanges } from './types';
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
export interface AudioURISource {
2+
uri?: string | undefined;
3+
// bundle?: string | undefined;
4+
method?: string | undefined;
5+
headers?: { [key: string]: string } | undefined;
6+
// cache?: 'default' | 'reload' | 'force-cache' | 'only-if-cached' | undefined;
7+
body?: string | undefined;
8+
}
9+
10+
export type AudioRequireSource = number;
11+
12+
export interface TimeRanges {
13+
length: number;
14+
start(index: number): number;
15+
end(index: number): number;
16+
}
17+
18+
export type AudioSource =
19+
| AudioURISource
20+
| AudioRequireSource
21+
| ReadonlyArray<AudioURISource>;
22+
23+
export type PreloadType = 'auto' | 'metadata' | 'none';
24+
25+
interface AudioControlProps {
26+
autoPlay: boolean;
27+
controls: boolean; // TBD: should we support control display at all?
28+
loop: boolean;
29+
muted: boolean;
30+
preload: PreloadType;
31+
source: AudioSource;
32+
playbackRate: number;
33+
preservesPitch: boolean;
34+
volume: number;
35+
}
36+
37+
interface AudioReadonlyProps {
38+
// TODO: decide if we want to expose them this way
39+
// duration: number;
40+
// currentTime: number;
41+
// ended: boolean;
42+
// paused: boolean;
43+
// buffered: TimeRanges;
44+
}
45+
46+
type TMPEmptyEventHandler = () => void;
47+
48+
interface AudioEventProps {
49+
onLoadStart?: TMPEmptyEventHandler;
50+
onLoad?: TMPEmptyEventHandler;
51+
onError?: TMPEmptyEventHandler;
52+
onProgress?: TMPEmptyEventHandler;
53+
onSeeked?: TMPEmptyEventHandler;
54+
onEnded?: TMPEmptyEventHandler;
55+
onPlay?: TMPEmptyEventHandler;
56+
onPause?: TMPEmptyEventHandler;
57+
}
58+
59+
export interface AudioPropsBase
60+
extends AudioControlProps,
61+
AudioReadonlyProps,
62+
AudioEventProps {}
63+
64+
export type AudioProps = Partial<AudioPropsBase> & { source: AudioSource };
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { useMemo } from 'react';
2+
import { AudioProps, AudioPropsBase } from './types';
3+
4+
export function withPropsDefaults(props: AudioProps): AudioPropsBase {
5+
return {
6+
...props,
7+
autoPlay: props.autoPlay ?? false,
8+
controls: props.controls ?? false,
9+
loop: props.loop ?? false,
10+
muted: props.muted ?? false,
11+
preload: props.preload ?? 'auto',
12+
source: props.source ?? [],
13+
playbackRate: props.playbackRate ?? 1.0,
14+
preservesPitch: props.preservesPitch ?? true,
15+
volume: props.volume ?? 1.0,
16+
};
17+
}
18+
19+
export function useStableAudioProps(props: AudioProps): AudioPropsBase {
20+
const {
21+
// Control Props
22+
autoPlay,
23+
controls,
24+
loop,
25+
muted,
26+
preload,
27+
source,
28+
playbackRate,
29+
preservesPitch,
30+
volume,
31+
32+
// Event Props
33+
onLoadStart,
34+
onLoad,
35+
onError,
36+
onProgress,
37+
onSeeked,
38+
onEnded,
39+
onPlay,
40+
onPause,
41+
} = withPropsDefaults(props);
42+
43+
return useMemo(
44+
() => ({
45+
// Control Props
46+
autoPlay,
47+
controls,
48+
loop,
49+
muted,
50+
preload,
51+
source,
52+
playbackRate,
53+
preservesPitch,
54+
volume,
55+
56+
// Event Props
57+
onLoadStart,
58+
onLoad,
59+
onError,
60+
onProgress,
61+
onSeeked,
62+
onEnded,
63+
onPlay,
64+
onPause,
65+
}),
66+
[
67+
autoPlay,
68+
controls,
69+
loop,
70+
muted,
71+
preload,
72+
source,
73+
playbackRate,
74+
preservesPitch,
75+
volume,
76+
onLoadStart,
77+
onLoad,
78+
onError,
79+
onProgress,
80+
onSeeked,
81+
onEnded,
82+
onPlay,
83+
onPause,
84+
]
85+
);
86+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './Audio';
2+
export { default as Audio } from './Audio';
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"main": "../../lib/module/unstable/react/index",
3+
"module": "../../lib/module/unstable/react/index",
4+
"react-native": "../../src/unstable/react/index",
5+
"types": "../../lib/typescript/unstable/react/index.d.ts"
6+
}

0 commit comments

Comments
 (0)