Skip to content

Commit fddb315

Browse files
authored
Merge pull request #18 from riyaddecoder/master
doc build
2 parents cbc35c9 + e049e96 commit fddb315

File tree

11 files changed

+2456
-412
lines changed

11 files changed

+2456
-412
lines changed

README.md

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ minimal setup.
2020

2121
## Demo
2222

23-
Check [examples](https://riyaddecoder.github.io/react-audio-play/#example-usage)
23+
Check [examples](https://riyaddecoder.github.io/react-audio-play/examples.html)
2424

2525
## Installation
2626

@@ -93,6 +93,54 @@ They can be turned off by setting `hasKeyBindings` prop to `false`
9393
- `width` (string, optional): The width of the audio player. Use this prop to set the width of the player. For example, `"100%"`, `"300px"`, etc.
9494
- `style` (object, optional): An object containing additional inline styles for the component.
9595

96+
97+
## Advanced Usage
98+
99+
Starting with version `v1.0.4`, you can access certain actions of the `AudioPlayer` component programmatically using a `ref` with the following interface:
100+
101+
- `play`: Function to start audio playback.
102+
- `pause`: Function to pause audio playback.
103+
- `stop`: Function to stop the audio playback.
104+
- `focus`: Function to focus on the audio player component.
105+
106+
## Example usage with ref (available from v1.0.4)
107+
108+
```js
109+
import { useRef } from 'react';
110+
import { AudioPlayer, AudioPlayerRef } from 'react-audio-play';
111+
112+
function App() {
113+
const playerRef = useRef<AudioPlayerRef>(null);
114+
115+
const handlePlay = () => {
116+
playerRef.current?.play();
117+
};
118+
119+
const handlePause = () => {
120+
playerRef.current?.pause();
121+
};
122+
123+
const handleStop = () => {
124+
playerRef.current?.stop();
125+
};
126+
127+
const handleFocus = () => {
128+
playerRef.current?.focus();
129+
};
130+
131+
return (
132+
<div>
133+
<AudioPlayer ref={playerRef} src="path/to/audio.mp3" autoPlay />
134+
<button onClick={handlePlay}>Play</button>
135+
<button onClick={handlePause}>Pause</button>
136+
<button onClick={handleStop}>Stop</button>
137+
<button onClick={handleFocus}>Focus</button>
138+
</div>
139+
);
140+
}
141+
142+
```
143+
96144
## Example with Custom Event Handling
97145
98146
```js
@@ -122,7 +170,7 @@ export default App;
122170
## Custom Styling
123171
124172
You can easily customize the appearance of the audio player by applying your CSS styles to the AudioPlayer component or by overriding the default styles in your project's CSS. Check
125-
[examples](https://riyaddecoder.github.io/react-audio-play/#styled-examples)
173+
[examples](https://riyaddecoder.github.io/react-audio-play/examples.html#example-5-using-style-object)
126174
127175
## License
128176
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<template>
2+
<div ref="el" />
3+
</template>
4+
5+
<script setup>
6+
import { createElement } from 'react';
7+
import { createRoot } from 'react-dom/client';
8+
import { ref, onMounted, defineProps } from 'vue';
9+
import AudioPlayerWithRef from './AudioPlayerWithRef.jsx';
10+
11+
const props = defineProps({
12+
src: {
13+
type: String,
14+
required: true
15+
}
16+
});
17+
18+
const el = ref();
19+
20+
onMounted(() => {
21+
const root = createRoot(el.value);
22+
root.render(createElement(AudioPlayerWithRef, { ...props }));
23+
});
24+
</script>
25+
<style>
26+
.audio-example-with-ref button {
27+
padding: 1px 10px;
28+
background: white;
29+
color: black;
30+
border-radius: 5px;
31+
margin-right: 3px;
32+
margin-top: 5px;
33+
box-shadow: 1px 1px 8px 3px #ececec;
34+
}
35+
36+
.dark .audio-example-with-ref button {
37+
box-shadow: none;
38+
}
39+
40+
.audio-example-with-ref button:hover {
41+
background: #ddd;
42+
}
43+
</style>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React, { useRef } from 'react';
2+
import { AudioPlayer } from 'react-audio-play';
3+
4+
export default function AudioPlayerWithRef(props) {
5+
const playerRef = useRef(null);
6+
7+
const handlePlay = () => {
8+
playerRef.current?.play();
9+
};
10+
11+
const handlePause = () => {
12+
playerRef.current?.pause();
13+
};
14+
15+
const handleStop = () => {
16+
playerRef.current?.stop();
17+
};
18+
19+
const handleFocus = () => {
20+
playerRef.current?.focus();
21+
};
22+
23+
return (
24+
<div className='audio-example-with-ref'>
25+
<AudioPlayer ref={playerRef} src={props.src} />
26+
<button onClick={handlePlay}>Play</button>
27+
<button onClick={handlePause}>Pause</button>
28+
<button onClick={handleStop}>Stop</button>
29+
<button onClick={handleFocus}>Focus</button>
30+
</div>
31+
);
32+
}

documentation/docs/examples.md

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,51 @@ Preview
8181

8282
<small>Music Source: <a :href="music.getCurrentMusic().sourceLink" target="_blank">{{music.getCurrentMusic().title}}</a></small>
8383

84-
## Example 4: Darkmode using basic style props
84+
## Example 4: Usage with ref (available from v1.0.4)
85+
86+
```tsx
87+
import { useRef } from 'react';
88+
import { AudioPlayer, AudioPlayerRef } from 'react-audio-play';
89+
90+
function App() {
91+
const playerRef = useRef<AudioPlayerRef>(null);
92+
93+
const handlePlay = () => {
94+
playerRef.current?.play();
95+
};
96+
97+
const handlePause = () => {
98+
playerRef.current?.pause();
99+
};
100+
101+
const handleStop = () => {
102+
playerRef.current?.stop();
103+
};
104+
105+
const handleFocus = () => {
106+
playerRef.current?.focus();
107+
};
108+
109+
return (
110+
<div>
111+
<AudioPlayer ref={playerRef} src="path/to/audio.mp3" autoPlay />
112+
<button onClick={handlePlay}>Play</button>
113+
<button onClick={handlePause}>Pause</button>
114+
<button onClick={handleStop}>Stop</button>
115+
<button onClick={handleFocus}>Focus</button>
116+
</div>
117+
);
118+
}
119+
120+
```
121+
122+
Preview
123+
124+
<AudioPlayerRefExample :src="music.nextMusic().url"/>
125+
126+
<small>Music Source: <a :href="music.getCurrentMusic().sourceLink" target="_blank">{{music.getCurrentMusic().title}}</a></small>
127+
128+
## Example 5: Darkmode using basic style props
85129

86130
```js
87131
import { AudioPlayer } from "react-audio-play";
@@ -104,7 +148,7 @@ Preview
104148

105149
<small>Music Source: <a :href="music.getCurrentMusic().sourceLink" target="_blank">{{music.getCurrentMusic().title}}</a></small>
106150

107-
## Example 5: Using Style Object
151+
## Example 6: Using Style Object
108152

109153
```js
110154
import { AudioPlayer } from "react-audio-play";
@@ -127,7 +171,7 @@ Preview
127171

128172
<small>Music Source: <a :href="music.getCurrentMusic().sourceLink" target="_blank">{{music.getCurrentMusic().title}}</a></small>
129173

130-
## Example 6: Using Custom CSS
174+
## Example 7: Using Custom CSS
131175

132176
::: code-group
133177

@@ -183,7 +227,7 @@ Preview
183227

184228
<small>Music Source: <a :href="music.getCurrentMusic().sourceLink" target="_blank">{{music.getCurrentMusic().title}}</a></small>
185229

186-
## Example 7: More Playing With CSS
230+
## Example 8: More Playing With CSS
187231

188232
::: code-group
189233

@@ -255,6 +299,7 @@ Preview
255299
<script setup>
256300
import { Music } from './.vitepress/lib/Music.ts';
257301
import AudioPlayerWrapper from './.vitepress/components/AudioPlayerWrapper.vue';
302+
import AudioPlayerRefExample from './.vitepress/components/AudioPlayerRefExample.vue';
258303

259304
const music = new Music();
260305
</script>

documentation/docs/index.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ The **AudioPlayer** component accepts the following props:
5252
| `width` | string, optional | The width of the audio player. For example, `"100%"`, `"300px"`, etc. |
5353
| `style` | object, optional | An object containing additional inline styles for the component. |
5454
55+
56+
## Advanced Usage
57+
58+
Starting with version `v1.0.4`, you can access certain actions of the `AudioPlayer` component programmatically using a `ref` with the following interface:
59+
60+
- `play`: Function to start audio playback.
61+
- `pause`: Function to pause audio playback.
62+
- `stop`: Function to stop the audio playback.
63+
- `focus`: Function to focus on the audio player component.
64+
65+
::: info
66+
[Example Here](/examples.html#example-4-usage-with-ref-available-from-v1-0-4)
67+
:::
68+
5569
## Keyboard Shortcuts
5670
5771
Below are the keyboard shortcuts available when the audio player is focused. They can be turned off by setting the `hasKeyBindings` prop to `false`.

0 commit comments

Comments
 (0)