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

标签 audio naudio

我想在音频 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/

相关文章:

Android:来自音频的外部硬件中断

python - 如何选择要录制的设备(Python PyAudio)

c# - 带有语音的 Lex 聊天机器人 C# 客户端

c# - 使用 NAudio 改变左右声道的声音平衡

c# - 重新采样环回捕获

Python openAL 3D音效

c# - C#使用SoundPlayer同时播放多种声音

c# - wma压缩音频文件使用Naudio抛出错误

java - android studio 改变 API<23 的音频播放速度

c# - 实现媒体监控(例如广播广告监控)