在 C# 脚本中使用 Audio Tap
How to add Audio Taps through C# scripts.
阅读时间3 分钟最后更新于 13 天前
本节介绍如何通过脚本而不是通过编辑器 UI 添加 Audio Tap。
Capture Source Tap 和 Channel Audio Tap
您可以通过 C# 脚本将 Capture Source Tap 和 Channel Audio Tap 添加到场景中。以下示例说明了如何通过脚本为 Vivox Channel Audio Tap 音频添加回声效果:创建一个空游戏对象,然后添加private GameObject _channelAudioGameObject;public void AddChannelAudioEffect(){ // Create the GameObject _channelAudioGameObject = new GameObject("ChannelAudioGameObject"); // Add the Tap to it _channelAudioGameObject.AddComponent<VivoxChannelAudioTap>(); // Also add an echo filter, for example _channelAudioGameObject.AddComponent<AudioEchoFilter>();}
VivoxChannelAudioTapAudioEchoFilterVivoxCaptureSourceTapParticipant Tap 脚本
通过 VivoxParticipant 的类CreateVivoxParticipantTapVivoxService.Instance.ParticipantAddedToChannelCreateVivoxParticipantTapParticipantAddedToChannel请注意,上面的示例脚本不会删除创建的对象。如果需要,可添加逻辑以仅为特定频道成员创建 Tap。 使用using Unity.Services.Vivox;using UnityEngine;public class ParticipantHandler : MonoBehaviour{ void Start() { VivoxService.Instance.ParticipantAddedToChannel += AddParticipantEffect; } void AddParticipantEffect(VivoxParticipant participant) { var gameObject = participant.CreateVivoxParticipantTap("MyNewGameObject"); gameObject.AddComponent<AudioEchoFilter>(); }}
CreateVivoxParticipantTapVivoxParticipant.DestroyVivoxParticipantTap监控 Audio Tap 状态
Audio Tap 具有公开的参数 TapId 可用于监控创建的 Audio Tap 的状态。创建 Tap 后,TapId 的值应大于或等于零,这意味着没有错误。以下是创建 Tap 并确保 TapId 具有预期值的示例:如果 TapId 小于零,则其值可用于排查错误。以下是可能的值及其错误原因:private GameObject _channelAudioGameObject;public void AddChannelAudioEffect(){ // Create the GameObject _channelAudioGameObject = new GameObject("ChannelAudioGameObject"); // Add the Tap to it var tap = _channelAudioGameObject.AddComponent<VivoxChannelAudioTap>(); if (tap.TapId < 0) { Destroy(_channelAudioGameObject); // Destroy the GameObject, since the status is not valid Debug.LogError($"Failed to create VivoxChannelAudioTap"); return; } else { Debug.Log("There was no error creating VivoxChannelAudioTap"); }}
- -1 :此 Tap 当前未注册。当 Tap 已实例化但尚未注册到 Vivox 时,便是这种情况。例如,由于正在等待自动获取频道,可能导致这种情况。
- -1010 :此 Tap 未注册。
- -1011 :此 Tap 试图使用无效的参与者名称进行注册。
- -1012 :此 Tap 试图使用无效的频道名称进行注册。
- -1050 :仅适用于 Vivox Participant Tap。从提供的参与者名称中找不到参与者 URI。
- -1051 :此 Tap 试图注册到 Vivox,但 VivoxService 未初始化。确保在初始化 Audio Tap 之前初始化 VivoxService。
- -1052 :此 Tap 试图使用无效的频道名称注册到 Vivox。确保正确设置频道名称。