1
-
2
- Imports System.Runtime.InteropServices
3
- Imports NAudio.CoreAudioApi.Interfaces
4
- Imports VBAudioRouter.Interop
1
+ Imports NAudio.CoreAudioApi.Interfaces
5
2
6
3
Namespace Controls
7
4
8
5
Public NotInheritable Class AudioSessionControl
9
6
Inherits UserControl
10
- Implements IAudioEndpointVolumeCallback, IAudioSessionEvents
7
+ Implements IAudioSessionEvents
11
8
12
- Public ReadOnly Property VolumeManager As IAudioEndpointVolume
13
- Public ReadOnly Property MeterInformation As IAudioMeterInformation
14
9
Public ReadOnly Property AudioSession As IAudioSessionControl
10
+ Public ReadOnly Property AudioSessionControl As NAudio.CoreAudioApi.AudioSessionControl
11
+ Public ReadOnly Property AudioMeterInformation As NAudio.CoreAudioApi.AudioMeterInformation
12
+ Public ReadOnly Property SimpleAudioVolume As NAudio.CoreAudioApi.SimpleAudioVolume
15
13
16
14
Public ReadOnly SpeakerControlPageInstance As SpeakerControlPage
17
15
18
16
Friend Sub New (parent As SpeakerControlPage, audioSession As IAudioSessionControl)
19
17
InitializeComponent()
20
18
21
19
Me .SpeakerControlPageInstance = parent
22
- Me .AudioSession = audioSession
20
+ Me .AudioSessionControl = New NAudio.CoreAudioApi.AudioSessionControl(audioSession)
21
+ AudioMeterInformation = AudioSessionControl.AudioMeterInformation
22
+ SimpleAudioVolume = AudioSessionControl.SimpleAudioVolume
23
23
24
- VolumeManager = DirectCast (audioSession, IAudioEndpointVolume)
25
- MeterInformation = DirectCast (audioSession, IAudioMeterInformation)
26
-
27
- VolumeManager.RegisterControlChangeNotify( Me )
28
- OnNotify(IntPtr.Zero)
24
+ VolumeSlider.Value = SimpleAudioVolume.Volume * 100
25
+ DisplayNameTextBlock.Text = AudioSessionControl.DisplayName
29
26
30
27
Dim timer As New Timers.Timer()
31
28
timer.Interval = 30
32
29
AddHandler timer.Elapsed, Sub ()
33
- Dim unused = Dispatcher.RunIdleAsync( Sub ()
34
- Dim meters As Single () = New Single (MeterInformation.GetMeteringChannelCount() - 1 ) {}
35
- Dim metersRef = GCHandle.Alloc(meters, GCHandleType.Pinned)
36
- MeterInformation.GetChannelsPeakValues(meters.Length, metersRef.AddrOfPinnedObject)
37
- metersRef.Free()
38
- LeftMeter.ScaleY = meters( 0 )
39
- RightMeter.ScaleY = meters( 1 )
40
- End Sub )
30
+ Dim unused = Dispatcher?.RunIdleAsync( Sub ()
31
+ Dim peakValues = AudioMeterInformation.PeakValues
32
+ If peakValues.Count > 0 Then
33
+ LeftMeter.ScaleX = peakValues( 0 )
34
+ End If
35
+ If peakValues.Count > 1 Then
36
+ RightMeter.ScaleX = peakValues( 1 )
37
+ End If
38
+ End Sub )
41
39
End Sub
42
40
timer.Enabled = True
43
41
44
42
audioSession.RegisterAudioSessionNotification( Me )
45
43
End Sub
46
44
47
45
Private Sub AudioSessionControl_Unloaded(sender As Object , e As RoutedEventArgs) Handles Me .Unloaded
48
- VolumeManager?.UnregisterControlChangeNotify( Me )
49
46
AudioSession?.UnregisterAudioSessionNotification( Me )
50
47
End Sub
51
48
52
49
Dim oldValue As Double = - 1
53
50
Private Sub VolumeSlider_ValueChanged(sender As Object , e As RangeBaseValueChangedEventArgs)
54
- If VolumeManager Is Nothing Or oldValue = VolumeSlider.Value Then Exit Sub
51
+ If SimpleAudioVolume Is Nothing Or oldValue = VolumeSlider.Value Then Exit Sub
55
52
oldValue = VolumeSlider.Value
56
- VolumeManager.SetMasterVolumeLevelScalar(Convert.ToSingle( VolumeSlider.Value / 100 ), Guid.Empty )
53
+ SimpleAudioVolume.Volume = CType ( VolumeSlider.Value / 100.0F , Single )
57
54
End Sub
58
55
59
56
Dim isMuted As Boolean = False
60
57
Private Sub MuteButton_Click(sender As Object , e As RoutedEventArgs)
61
- If VolumeManager Is Nothing Then Exit Sub
58
+ If SimpleAudioVolume Is Nothing Then Exit Sub
62
59
63
60
If sender IsNot Nothing Then
64
61
isMuted = Not isMuted
65
- VolumeManager.SetMute(isMuted, Guid.Empty)
62
+ SimpleAudioVolume.Mute = isMuted
66
63
End If
67
64
68
65
If isMuted Then
@@ -72,40 +69,45 @@ Namespace Controls
72
69
End If
73
70
End Sub
74
71
75
- Public Sub OnNotify(notifyData As IntPtr) Implements IAudioEndpointVolumeCallback.OnNotify
76
- Dim unused = Dispatcher.RunIdleAsync( Sub ()
77
- VolumeSlider.Value = VolumeManager.GetMasterVolumeLevelScalar() * 100
78
- isMuted = VolumeManager.GetMute()
79
- MuteButton_Click( Nothing , Nothing )
80
- End Sub )
81
- End Sub
82
-
83
72
Public Function OnDisplayNameChanged(displayName As String , ByRef eventContext As Guid) As Integer Implements IAudioSessionEvents.OnDisplayNameChanged
73
+ DisplayNameTextBlock.Text = AudioSessionControl.DisplayName
84
74
75
+ Return 0
85
76
End Function
86
77
87
78
Public Function OnIconPathChanged(iconPath As String , ByRef eventContext As Guid) As Integer Implements IAudioSessionEvents.OnIconPathChanged
88
-
79
+ Return 0
89
80
End Function
90
81
91
82
Public Function OnSimpleVolumeChanged(volume As Single , isMuted As Boolean , ByRef eventContext As Guid) As Integer Implements IAudioSessionEvents.OnSimpleVolumeChanged
92
-
83
+ Dispatcher.RunIdleAsync( Sub ()
84
+ If Not oldValue = volume * 100 Then
85
+ oldValue = volume * 100
86
+ VolumeSlider.Value = volume * 100
87
+ End If
88
+ Me .isMuted = isMuted
89
+ MuteButton_Click( Nothing , Nothing )
90
+ End Sub )
91
+
92
+ Return 0
93
93
End Function
94
94
95
95
Public Function OnChannelVolumeChanged(channelCount As UInteger, newVolumes As IntPtr, channelIndex As UInteger, ByRef eventContext As Guid) As Integer Implements IAudioSessionEvents.OnChannelVolumeChanged
96
-
96
+ Return 0
97
97
End Function
98
98
99
99
Public Function OnGroupingParamChanged( ByRef groupingId As Guid, ByRef eventContext As Guid) As Integer Implements IAudioSessionEvents.OnGroupingParamChanged
100
-
100
+ Return 0
101
101
End Function
102
102
103
103
Public Function OnStateChanged(state As AudioSessionState) As Integer Implements IAudioSessionEvents.OnStateChanged
104
-
104
+ Return 0
105
105
End Function
106
106
107
107
Public Function OnSessionDisconnected(disconnectReason As AudioSessionDisconnectReason) As Integer Implements IAudioSessionEvents.OnSessionDisconnected
108
- SpeakerControlPageInstance.AudioSessions.Remove( Me )
108
+ Dispatcher.RunIdleAsync( Sub () SpeakerControlPageInstance.AudioSessions.Remove( Me ))
109
+
110
+ Return 0
109
111
End Function
110
112
End Class
111
113
0 commit comments