Skip to content

Commit 7223030

Browse files
committed
Add StreamVolumeControl component and its Storybook examples
1 parent dc7403e commit 7223030

4 files changed

Lines changed: 586 additions & 0 deletions

File tree

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# StreamVolumeControl 串流音量控制元件
2+
3+
## 概述
4+
5+
`StreamVolumeControl` 是一個用於控制串流直播音量的 React 元件。它提供了直觀的音量滑動條、靜音按鈕以及音量百分比顯示功能。
6+
7+
## 特色功能
8+
9+
- 🎵 **音量滑動條**: 平滑的音量調節體驗
10+
- 🔇 **靜音切換**: 一鍵靜音/取消靜音
11+
- 📊 **音量顯示**: 即時顯示當前音量百分比
12+
- 🎨 **現代化設計**: 漸層背景和圓滑的 UI 設計
13+
- 🖱️ **互動反饋**: 滑鼠懸停效果和平滑過渡動畫
14+
15+
## 使用方法
16+
17+
### 基本使用
18+
19+
```tsx
20+
import StreamVolumeControl from './StreamVolumeControl';
21+
22+
function App() {
23+
return (
24+
<StreamVolumeControl
25+
initialVolume={50}
26+
showPercentage={true}
27+
onVolumeChange={(volume, isMuted) => {
28+
console.log('音量變化:', { volume, isMuted });
29+
}}
30+
/>
31+
);
32+
}
33+
```
34+
35+
### 與 YouTube Player API 集成
36+
37+
```tsx
38+
function LiveStreamPlayer() {
39+
const [player, setPlayer] = useState<any>(null);
40+
const [isPlayerReady, setIsPlayerReady] = useState(false);
41+
42+
useEffect(() => {
43+
// 載入 YouTube API
44+
const script = document.createElement('script');
45+
script.src = 'https://www.youtube.com/iframe_api';
46+
document.body.appendChild(script);
47+
48+
window.onYouTubeIframeAPIReady = () => {
49+
const ytPlayer = new window.YT.Player('youtube-player', {
50+
videoId: 'YOUR_VIDEO_ID',
51+
events: {
52+
onReady: (event: any) => {
53+
setPlayer(event.target);
54+
setIsPlayerReady(true);
55+
},
56+
},
57+
});
58+
};
59+
}, []);
60+
61+
const handleVolumeChange = (volume: number, isMuted: boolean) => {
62+
if (!player || !isPlayerReady) return;
63+
64+
if (isMuted) {
65+
player.mute();
66+
} else {
67+
player.unMute();
68+
player.setVolume(volume);
69+
}
70+
};
71+
72+
return (
73+
<div>
74+
<div id="youtube-player"></div>
75+
<StreamVolumeControl
76+
initialVolume={75}
77+
onVolumeChange={handleVolumeChange}
78+
/>
79+
</div>
80+
);
81+
}
82+
```
83+
84+
## Props
85+
86+
| 屬性名 | 類型 | 預設值 | 說明 |
87+
|--------|------|--------|------|
88+
| `initialVolume` | `number` | `50` | 初始音量 (0-100) |
89+
| `onVolumeChange` | `(volume: number, isMuted: boolean) => void` | - | 音量變化回調函數 |
90+
| `showPercentage` | `boolean` | `true` | 是否顯示音量百分比 |
91+
| `className` | `string` | `''` | 自定義 CSS 類名 |
92+
93+
## 互動說明
94+
95+
- **音量調節**: 拖動滑動條或點擊滑動條上的任意位置
96+
- **靜音切換**: 點擊音量圖標 (🔊/🔉/🔈/🔇)
97+
- **視覺反饋**:
98+
- 音量 0-29%: 🔈 (低音量)
99+
- 音量 30-69%: 🔉 (中音量)
100+
- 音量 70-100%: 🔊 (高音量)
101+
- 靜音狀態: 🔇 (靜音)
102+
103+
## 注意事項
104+
105+
1. **真實音量控制**: 在 Storybook 示例中,此元件使用 YouTube Player API 實現真實的音量控制功能
106+
2. **API 載入**: 元件會自動載入 YouTube IFrame Player API,首次使用可能需要等待 API 載入完成
107+
3. **網路連接**: 需要穩定的網路連接來載入 YouTube API 和影片內容
108+
4. **瀏覽器相容性**: 支援現代瀏覽器,需要 JavaScript 啟用
109+
5. **實際整合**: 在生產環境中使用時,建議預先載入 YouTube API 以提升使用者體驗
110+
111+
## Storybook 範例
112+
113+
在 Storybook 中提供了多個使用範例:
114+
115+
- `Primary`: 基本音量控制
116+
- `WithLaoPiStream`: 老皮直播模擬
117+
- `WithLuDanStream`: 滷蛋直播模擬
118+
- `WithDingTeStream`: 丁特直播模擬
119+
- `MultipleStreams`: 多重直播同時控制
120+
121+
## 技術細節
122+
123+
- 使用 React Hooks (useState, useCallback) 進行狀態管理
124+
- CSS-in-JS 樣式定義,支持響應式設計
125+
- TypeScript 完整類型定義支持
126+
- 無外部依賴,純 React 實現

0 commit comments

Comments
 (0)