Skip to content

Commit e1fceb2

Browse files
committed
feat: add <Media> and make <Audio> and <Video> use
1 parent 0f3c49e commit e1fceb2

File tree

4 files changed

+259
-387
lines changed

4 files changed

+259
-387
lines changed

src/Audio/index.ts

Lines changed: 6 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {Component, cloneElement, Children} from 'react';
22
import {h, noop} from '../util';
3+
import {Media} from '../Media';
34

45
export type TAudioEvent = (event, Audio, IAudioState) => void;
56

@@ -46,174 +47,17 @@ export interface IAudioState {
4647
volume?: number;
4748
}
4849

49-
export class Audio extends Component<IAudioProps, IAudioState> {
50-
el: HTMLAudioElement = null;
51-
52-
state: IAudioState = {
53-
time: 0,
54-
duration: 0,
55-
isPlaying: false,
56-
muted: false,
57-
volume: 1
58-
};
59-
60-
ref = (el) => {
61-
this.el = el;
62-
};
63-
64-
componentDidMount () {
65-
if (this.props.autoPlay && this.el.paused) {
66-
this.play();
67-
}
68-
69-
this.setState({
70-
volume: this.el.volume
71-
});
72-
}
73-
74-
componentWillUnmount () {
75-
this.el = null;
76-
}
77-
78-
play = () => {
79-
if (this.el) {
80-
this.el.play();
81-
}
82-
};
83-
84-
pause = () => {
85-
if (this.el) {
86-
this.el.pause();
87-
}
88-
};
89-
90-
seek = (time: number) => {
91-
if (this.el) {
92-
time = Math.min(this.state.duration, Math.max(0, time));
93-
this.el.currentTime = time;
94-
}
95-
};
96-
97-
volume = (volume) => {
98-
if (this.el) {
99-
volume = Math.min(1, Math.max(0, volume));
100-
101-
this.el.volume = volume;
102-
this.setState({
103-
volume
104-
});
105-
}
106-
};
107-
108-
mute = () => {
109-
if (this.el) {
110-
this.el.muted = true;
111-
}
112-
};
113-
114-
unmute = () => {
115-
if (this.el) {
116-
this.el.muted = false;
117-
}
118-
};
119-
120-
event = (name: string) => (event) => {
121-
const handler = this.props[name];
122-
123-
if (handler) {
124-
handler(event, this, this.state);
125-
}
126-
};
127-
128-
onPlay = (event) => {
129-
this.setState({
130-
isPlaying: true
131-
});
132-
133-
this.event('onPlay')(event);
134-
};
135-
136-
onPause = (event) => {
137-
this.setState({
138-
isPlaying: false
139-
});
140-
141-
this.event('onPause')(event);
142-
};
143-
144-
onVolumeChange = (event) => {
145-
const {muted, volume} = this.el;
146-
147-
this.setState({
148-
muted,
149-
volume
150-
});
151-
152-
this.event('onVolumeChange')(event);
153-
};
154-
155-
onDurationChange = (event) => {
156-
this.setState({
157-
duration: this.el.duration
158-
});
159-
160-
this.event('onDurationChange')(event);
161-
};
162-
163-
onTimeUpdate = (event) => {
164-
this.setState({
165-
time: this.el.currentTime
166-
});
167-
168-
this.event('onTimeUpdate')(event);
169-
};
50+
export class Audio extends Media<IAudioProps, IAudioState, Audio> {
51+
tag = 'audio' as any;
17052

17153
render () {
172-
const {props, event} = this;
173-
const {children, src, autoPlay, loop, muted, preload, volume, noJs} = props;
174-
175-
176-
const audio = h('audio', {
177-
ref: this.ref,
178-
controls: false,
179-
src,
180-
autoPlay,
181-
loop,
182-
muted,
183-
preload,
184-
volume,
185-
onAbort: event('onAbort'),
186-
onCanPlay: event('onCanPlay'),
187-
onCanPlayThrough: event('onCanPlayThrough'),
188-
onDurationChange: this.onDurationChange,
189-
onEmptied: event('onEmptied'),
190-
onEncrypted: event('onEncrypted'),
191-
onEnded: event('onEnded'),
192-
onError: event('onError'),
193-
onLoadedData: event('onLoadedData'),
194-
onLoadedMetadata: event('onLoadedMetadata'),
195-
onLoadStart: event('onLoadStart'),
196-
onPause: this.onPause,
197-
onPlay: this.onPlay,
198-
onPlaying: event('onPlaying'),
199-
onProgress: event('onProgress'),
200-
onRateChange: event('onRateChange'),
201-
onSeeked: event('onSeeked'),
202-
onSeeking: event('onSeeking'),
203-
onStalled: event('onStalled'),
204-
onSuspend: event('onSuspend'),
205-
onTimeUpdate: this.onTimeUpdate,
206-
onVolumeChange: this.onVolumeChange,
207-
onWaiting: event('onWaiting')
208-
},
209-
noJs
210-
);
211-
54+
const {children} = this.props;
55+
const audio = super.render();
21256
const markup = children(this, this.state);
21357

21458
return cloneElement(markup, null, ...[
21559
...Children.toArray(markup.props.children),
21660
audio
217-
]);
61+
]) as any;
21862
}
21963
}

0 commit comments

Comments
 (0)