gpt4 book ai didi

audio - 使用NAudio接收状态更改的 Audio Session 回调

转载 作者:行者123 更新时间:2023-12-03 00:37:44 35 4
gpt4 key购买 nike

我想在音频 session 以NAudio开始和结束时接收回调。以下代码正在运行:

    private void SetupMediaSessionCallbacks()
{
// Foreach output endpoint
foreach (var md in new MMDeviceEnumerator().EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active))
{
md.AudioSessionManager.OnSessionCreated += OnSessionCreated;
}
}

创建新的音频 session 时,将触发此代码。我是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");
}
}

任何帮助将不胜感激。我一直在搜索有关此问题的文档。

最佳答案

弄清楚了。我需要创建自己的类,该类继承自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中设置可以正常工作的断点。

关于audio - 使用NAudio接收状态更改的 Audio Session 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39011567/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com