|
| 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 'mol-fe-react/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 | +## Props |
| 16 | + |
| 17 | +In addition to props below also accepts all [React's media events](https://reactjs.org/docs/events.html#media-events). |
| 18 | + |
| 19 | +```tsx |
| 20 | +export interface IAudioProps { |
| 21 | + src: string; |
| 22 | + autoplay?: boolean; |
| 23 | + loop?: boolean; |
| 24 | + muted?: boolean; |
| 25 | + preload?: 'none' | 'metadata' | 'auto'; |
| 26 | + volume?: number; |
| 27 | + noJs?: React.ReactElement<any>; |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | + - `src` - required, string, audio source file URL. |
| 32 | + - `autoplay` - optional, boolean, whether to autoplay audio, defaults to `false`. |
| 33 | + - `loop` - optional, boolean, whether to repeat the track when it ends, defaults to `false`. |
| 34 | + - `muted` - optional, boolean, whether to mute the audio, defaults to `false`. |
| 35 | + - `preload` - optional, string, `<audio>` element preload attribute. |
| 36 | + - `volume` - optional, number, audio volume in `[0..1]` range, defaults to `1`. |
| 37 | + - `noJs` - optional, React element(s) to render insided the `<audio>` tag. |
| 38 | + |
| 39 | +## Example |
| 40 | + |
| 41 | +Play a sample audio track with control buttons. |
| 42 | + |
| 43 | +```jsx |
| 44 | +<Audio src='https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3'>{(audio, state) => |
| 45 | + <div> |
| 46 | + <button onClick={audio.play}>Play</button> |
| 47 | + <button onClick={audio.pause}>Pause</button> |
| 48 | + <button onClick={() => audio.seek(state.time - 5)}>Seek -</button> |
| 49 | + <button onClick={() => audio.seek(state.time + 5)}>Seek +</button> |
| 50 | + <button onClick={() => audio.volume(state.volume - 0.05)}>Volume -</button> |
| 51 | + <button onClick={() => audio.volume(state.volume + 0.05)}>Volume +</button> |
| 52 | + <button onClick={audio.mute}>Mute</button> |
| 53 | + <button onClick={audio.unmute}>Unmute</button> |
| 54 | + </div> |
| 55 | +}</Audio> |
| 56 | +``` |
0 commit comments