使用NAudio接收状态改变的音频会话回调



我想接收回调时音频会话开始和结束与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");
        }
    }

任何帮助都将非常感激。

明白了。我需要创建我自己的类,从iaudiosessioneventhandler继承。下面是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中设置断点,按预期工作

相关内容

  • 没有找到相关文章

最新更新