|
| 1 | +# `<Audio>` |
| 2 | + |
| 3 | +FaCC that creates an `<audio>` element to play audio tracks, re-renders on audio state changes. |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +```jsx |
| 8 | +import {Audio} from 'libreact/lib/Audio'; |
| 9 | + |
| 10 | +<Audio autoPlay src='https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3'>{(control, state) => { |
| 11 | + JSON.stringify(state, null, 4) |
| 12 | +}}</Audio> |
| 13 | +``` |
| 14 | + |
| 15 | + |
| 16 | +## Props |
| 17 | + |
| 18 | +In addition to props below also accepts all [React's media events](https://reactjs.org/docs/events.html#media-events). |
| 19 | + |
| 20 | +```tsx |
| 21 | +interface IAudioProps { |
| 22 | + src: string; |
| 23 | + autoPlay?: boolean; |
| 24 | + loop?: boolean; |
| 25 | + muted?: boolean; |
| 26 | + preload?: 'none' | 'metadata' | 'auto'; |
| 27 | + volume?: number; |
| 28 | + noJs?: React.ReactElement<any>; |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | + - `src` - required, string, audio source file URL. |
| 33 | + - `autoPlay` - optional, boolean, whether to autoplay audio, defaults to `false`. |
| 34 | + - `loop` - optional, boolean, whether to repeat the track when it ends, defaults to `false`. |
| 35 | + - `muted` - optional, boolean, whether to mute the audio, defaults to `false`. |
| 36 | + - `preload` - optional, string, `<audio>` element preload attribute. |
| 37 | + - `volume` - optional, number, audio volume in `[0..1]` range, defaults to `1`. |
| 38 | + - `noJs` - optional, React element(s) to render insided the `<audio>` tag. |
| 39 | + - `onMount` - optional, callback, called when component mounts, receives `IAudio` as the first argument. |
| 40 | + - `onUnmount` - optional, callback, called when component un-mounts, receives `IAudio` as the first argument. |
| 41 | + |
| 42 | + |
| 43 | +## Arguments |
| 44 | + |
| 45 | +The `children` function receives two arguments, `audio` instance as *controller* and `state`. |
| 46 | + |
| 47 | +```jsx |
| 48 | +<Audio autoPlay src={src}>{(audio, state) => |
| 49 | + |
| 50 | +}</Audio> |
| 51 | +``` |
| 52 | + |
| 53 | +First argument is the `<Audio>` component instance with the following public signature. |
| 54 | + |
| 55 | +```ts |
| 56 | +interface IAudio { |
| 57 | + el: HTMLAudioElement; |
| 58 | + play(); |
| 59 | + pause(); |
| 60 | + seek(time: number); |
| 61 | + volume(value: number); |
| 62 | + mute(); |
| 63 | + unmute(); |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +, where |
| 68 | + |
| 69 | + - `el` - `<audio>` element DOM node. |
| 70 | + |
| 71 | +The second argument is the state of the `<Audio>` component with the following signature. |
| 72 | + |
| 73 | +```ts |
| 74 | +interface IAudioState { |
| 75 | + time?: number; |
| 76 | + duration?: number; |
| 77 | + isPlaying?: boolean; |
| 78 | + muted?: boolean; |
| 79 | + volume?: number; |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +, where |
| 84 | + |
| 85 | + - `time` - current time in seconds. |
| 86 | + - `duration` - total audio track duration in seconds. |
| 87 | + - `isPlaying` - whether the audio track is currently playing. |
| 88 | + - `muted` - whether `muted` attribute is set on the audio element. |
| 89 | + - `volume` - current volume in range `[0..1]`. |
| 90 | + |
| 91 | + |
| 92 | +## Example |
| 93 | + |
| 94 | +Play a sample audio track with control buttons. |
| 95 | + |
| 96 | +```jsx |
| 97 | +<Audio src='https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3'> |
| 98 | + {({play, pause, seek, volume, mute, unmute}, state) => |
| 99 | + <div> |
| 100 | + <button onClick={play}>Play</button> |
| 101 | + <button onClick={pause}>Pause</button> |
| 102 | + <button onClick={() => seek(state.time - 5)}>Seek -</button> |
| 103 | + <button onClick={() => seek(state.time + 5)}>Seek +</button> |
| 104 | + <button onClick={() => volume(state.volume - 0.05)}>Volume -</button> |
| 105 | + <button onClick={() => volume(state.volume + 0.05)}>Volume +</button> |
| 106 | + <button onClick={mute}>Mute</button> |
| 107 | + <button onClick={unmute}>Unmute</button> |
| 108 | + </div> |
| 109 | + } |
| 110 | +</Audio> |
| 111 | +``` |
0 commit comments