具有SDK效果的虚拟背景
虚拟背景插件有助于自定义自己的背景 。 自定义选项正在模糊背景或被静态图像所取代. 本指南概述了100ms虚拟背景插件的使用.
先决条件
获取100ms VirtualBackground软件包**(自1.11.28版本起支持)
npm install --save @100mslive/hms-virtual-background@latest
特征
以下特性目前得到以下 HMSEffectsPlugin 类 :
/**
* Sets the blur intensity.
* @param {number} blur - The blur intensity, ranging from 0 to 1.
* @returns {void}
*/
setBlur(blur) {}
/**
* Sets the virtual background using the provided media URL.
* @param {HMSEffectsBackground} url - The media URL to set as the virtual background.
* Alternatively, the background image can be downloaded beforehand and passed to setBackground as objectURL
* @returns {void}
*/
setBackground(url) {}
/**
* Retrieves the name of the plugin.
* @returns {string} The name of the plugin, 'HMSEffects'.
*/
getName() {}
/**
* Retrieves the currently enabled background type or media URL.
* @returns {string|MediaStream|MediaStreamTrack|HTMLVideoElement} The background type or media URL.
*/
getBackground() {}
/**
* Sets the preset quality of the virtual background.
* Options: "balanced" | "quality"
* The 'quality' preset has a higher CPU usage than the 'balanced' preset which is the default
* @param {string} preset - The preset quality to set.
* @returns {Promise<void>}
*/
setPreset(preset) {}
/**
* Retrieves the active preset quality of the virtual background.
* @returns {string} The active preset quality.
*/
getPreset() {}
/**
* Clears all applied filters.
* @returns {void}
*/
removeEffects() {}
/**
* Stops the plugin.
* @returns {void}
*/
stop() {}
插件支持的回调 :
在初始化时, 插件下载所需资源后:
const effectsPlugin = new HMSEffectsPlugin(<key>, () => console.log("Plugin initialised"));
在分辨率变化( 在设备旋转或视频宽度比变化时) :
effectsPlugin.onResolutionChange = (width: number, height: number) => {
console.log(`Resolution changed to ${width}x${height}`)
}
证明虚拟背景
效果的 SDK 键需要用于即时 HMSEffectsPlugin 类 :
const effectsKey = useHMSStore(selectEffectsKey);
建议在单独的文件中初始化对象,以防止在重报时多次初始化,并保持UI级别代码独立于SDK的内部调用.
import { HMSEffectsPlugin, HMSVirtualBackgroundTypes } from '@100mslive/hms-virtual-background';
export class VBPlugin {
private effectsPlugin?: HMSEffectsPlugin | undefined;
initialisePlugin = (effectsSDKKey?: string) => {
if (this.getVBObject()) {
return;
}
if (effectsSDKKey) {
this.effectsPlugin = new HMSEffectsPlugin(effectsSDKKey);
}
};
getBackground = () => {
return this.effectsPlugin?.getBackground();
};
getBlurAmount = () => {
return this.effectsPlugin?.getBlurAmount();
};
getVBObject = () => {
return this.effectsPlugin;
};
getName = () => {
return this.effectsPlugin?.getName();
};
setBlur = async (blurPower: number) => {
this.effectsPlugin?.setBlur(blurPower);
};
setBackground = async (mediaURL: string) => {
this.effectsPlugin?.setBackground(mediaURL);
};
setPreset = (preset: string) => {
this.effectsPlugin.setPreset(preset);
};
getPreset = () => {
return this.effectsPlugin?.getPreset() || '';
};
removeEffects = async () => {
this.effectsPlugin?.removeEffects();
};
reset = () => {
this.effectsPlugin = undefined;
};
}
export const VBHandler = new VBPlugin();
建设UI
以下片段说明了如何将插件添加到视频中并管理UI状态以保存配置:
import {
selectEffectsKey,
selectIsLocalVideoPluginPresent
selectLocalVideoTrackID,
useHMSStore,
} from '@100mslive/react-sdk';
import {
HMSEffectsPlugin,
HMSVirtualBackgroundTypes
} from '@100mslive/hms-virtual-background';
import { VBHandler } from './VBHandler';
export const VirtualBackgroundPicker = () => {
const hmsActions = useHMSActions();
// Get the effects SDK key here
const effectsKey = useHMSStore(selectEffectsKey);
const trackId = useHMSStore(selectLocalVideoTrackID);
const isPluginAdded = useHMSStore(selectIsLocalVideoPluginPresent(VBHandler?.getName() || ''));
// State can be used to show active selection
const [background, setBackground] = useState<string | HMSVirtualBackgroundTypes>(
VBHandler.getBackground() as string | HMSVirtualBackgroundTypes,
);
useEffect(() => {
if (!track?.id) {
return;
}
if (!isPluginAdded) {
let vbObject = VBHandler.getVBObject();
if (!vbObject) {
VBHandler.initialisePlugin(effectsKey);
vbObject = VBHandler.getVBObject();
if (effectsKey) {
hmsActions.addPluginsToVideoStream([vbObject as HMSEffectsPlugin]);
}
}
}
}, [hmsActions, isPluginAdded, effectsKey, track?.id]);
// UI code for media picker can go here
}
这处理初始化,并在视频流中添加插件. 插件在初始化过程中第一次负载需要几秒钟. 随后的过滤和效果选择应花不到一秒钟的时间来反映.
这些方法可以通过 VBHandler 对象 :
const setBackground = async(mediaURL : string) => {
await VBHandler?.setBackground(mediaURL);
// The selection can be highlighted using the activeBackground state
setActiveBackground(mediaURL);
}
const setBlur = async(blurAmount: number) => {
await VBHandler?.setBlur(blurAmount);
setActiveBackground(HMSVirtualBackgroundTypes.BLUR);
}
const removeEffects = async() => {
await VBHandler.removeEffects();
setActiveBackground(HMSVirtualBackgroundTypes.NONE);
}
可在下文中看到全面执行《公约》的情况 房间基特反应包。 。 。 .
支持的浏览器
效果虚拟背景在基于Safari,Firefox和Chromium的浏览器上得到支持.