impl #796 observe media volume change (#798)

This commit is contained in:
yz-yu
2022-01-16 15:39:43 +08:00
committed by GitHub
parent 69a1b9ffe6
commit a307c5c5bb
5 changed files with 36 additions and 12 deletions

View File

@@ -684,22 +684,28 @@ function initMediaInteractionObserver(
mediaInteractionCb: mediaInteractionCallback,
blockClass: blockClass,
mirror: Mirror,
sampling: SamplingStrategy,
): listenerHandler {
const handler = (type: MediaInteractions) => (event: Event) => {
const target = getEventTarget(event);
if (!target || isBlocked(target as Node, blockClass)) {
return;
}
mediaInteractionCb({
type,
id: mirror.getId(target as INode),
currentTime: (target as HTMLMediaElement).currentTime,
});
};
const handler = (type: MediaInteractions) =>
throttle((event: Event) => {
const target = getEventTarget(event);
if (!target || isBlocked(target as Node, blockClass)) {
return;
}
const { currentTime, volume, muted } = target as HTMLMediaElement;
mediaInteractionCb({
type,
id: mirror.getId(target as INode),
currentTime,
volume,
muted,
});
}, sampling.media || 500);
const handlers = [
on('play', handler(MediaInteractions.Play)),
on('pause', handler(MediaInteractions.Pause)),
on('seeked', handler(MediaInteractions.Seeked)),
on('volumechange', handler(MediaInteractions.VolumeChange)),
];
return () => {
handlers.forEach((h) => h());
@@ -987,6 +993,7 @@ export function initObservers(
o.mediaInteractionCb,
o.blockClass,
o.mirror,
o.sampling,
);
const styleSheetObserver = initStyleSheetObserver(

View File

@@ -1019,6 +1019,12 @@ export class Replayer {
if (d.currentTime) {
mediaEl.currentTime = d.currentTime;
}
if (d.volume) {
mediaEl.volume = d.volume;
}
if (d.muted) {
mediaEl.muted = d.muted;
}
if (d.type === MediaInteractions.Pause) {
mediaEl.pause();
}

View File

@@ -192,6 +192,10 @@ export type SamplingStrategy = Partial<{
* number is the throttle threshold of recording scroll
*/
scroll: number;
/**
* number is the throttle threshold of recording media interactions
*/
media: number;
/**
* 'all' will record all the input events
* 'last' will only record the last input value while input a sequence of chars
@@ -472,12 +476,15 @@ export const enum MediaInteractions {
Play,
Pause,
Seeked,
VolumeChange,
}
export type mediaInteractionParam = {
type: MediaInteractions;
id: number;
currentTime?: number;
volume?: number;
muted?: boolean;
};
export type mediaInteractionCallback = (p: mediaInteractionParam) => void;