共用方式為


快速入門:將視訊效果新增至您的視訊通話

您可以使用視訊效果功能,在視訊通話中將效果新增至您的視訊。 這項功能可讓開發人員將背景視覺效果和背景視訊取代項目建置到通話體驗中。 背景模糊為使用者提供移除參與者背後干擾的機制,以便參與者可在沒有背景干擾活動或機密資訊的情況下進行通訊。 這在遠距醫療環境中特別有用,其中提供者或患者可能想要遮蔽其周圍環境,以保護敏感性資訊或個人識別資訊。 背景模糊可以套用到所有虛擬約會案例 (包括電話銀行和虛擬聽證會) 以保護使用者隱私權。 除了增強機密性之外,背景模糊還允許更多創意的表達,讓使用者可以上傳自訂背景來裝載更有趣、個人化的通話體驗。

注意

通話效果無法獨立使用,必須與 WebJS 的 Azure 通訊通話用戶端程式庫 (https://www.npmjs.com/package/@azure/communication-calling) 搭配使用才能運作。

使用視訊效果

Install the package

使用 npm install 命令,以安裝適用於 JavaScript 的 Azure 通訊服務效果 SDK。

重要

本快速入門會使用 Azure 通訊服務通話 SDK 1.13.1 版 (或更新版本) 和大於或等於 1.0.1 的 Azure 通訊服務通話效果 SDK 版本。

npm install @azure/communication-calling-effects --save

如需通話通訊效果 npm 套件頁面的詳細資料,請參閱這裡

注意

目前,只有 Chrome 和 Edge Desktop Browser (Windows 與 Mac) 以及 Mac Safari Desktop 提供建立視訊背景效果的瀏覽器支援。

注意

目前有兩種可用的視訊效果:

  • 背景模糊
  • 以影像取代背景 (外觀比例應為 16:9 才能相容)

若要搭配 Azure 通訊的通話 SDK 使用視訊效果,一旦您建立了 LocalVideoStream,就必須取得 LocalVideoStreamVideoEffects 功能 API,才能啟動/停止視訊效果:

import * as AzureCommunicationCallingSDK from '@azure/communication-calling'; 

import { BackgroundBlurEffect, BackgroundReplacementEffect } from '@azure/communication-calling-effects'; 

// Get the video effects feature api on the LocalVideoStream 
// (here, localVideoStream is the LocalVideoStream object you created while setting up video calling)
const videoEffectsFeatureApi = localVideoStream.feature(AzureCommunicationCallingSDK.Features.VideoEffects); 


// Subscribe to useful events 
videoEffectsFeatureApi.on(‘effectsStarted’, () => { 
    // Effects started
});

videoEffectsFeatureApi.on(‘effectsStopped’, () => { 
    // Effects stopped
}); 

videoEffectsFeatureApi.on(‘effectsError’, (error) => { 
    // Effects error
});

背景模糊

// Create the effect instance 
const backgroundBlurEffect = new BackgroundBlurEffect(); 

// Recommended: Check support by using the isSupported method on the feature API
const backgroundBlurSupported = await videoEffectsFeatureApi.isSupported(backgroundBlurEffect);

if (backgroundBlurSupported) { 
    // Use the video effects feature api we created to start effects
    await videoEffectsFeatureApi.startEffects(backgroundBlurEffect); 
}

以影像取代背景

您必須提供要作為此效果背景的影像 URL。

重要

如果 URL 不是影像的或無法連線/無法讀取,則 startEffects 方法將會失敗。

注意

目前支援的影像格式為:png、jpg、jpeg、tiff、bmp。

目前支援的外觀比例為 16:9。

const backgroundImage = 'https://linkToImageFile'; 

// Create the effect instance 
const backgroundReplacementEffect = new BackgroundReplacementEffect({ 
    backgroundImageUrl: backgroundImage
}); 

// Recommended: Check support by using the isSupported method on the feature API
const backgroundReplacementSupported = await videoEffectsFeatureApi.isSupported(backgroundReplacementEffect);

if (backgroundReplacementSupported) { 
    // Use the video effects feature api as before to start/stop effects 
    await videoEffectsFeatureApi.startEffects(backgroundReplacementEffect); 
}

變更此效果的影像可以透過設定的方法傳遞此影像來完成:

const newBackgroundImage = 'https://linkToNewImageFile'; 

await backgroundReplacementEffect.configure({ 
    backgroundImageUrl: newBackgroundImage
});

切換效果可在視訊效果功能 API 上使用相同的方法來完成:

// Switch to background blur 
await videoEffectsFeatureApi.startEffects(backgroundBlurEffect); 

// Switch to background replacement 
await videoEffectsFeatureApi.startEffects(backgroundReplacementEffect);

如果您隨時想要檢查哪些效果處於作用中狀態,則可以使用 activeEffects 屬性。 activeEffects 屬性會傳回具有目前作用中效果名稱的陣列,如果沒有任何作用中的效果,則會傳回空陣列。

// Using the video effects feature api
const currentActiveEffects = videoEffectsFeatureApi.activeEffects;

若要停止效果:

await videoEffectsFeatureApi.stopEffects();

注意

為了在 iOS 通話 SDK 上使用視訊效果,機器學習模型會下載到客戶的裝置。 我們鼓勵您檢閱應用程式中的隱私權注意事項,並視需要進行相應的更新。

您可以使用視訊效果功能,在視訊通話中將效果新增至您的視訊。 背景模糊為使用者提供移除參與者背後干擾的機制,以便參與者可在沒有背景干擾活動或機密資訊的情況下進行通訊。 此功能在遠距醫療環境中特別有用,其中提供者或患者可能想要遮蔽其周圍環境,以保護敏感性資訊或個人資料。 背景模糊可以套用到所有虛擬約會案例 (包括電話銀行和虛擬聽證會) 以保護使用者隱私權。

使用視訊效果

注意

iOS 上的視訊效果支援僅限於兩個最新主要版本的 iOS。 例如,當發行新的主要 iOS 版本時,iOS 需求是新版本與其之前的最新版本。

目前有一種可用的視訊效果:背景模糊。

LocalVideoEffectsFeature 物件具有下列 API 結構:

  • enable:在 LocalVideoStream 執行個體上啟用視訊效果。
  • disable:在 LocalVideoStream 執行個體上停用視訊效果。
  • isSupported:指出 LocalVideoStream 執行個體上是否支援視訊效果。
  • onVideoEffectEnabled:成功啟用視訊效果時所觸發的事件。
  • onVideoEffectDisabled:成功停用視訊效果時所觸發的事件。
  • onVideoEffectError:視訊效果作業失敗時所觸發的事件。

一旦您具有 LocalVideoEffectsFeature 物件,就可以訂閱事件。這些事件具有下列委派:didEnableVideoEffectdidDisableVideoEffectdidReceiveVideoEffectError

若要搭配 Azure 通訊的通話 SDK 使用視訊效果,一旦您建立了 LocalVideoStream,就必須取得 LocalVideoStreamVideoEffects 功能 API,才能啟用/停用視訊效果:

// Obtain the Video Effects feature from the LocalVideoStream object that is sending the video.
@State var localVideoEffectsFeature: LocalVideoEffectsFeature?
localVideoEffectsFeature = self.localVideoStreams.first.feature(Features.localVideoEffects)
// Subscribe to the events
public func localVideoEffectsFeature(_ videoEffectsLocalVideoStreamFeature: LocalVideoEffectsFeature, didEnableVideoEffect args: VideoEffectEnabledEventArgs) {
        os_log("Video Effect Enabled, VideoEffectName: %s", log:log, args.videoEffectName)
    }
public func localVideoEffectsFeature(_ videoEffectsLocalVideoStreamFeature: LocalVideoEffectsFeature, didDisableVideoEffect args: VideoEffectDisabledEventArgs) {
    os_log("Video Effect Disabled, VideoEffectName: %s", log:log, args.videoEffectName)
}
public func localVideoEffectsFeature(_ videoEffectsLocalVideoStreamFeature: LocalVideoEffectsFeature, didReceiveVideoEffectError args: VideoEffectErrorEventArgs) {
    os_log("Video Effect Error, VideoEffectName: %s, Code: %s, Message: %s", log:log, args.videoEffectName, args.code, args.message)
}

然後開始使用 API 來啟用和停用視訊效果:

localVideoEffectsFeature.enable(effect: backgroundBlurVideoEffect)
localVideoEffectsFeature.disable(effect: backgroundBlurVideoEffect)

背景模糊

背景模糊是一種視訊效果,可讓人員的背景變得模糊。 若要使用背景視訊效果,您必須從有效的 LocalVideoStream 取得 LocalVideoEffectsFeature 功能。

  • 建立新的背景模糊視訊效果物件:
@State var backgroundBlurVideoEffect: BackgroundBlurEffect?
  • 檢查是否支援 BackgroundBlurEffect,並在 videoEffectsFeature 物件上呼叫 Enable
if((localVideoEffectsFeature.isSupported(effect: backgroundBlurVideoEffect)) != nil)
{
    localVideoEffectsFeature.enable(effect: backgroundBlurVideoEffect)
}

若要停用背景模糊視訊效果:

localVideoEffectsFeature.disable(effect: backgroundBlurVideoEffect)

背景取代

背景取代是一種影片效果,可讓用戶設定自己的自定義背景。 若要使用背景取代效果,您必須從有效的 LocalVideoStream取得LocalVideoEffectsFeature功能。

  • 建立新的背景取代視訊效果物件:
@State var backgroundReplacementVideoEffect: BackgroundReplacementEffect?
  • 透過緩衝區傳入影像來設定自定義背景。
let image = UIImage(named:"image.png")
guard let imageData = image?.jpegData(compressionQuality: 1.0) else {
return
}
backgroundReplacementVideoEffect.buffer = imageData
  • 檢查是否支援 BackgroundReplacementEffect,並在 videoEffectsFeature 物件上呼叫 Enable
if((localVideoEffectsFeature.isSupported(effect: backgroundReplacementVideoEffect)) != nil)
{
    localVideoEffectsFeature.enable(effect: backgroundReplacementVideoEffect)
}

若要停用背景取代視訊效果:

localVideoEffectsFeature.disable(effect: backgroundReplacementVideoEffect)

注意

為了在 Android 通話 SDK 上使用視訊效果,機器學習模型會下載到客戶的裝置。 我們鼓勵您檢閱應用程式中的隱私權注意事項,並視需要進行相應的更新。

您可以使用視訊效果功能,在視訊通話中將效果新增至您的視訊。 背景模糊為使用者提供移除參與者背後干擾的機制,以便參與者可在沒有背景干擾活動或機密資訊的情況下進行通訊。 此功能在遠距醫療環境中特別有用,其中提供者或患者可能想要遮蔽其周圍環境,以保護敏感性資訊或個人資料。 背景模糊可以套用到所有虛擬約會案例 (包括電話銀行和虛擬聽證會) 以保護使用者隱私權。

本快速入門基於適用於 Android 的快速入門:將 1:1 視訊通話新增至應用程式建置的。

使用視訊效果

注意

Android 上的視訊效果支援僅限於最後四個主要版本的 Android。 例如,當發行新的主要 Android 版本時,Android 需求是新版本與其之前的三個最新版本。

目前有一種可用的視訊效果:背景模糊。

VideoEffectsLocalVideoStreamFeature 物件具有下列 API 結構:

  • enableEffect:在 LocalVideoStream 執行個體上啟用視訊效果。
  • disableEffect:在 LocalVideoStream 執行個體上停用視訊效果。
  • OnVideoEffectEnabledListener:成功啟用視訊效果時所觸發的事件。
  • OnVideoEffectDisabledListener:成功停用視訊效果時所觸發的事件。
  • OnVideoEffectErrorListener:視訊效果作業失敗時所觸發的事件。

VideoEffectEnabledEventVideoEffectDisabledEventVideoEffectErrorEvent 物件具有下列 API 結構:

  • getVideoEffectName:取得已觸發事件的視訊效果名稱。

一旦您具有 VideoEffectsLocalVideoStreamFeature 物件,就可以訂閱事件:

若要搭配 Azure 通訊的通話 SDK 使用視訊效果,一旦您建立了 LocalVideoStream,就必須取得 LocalVideoStreamVideoEffects 功能 API,才能啟用/停用視訊效果:

// Obtain the Video Effects feature from the LocalVideoStream object that is sending the video.
VideoEffectsLocalVideoStreamFeature videoEffectsFeature = currentVideoStream.feature(Features.LOCAL_VIDEO_EFFECTS);
// Create event handlers for the events
private void handleOnVideoEffectEnabled(VideoEffectEnabledEvent args) {
}
private void handleOnVideoEffectDisabled(VideoEffectDisabledEvent args) {
}
private void handleOnVideoEffectError(VideoEffectErrorEvent args) {
}
 
// Subscribe to the events
videoEffectsFeature.addOnVideoEffectEnabledListener(this::handleOnVideoEffectEnabled);
videoEffectsFeature.addOnVideoEffectDisabledListener(this::handleOnVideoEffectDisabled);
videoEffectsFeature.addOnVideoEffectErrorListener(this::handleOnVideoEffectError);

然後開始使用 API 來啟用和停用視訊效果:

videoEffectsFeature.enableEffect( {{VIDEO_EFFECT_TO_DISABLE}} );
videoEffectsFeature.disableEffect( {{VIDEO_EFFECT_TO_DISABLE}} );

背景模糊

背景模糊是一種視訊效果,可讓人員的背景變得模糊。 若要使用背景視訊效果,您必須從有效的 LocalVideoStream 取得 VideoEffectsLocalVideoStreamFeature 功能。

若要啟用背景模糊視訊效果:

  • 建立一種取得 VideoEFfects 功能和訂閱事件的方法:
private void handleOnVideoEffectEnabled(VideoEffectEnabledEvent args) {
   Log.i("VideoEfects", "Effect enabled for effect " + args.getVideoEffectName());
}
private void handleOnVideoEffectDisabled(VideoEffectDisabledEvent args) {
   Log.i("VideoEfects", "Effect disabled for effect " + args.getVideoEffectName());
}
private void handleOnVideoEffectError(VideoEffectErrorEvent args) {
   Log.i("VideoEfects", "Error " + args.getCode() + ":" + args.getMessage()
           + " for effect " + args.getVideoEffectName());
}

VideoEffectsLocalVideoStreamFeature videoEffectsFeature;
public void createVideoEffectsFeature() {
    videoEffectsFeature = currentVideoStream.feature(Features.LOCAL_VIDEO_EFFECTS);
    videoEffectsFeature.addOnVideoEffectEnabledListener(this::handleOnVideoEffectEnabled);
    videoEffectsFeature.addOnVideoEffectDisabledListener(this::handleOnVideoEffectDisabled);
    videoEffectsFeature.addOnVideoEffectErrorListener(this::handleOnVideoEffectError);
}

  • 建立新的背景模糊視訊效果物件:
BackgroundBlurEffect backgroundBlurVideoEffect = new BackgroundBlurEffect();
  • videoEffectsFeature 物件上呼叫 EnableEffect
public void enableBackgroundBlur() {
    videoEffectsFeature.enableEffect(backgroundBlurEffect);
}

若要停用背景模糊視訊效果:

public void disableBackgroundBlur() {
    videoEffectsFeature.disableEffect(backgroundBlurEffect);
}

背景取代

背景取代是可讓人員取代背景的視訊效果。 若要使用背景視訊效果,您必須從有效的 LocalVideoStream 取得 VideoEffectsLocalVideoStreamFeature 功能。

若要啟用背景取代視訊效果:

  • 建立一種取得 VideoEFfects 功能和訂閱事件的方法:
private void handleOnVideoEffectEnabled(VideoEffectEnabledEvent args) {
   Log.i("VideoEfects", "Effect enabled for effect " + args.getVideoEffectName());
}
private void handleOnVideoEffectDisabled(VideoEffectDisabledEvent args) {
   Log.i("VideoEfects", "Effect disabled for effect " + args.getVideoEffectName());
}
private void handleOnVideoEffectError(VideoEffectErrorEvent args) {
   Log.i("VideoEfects", "Error " + args.getCode() + ":" + args.getMessage()
           + " for effect " + args.getVideoEffectName());
}

VideoEffectsLocalVideoStreamFeature videoEffectsFeature;
public void createVideoEffectsFeature() {
    videoEffectsFeature = currentVideoStream.feature(Features.LOCAL_VIDEO_EFFECTS);
    videoEffectsFeature.addOnVideoEffectEnabledListener(this::handleOnVideoEffectEnabled);
    videoEffectsFeature.addOnVideoEffectDisabledListener(this::handleOnVideoEffectDisabled);
    videoEffectsFeature.addOnVideoEffectErrorListener(this::handleOnVideoEffectError);
}

  • 建立新的背景取代視訊效果物件:
BackgroundReplacementEffect backgroundReplacementVideoEffect = new BackgroundReplacementEffect();
  • 透過緩衝區傳入影像來設定自定義背景。
//example of where we can get an image from, in this case, this is from assets in Android folder
InputStream inputStream = getAssets().open("image.jpg");
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] data = stream.toByteArray();
ByteBuffer dataBuffer = ByteBuffer.allocateDirect(data.length);
dataBuffer.put(data);
dataBuffer.rewind();
backgroundReplacementVideoEffect.setBuffer(dataBuffer);
  • videoEffectsFeature 物件上呼叫 EnableEffect
