Skip to content

Instantly share code, notes, and snippets.

@erikas-taroza
Last active July 26, 2022 12:49
Show Gist options
  • Save erikas-taroza/cb33ed4ad888f750a8f8549215fbe2be to your computer and use it in GitHub Desktop.
Save erikas-taroza/cb33ed4ad888f750a8f8549215fbe2be to your computer and use it in GitHub Desktop.
Sets the display name of the Flutter app in Windows' mixer. Solves the issue of the name not changing in the mixer without setting the user agent (DartVLC).
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'package:win32/win32.dart';
/// Documentation:
/// Dart Win32 Example - https://github.com/timsneath/win32/blob/main/example/wasapi.dart
///
/// Win32Api IMMDeviceEnumerator - https://docs.microsoft.com/en-us/windows/win32/api/mmdeviceapi/nn-mmdeviceapi-immdeviceenumerator
///
/// Win32Api IMMDevice - https://docs.microsoft.com/en-us/windows/win32/api/mmdeviceapi/nn-mmdeviceapi-immdevice
///
/// Win32Api IAudioClient - https://docs.microsoft.com/en-us/windows/win32/api/audioclient/nn-audioclient-iaudioclient
///
/// Win32Api IAudioSessionControl - https://docs.microsoft.com/en-us/windows/win32/api/audiopolicy/nn-audiopolicy-iaudiosessioncontrol
///
/// This is done with win32 v2.6.1
void setDisplayName(String name) {
// Get the default audio device.
Pointer<COMObject> ptrIMMDE = COMObject.createFromID(CLSID_MMDeviceEnumerator, IID_IMMDeviceEnumerator);
IMMDeviceEnumerator immde = IMMDeviceEnumerator(ptrIMMDE);
Pointer<Pointer<COMObject>> ptrIMMD = calloc<Pointer<COMObject>>();
immde.GetDefaultAudioEndpoint(0, 0, ptrIMMD);
// Active an IAudioClient for the output device.
IMMDevice immd = IMMDevice(ptrIMMD.cast());
Pointer<Pointer<COMObject>> ptrIAC = calloc<Pointer<COMObject>>();
Pointer<GUID> ptrIACGuid = convertToIID(IID_IAudioClient);
immd.Activate(ptrIACGuid, CLSCTX_ALL, nullptr, ptrIAC);
// Initialize IAudioClient and get the IAudioSessionControl service.
IAudioClient iac = IAudioClient(ptrIAC.cast());
Pointer<Pointer<WAVEFORMATEX>> ptrMixFormat = calloc<Pointer<WAVEFORMATEX>>();
iac.GetMixFormat(ptrMixFormat);
iac.Initialize(0, 0, 30000, 0, ptrMixFormat.value, nullptr);
Pointer<Pointer<COMObject>> ptrIASC = calloc<Pointer<COMObject>>();
Pointer<GUID> ptrIASCGuid = convertToIID(IID_IAudioSessionControl);
iac.GetService(ptrIASCGuid, ptrIASC);
// Set the display name.
IAudioSessionControl iasc = IAudioSessionControl(ptrIASC.cast());
iasc.SetDisplayName(name.toNativeUtf16(), nullptr);
// Release resources.
calloc.free(ptrIMMDE);
calloc.free(ptrIMMD);
calloc.free(ptrIAC);
calloc.free(ptrIACGuid);
calloc.free(ptrMixFormat);
calloc.free(ptrIASC);
calloc.free(ptrIASCGuid);
immde.Release();
immd.Release();
iac.Release();
iasc.Release();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment