Skip to content

Commit f0a0c1a

Browse files
committed
Networked: ability to mute microphone
1 parent ad10a7d commit f0a0c1a

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/XRSharp.CommunityToolkit.Networked/NetworkedScene.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Windows;
23
using XRSharp.Components;
34
using XRSharp.Controls;
45
using XRSharp.Primitives;
@@ -70,6 +71,40 @@ public class NetworkedScene : RootComponent
7071
/// </summary>
7172
public FrameworkElement3D Avatar { get; set; } = new Box { SizeX = 0.3, SizeY = 0.5, SizeZ = 0.3, Color = RandomColor.Generate() };
7273

74+
#region IsMicrophoneEnabled
75+
public bool IsMicrophoneEnabled
76+
{
77+
get => (bool)GetValue(IsMicrophoneEnabledProperty);
78+
set => SetValue(IsMicrophoneEnabledProperty, value);
79+
}
80+
81+
public static readonly DependencyProperty IsMicrophoneEnabledProperty =
82+
DependencyProperty.Register(nameof(IsMicrophoneEnabled), typeof(bool), typeof(NetworkedScene), new PropertyMetadata(true, OnIsMicrophoneEnabledChanged));
83+
84+
private static void OnIsMicrophoneEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
85+
{
86+
var component = (NetworkedScene)d;
87+
88+
if (component.Audio && component.IsInitialized)
89+
{
90+
component.UpdateMicState();
91+
}
92+
}
93+
94+
private void UpdateMicState()
95+
{
96+
Interop.ExecuteJavaScriptVoid($@"
97+
if (NAF.connection.isConnected()) {{
98+
NAF.connection.adapter.enableMicrophone({IsMicrophoneEnabled.ToLowerString()});
99+
}}
100+
else {{
101+
document.body.addEventListener('clientConnected', function (evt) {{
102+
NAF.connection.adapter.enableMicrophone({IsMicrophoneEnabled.ToLowerString()})
103+
}});
104+
}}");
105+
}
106+
#endregion
107+
73108
// todo: add onConnect, events
74109

75110
protected override void Init()
@@ -120,6 +155,11 @@ protected override void Init()
120155
SetAvatarToCamera();
121156
}
122157
}
158+
159+
if (Audio)
160+
{
161+
UpdateMicState();
162+
}
123163
}
124164

125165
private void SetAvatarToCamera()

0 commit comments

Comments
 (0)