public void enableBackgroundReplacement() {
    videoEffectsFeature.enableEffect(backgroundReplacementVideoEffect);
}

若要停用背景取代視訊效果:

public void disableBackgroundReplacement() {
    videoEffectsFeature.disableEffect(backgroundReplacementVideoEffect);
}

注意

為了在 Windows 通話 SDK 上使用視訊效果,機器學習模型會下載到客戶的裝置。 我們鼓勵您檢閱應用程式中的隱私權注意事項,並視需要進行相應的更新。

您可以使用視訊效果功能,在視訊通話中將效果新增至您的視訊。 背景模糊為使用者提供移除參與者背後干擾的機制,以便參與者可在沒有背景干擾活動或機密資訊的情況下進行通訊。 此功能在遠距醫療環境中特別有用,其中提供者或患者可能想要遮蔽其周圍環境,以保護敏感性資訊或個人資料。 背景模糊可以套用到所有虛擬約會案例 (包括電話銀行和虛擬聽證會) 以保護使用者隱私權。

本快速入門基於適用於 Windows 的快速入門:將 1:1 視訊通話新增至應用程式建置的。

使用視訊效果

目前有一種可用的視訊效果:背景模糊。

VideoEffectsLocalVideoStreamFeature 物件具有下列 API 結構:

  • EnableEffect:在 LocalVideoStream 執行個體上啟用視訊效果。
  • DisableEffect:在 LocalVideoStream 執行個體上停用視訊效果。
  • VideoEffectEnabled:成功啟用視訊效果時所觸發的事件。
  • VideoEffectDisabledListener:成功停用視訊效果時所觸發的事件。
  • VideoEffectErrorListener:視訊效果作業失敗時所觸發的事件。

