9

素晴らしいNAudioフレームワークを使用して、オーディオ デバイスのリストを取得しています。

しかし、私が見ているように、どのオーディオ デバイスが PC の統合オーディオであり、どのオーディオ デバイスがヘッドフォンであるかの違いはあり得ません。つまり、それらは同じ名前を持ち、ヘッドフォンを接続した場合にのみActive状態になります。

ヘッドフォンを接続した状態でアプリケーションを起動した場合、現在のデバイスがヘッドフォンであり、PC の内蔵オーディオではないかどうかをどのように知ることができるでしょうか?

つまり、接続されたオーディオ デバイスが外部オーディオ デバイスであり、ヘッドフォン自体であることを NAduio 経由で検出できますか?

var enumerator = new NAudio.CoreAudioApi.MMDeviceEnumerator();

// Allows you to enumerate rendering devices in certain states
var endpoints = enumerator.EnumerateAudioEndPoints(
    DataFlow.Render,
    DeviceState.Unplugged | DeviceState.Active);
foreach (var endpoint in endpoints)
{
    Console.WriteLine("{0} - {1}", endpoint.DeviceFriendlyName, endpoint.State);
}

// Aswell as hook to the actual event
enumerator.RegisterEndpointNotificationCallback(new NotificationClient());

NotificationClient は次のように実装されます。

class NotificationClient : NAudio.CoreAudioApi.Interfaces.IMMNotificationClient
{
    void IMMNotificationClient.OnDeviceStateChanged(string deviceId, DeviceState newState)
    {
        Console.WriteLine("OnDeviceStateChanged\n Device Id -->{0} : Device State {1}", deviceId, newState);
    }

    void IMMNotificationClient.OnDeviceAdded(string pwstrDeviceId) { }
    void IMMNotificationClient.OnDeviceRemoved(string deviceId) { }
    void IMMNotificationClient.OnDefaultDeviceChanged(DataFlow flow, Role role, string defaultDeviceId) { }
    void IMMNotificationClient.OnPropertyValueChanged(string pwstrDeviceId, PropertyKey key) { }
}
4

2 に答える 2

3

投稿されたスニペットは不完全です。(おそらく新しい) デフォルトのオーディオ デバイスのデバイス プロパティを確認する必要があります。特にフォーム ファクタでは、ヘッドセットまたはヘッドフォンを検出する必要があります。おおよそ次のようになります。

void IMMNotificationClient.OnDeviceStateChanged(string deviceId, DeviceState newState) {
    Console.WriteLine("OnDeviceStateChanged\n Device Id -->{0} : Device State {1}", deviceId, newState);
    var endp = new NAudio.CoreAudioApi.MMDeviceEnumerator().GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
    bool isHeadPhone = false;
    PropertyKey key = PropertyKeys.PKEY_AudioEndpoint_FormFactor;
    var store = endp.Properties;
    for (var index = 0; index < store.Count; index++) {
        if (store.Get(index).Equals(key)) {
            var value = (uint)store.GetValue(index).Value;
            const uint formHeadphones = 3;
            const uint formHeadset = 5;
            if (value == formHeadphones || value == formHeadset) {
                isHeadPhone = true;
                break;
            }
        }
    }
    // Use isHeadPhone
    // etc...

}
于 2016-02-24T16:52:35.567 に答える