当音频会话以NAudio开始和结束时,我想收到回调。以下代码正在运行:
private void SetupMediaSessionCallbacks()
{
// Foreach output endpoint
foreach (var md in new MMDeviceEnumerator().EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active))
{
md.AudioSessionManager.OnSessionCreated += OnSessionCreated;
}
}
此代码在创建新音频会话时触发。我是NAudio的新手,所以我不明白如何设置RegisterEventClient来接收状态变化事件:
private void OnSessionCreated(object sender, IAudioSessionControl newSession)
{
// Not working, need help here!
AudioSessionControl audioSession = new AudioSessionControl(newSession);
IAudioSessionEventsHandler handler = null;
AudioSessionEventsCallback notifications = new AudioSessionEventsCallback(test);
handler.OnStateChanged += new EventHandler<AudioSessionState>(notifications_StateChanged);
audioSession.RegisterEventClient(handler);
}
这是回调,我认为这是正确的:
void notifications_StateChanged(object sender, AudioSessionState newState)
{
if (newState == AudioSessionState.AudioSessionStateActive)
{
Console.WriteLine("StateChanged");
}
}
非常感谢任何帮助。为此,我搜索了高低文档。
答案 0 :(得分:1)
想出来。我需要创建自己的继承自IAudioSessionEventsHandler的类。这是OnSessionCreated的代码:
private void OnSessionCreated(object sender, IAudioSessionControl newSession)
{
AudioSessionControl audioSession = new AudioSessionControl(newSession);
NAudioEventCallbacks callbacks = new NAudioEventCallbacks();
AudioSessionEventsCallback notifications = new AudioSessionEventsCallback(callbacks);
audioSession.RegisterEventClient(callbacks);
}
public class NAudioEventCallbacks : IAudioSessionEventsHandler
{
public void OnChannelVolumeChanged(uint channelCount, IntPtr newVolumes, uint channelIndex) { }
public void OnDisplayNameChanged(string displayName) { }
public void OnGroupingParamChanged(ref Guid groupingId) { }
public void OnIconPathChanged(string iconPath) { }
public void OnSessionDisconnected(AudioSessionDisconnectReason disconnectReason) { }
public void OnStateChanged(AudioSessionState state) { }
public void OnVolumeChanged(float volume, bool isMuted) { }
}
我现在可以在NAudioEventCallbacks中设置符合预期的断点。