VideoEffectEnabledEventVideoEffectDisabledEventVideoEffectErrorEvent 物件具有下列 API 結構:

  • VideoEffectName:取得已觸發事件的視訊效果名稱。

一旦您具有 VideoEffectsLocalVideoStreamFeature 物件,就可以訂閱事件:

若要搭配 Azure 通訊的通話 SDK 使用視訊效果,請將變數新增至 MainPage。

public sealed partial class MainPage : Page
{
    private LocalVideoEffectsFeature localVideoEffectsFeature;
}

一旦您建立了 LocalVideoStream,就必須取得 LocalVideoStreamVideoEffects 功能 API,才能啟用/停用視訊效果。

private async void CameraList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var selectedCamerea = CameraList.SelectedItem as VideoDeviceDetails;
    cameraStream = new LocalOutgoingVideoStream(selectedCamerea);
    InitVideoEffectsFeature(cameraStream);
    
    var localUri = await cameraStream.StartPreviewAsync();
    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
    {
        LocalVideo.Source = MediaSource.CreateFromUri(localUri);
    });
}

public void InitVideoEffectsFeature(LocalOutgoingVideoStream videoStream){
    localVideoEffectsFeature = videoStream.Features.VideoEffects;
    localVideoEffectsFeature.VideoEffectEnabled += LocalVideoEffectsFeature_VideoEffectEnabled;
    localVideoEffectsFeature.VideoEffectDisabled += LocalVideoEffectsFeature_VideoEffectDisabled;
    localVideoEffectsFeature.VideoEffectError += LocalVideoEffectsFeature_VideoEffectError;
}

// Create event handlers for the events
private void LocalVideoEffectsFeature_VideoEffectEnabled(object sender, VideoEffectEnabledEventArgs args)
{
}

private void LocalVideoEffectsFeature_VideoEffectDisabled(object sender, VideoEffectDisabledEventArgs args)
{
}

private void LocalVideoEffectsFeature_VideoEffectError(object sender, VideoEffectErrorEventArgs args)
{
}
 
// Subscribe to the events
videoEffectsFeature.VideoEffectEnabled += VideoEffectsFeature_OnVideoEffectEnabled;
videoEffectsFeature.VideoEffectDisabled += VideoEffectsFeature_OnVideoEffectDisabled;
videoEffectsFeature.VideoEffectError += VideoEffectsFeature_OnVideoEffectError;

然後開始使用 API 來啟用和停用視訊效果:

videoEffectsLocalVideoStreamFeature.EnableEffect( {{VIDEO_EFFECT_TO_ENABLE}} );
videoEffectsLocalVideoStreamFeature.DisableEffect( {{VIDEO_EFFECT_TO_DISABLE}} );

背景模糊

背景模糊是一種視訊效果,可讓人員的背景變得模糊。 若要使用背景視訊效果,您必須從有效的 LocalVideoStream 取得 VideoEffectsLocalVideoStreamFeature 功能。

若要啟用背景模糊視訊效果:

  • BackgroundBlurEffect 執行個體新增至 MainPage。
public sealed partial class MainPage : Page
{
    private BackgroundBlurEffect backgroundBlurVideoEffect = new BackgroundBlurEffect();
}
  • 建立一種取得 VideoEFfects 功能和訂閱事件的方法:
private async void LocalVideoEffectsFeature_VideoEffectEnabled(object sender, VideoEffectEnabledEventArgs e)
{
    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
    {
        BackgroundBlur.IsChecked = true;
    });
}

private async void LocalVideoEffectsFeature_VideoEffectDisabled(object sender, VideoEffectDisabledEventArgs e)
{
    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
    {
        BackgroundBlur.IsChecked = false;
    });
}

private void LocalVideoEffectsFeature_VideoEffectError(object sender, VideoEffectErrorEventArgs e)
{
    String effectName = args.VideoEffectName;
    String errorCode = args.Code;
    String errorMessage = args.Message;

    Trace.WriteLine("VideoEffects VideoEffectError on effect " + effectName + "with code "
        + errorCode + "and error message " + errorMessage);
}
  • 啟用和停用背景模糊效果:
private async void BackgroundBlur_Click(object sender, RoutedEventArgs e)
{
    if (localVideoEffectsFeature.IsEffectSupported(backgroundBlurVideoEffect))
    {
        var backgroundBlurCheckbox = sender as CheckBox;
        if (backgroundBlurCheckbox.IsChecked.Value)
        {
            localVideoEffectsFeature.EnableEffect(backgroundBlurVideoEffect);
        }
        else
        {
            localVideoEffectsFeature.DisableEffect(backgroundBlurVideoEffect);
        }
    }
}

背景取代

背景取代是可讓人員取代背景的視訊效果。 若要使用背景視訊效果,您必須從有效的 LocalVideoStream 取得 VideoEffectsLocalVideoStreamFeature 功能。

若要啟用背景取代視訊效果:

  • BackgroundReplacementEffect 執行個體新增至 MainPage。
public sealed partial class MainPage : Page
{
    private BackgroundReplacementEffect backgroundReplacementVideoEffect = new BackgroundReplacementEffect();
}
  • 建立一種取得 VideoEFfects 功能和訂閱事件的方法:
private async void LocalVideoEffectsFeature_VideoEffectEnabled(object sender, VideoEffectEnabledEventArgs e)
{
    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
    {
        BackgroundReplacement.IsChecked = true;
    });
}

private async void LocalVideoEffectsFeature_VideoEffectDisabled(object sender, VideoEffectDisabledEventArgs e)
{
    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
    {
        BackgroundReplacement.IsChecked = false;
    });
}

private void LocalVideoEffectsFeature_VideoEffectError(object sender, VideoEffectErrorEventArgs e)
{
    String effectName = args.VideoEffectName;
    String errorCode = args.Code;
    String errorMessage = args.Message;

    Trace.WriteLine("VideoEffects VideoEffectError on effect " + effectName + "with code "
        + errorCode + "and error message " + errorMessage);
}
  • 透過緩衝區傳入影像來設定自定義背景。
//example of getting the image from storage folder
MemoryBuffer memoryBuffer = new MemoryBuffer(0);
StorageFolder InstallationFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFile file = InstallationFolder.GetFileAsync("image.jpg").GetAwaiter().GetResult();
if (File.Exists(file.Path))
{
    byte[] imageBytes = File.ReadAllBytes(file.Path);
    memoryBuffer = new MemoryBuffer((uint)imageBytes.Length);
    using (IMemoryBufferReference reference = memoryBuffer.CreateReference())
    {
        byte* dataInBytes;
        uint capacityInBytes;

        (reference.As<IMemoryBufferByteAccess>()).GetBuffer(out dataInBytes, out capacityInBytes);
        for (int i = 0; i < imageBytes.Length; i++)
        {
            dataInBytes[i] = imageBytes[i];
        }
    }
    return memoryBuffer;
}
backgroundReplacementVideoEffect.Buffer = memoryBuffer;
  • 開啟與停用背景取代效果:
private async void BackgroundReplacement_Click(object sender, RoutedEventArgs e)
{
    if (localVideoEffectsFeature.IsEffectSupported(backgroundReplacementVideoEffect))
    {
        var backgroundReplacementCheckbox = sender as CheckBox;
        if (backgroundReplacementCheckbox.IsChecked.Value)
        {
            localVideoEffectsFeature.EnableEffect(backgroundReplacementVideoEffect);
        }
        else
        {
            localVideoEffectsFeature.DisableEffect(backgroundReplacementVideoEffect);
        }
    }
}

下一步

如需詳細資訊,請參閱下列文章: