File size: 5,332 Bytes
7fd553e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | // Copyright Epic Games, Inc. All Rights Reserved.
#include "LyraSettingValueDiscreteDynamic_AudioOutputDevice.h"
#include "AudioDeviceNotificationSubsystem.h"
#include UE_INLINE_GENERATED_CPP_BY_NAME(LyraSettingValueDiscreteDynamic_AudioOutputDevice)
#define LOCTEXT_NAMESPACE "LyraSettings"
void ULyraSettingValueDiscreteDynamic_AudioOutputDevice::OnInitialized()
{
Super::OnInitialized();
DevicesObtainedCallback.BindUFunction(this, FName("OnAudioOutputDevicesObtained"));
DevicesSwappedCallback.BindUFunction(this, FName("OnCompletedDeviceSwap"));
if (UAudioDeviceNotificationSubsystem* AudioDeviceNotifSubsystem = UAudioDeviceNotificationSubsystem::Get())
{
AudioDeviceNotifSubsystem->DeviceAddedNative.AddUObject(this, &ULyraSettingValueDiscreteDynamic_AudioOutputDevice::DeviceAddedOrRemoved);
AudioDeviceNotifSubsystem->DeviceRemovedNative.AddUObject(this, &ULyraSettingValueDiscreteDynamic_AudioOutputDevice::DeviceAddedOrRemoved);
//AudioDeviceNotifSubsystem->DeviceSwitchedNative.AddUObject(this, &ULyraSettingValueDiscreteDynamic_AudioOutputDevice::DeviceSwitched);
AudioDeviceNotifSubsystem->DefaultRenderDeviceChangedNative.AddUObject(this, &ULyraSettingValueDiscreteDynamic_AudioOutputDevice::DefaultDeviceChanged);
}
UAudioMixerBlueprintLibrary::GetAvailableAudioOutputDevices(this, DevicesObtainedCallback);
}
void ULyraSettingValueDiscreteDynamic_AudioOutputDevice::OnAudioOutputDevicesObtained(const TArray<FAudioOutputDeviceInfo>& AvailableDevices)
{
int32 NewSize = AvailableDevices.Num();
OutputDevices.Reset(NewSize++);
OutputDevices.Append(AvailableDevices);
OptionValues.Reset(NewSize);
OptionDisplayTexts.Reset(NewSize);
// Placeholder - needs to be first option so we can format the default device string later
AddDynamicOption(TEXT(""), FText::GetEmpty());
FString SystemDefaultDeviceName;
for (const FAudioOutputDeviceInfo& DeviceInfo : OutputDevices)
{
if (!DeviceInfo.DeviceId.IsEmpty() && !DeviceInfo.Name.IsEmpty())
{
// System Default
if (DeviceInfo.bIsSystemDefault)
{
SystemDefaultDeviceId = DeviceInfo.DeviceId;
SystemDefaultDeviceName = DeviceInfo.Name;
}
// Current Device
if (DeviceInfo.bIsCurrentDevice)
{
CurrentDeviceId = DeviceInfo.DeviceId;
}
// Add the menu option
AddDynamicOption(DeviceInfo.DeviceId, FText::FromString(DeviceInfo.Name));
}
}
OptionDisplayTexts[0] = FText::Format(LOCTEXT("DefaultAudioOutputDevice", "Default Output - {0}"), FText::FromString(SystemDefaultDeviceName));
SetDefaultValueFromString(TEXT(""));
RefreshEditableState();
//LastKnownGoodIndex = GetDiscreteOptionDefaultIndex();
//SetDiscreteOptionByIndex(GetDiscreteOptionIndex());
}
void ULyraSettingValueDiscreteDynamic_AudioOutputDevice::OnCompletedDeviceSwap(const FSwapAudioOutputResult& SwapResult)
{
//if (SwapResult.Result == ESwapAudioOutputDeviceResultState::Failure)
//{
// UE_LOG(LogLyra, VeryVerbose, TEXT("AudioOutputDevice failure! Resetting to: %s"), *(OptionDisplayTexts[LastKnownGoodIndex].ToString()));
// if (OptionValues.Num() < LastKnownGoodIndex && SwapResult.RequestedDeviceId != OptionValues[LastKnownGoodIndex])
// {
// SetDiscreteOptionByIndex(LastKnownGoodIndex);
// }
// // Remove the invalid device
// if (SwapResult.RequestedDeviceId != SystemDefaultDeviceId)
// {
// OutputDevices.RemoveAll([&SwapResult](FAudioOutputDeviceInfo& Device)
// {
// return Device.DeviceId == SwapResult.RequestedDeviceId;
// });
// RemoveDynamicOption(SwapResult.RequestedDeviceId);
// RefreshEditableState();
// }
//}
}
void ULyraSettingValueDiscreteDynamic_AudioOutputDevice::DeviceAddedOrRemoved(FString DeviceId)
{
UAudioMixerBlueprintLibrary::GetAvailableAudioOutputDevices(this, DevicesObtainedCallback);
}
void ULyraSettingValueDiscreteDynamic_AudioOutputDevice::DefaultDeviceChanged(EAudioDeviceChangedRole InRole, FString DeviceId)
{
UAudioMixerBlueprintLibrary::GetAvailableAudioOutputDevices(this, DevicesObtainedCallback);
}
void ULyraSettingValueDiscreteDynamic_AudioOutputDevice::SetDiscreteOptionByIndex(int32 Index)
{
Super::SetDiscreteOptionByIndex(Index);
//UE_LOG(LogLyra, VeryVerbose, TEXT("AudioOutputDevice set to %s - %s"), *(OptionDisplayTexts[Index].ToString()), *OptionValues[Index]);
//bRequestDefault = false;
//FString RequestedAudioDeviceId = GetValueAsString();
//// Grab the correct deviceId if the user has selected default
//const int32 DefaultOptionIndex = GetDiscreteOptionDefaultIndex();
//if (Index == DefaultOptionIndex)
//{
// RequestedAudioDeviceId = SystemDefaultDeviceId;
//}
//// Only swap if the requested deviceId is different than our current
//if (RequestedAudioDeviceId == CurrentDeviceId)
//{
// LastKnownGoodIndex = Index;
// UE_LOG(LogLyra, VeryVerbose, TEXT("AudioOutputDevice (Not Swapping) - LKG set to index :%d"), LastKnownGoodIndex);
//}
//else
//{
// bRequestDefault = (Index == DefaultOptionIndex);
// UAudioMixerBlueprintLibrary::SwapAudioOutputDevice(LocalPlayer, RequestedAudioDeviceId, DevicesSwappedCallback);
// UE_LOG(LogLyra, VeryVerbose, TEXT("AudioOutputDevice requesting %s"), *RequestedAudioDeviceId);
//}
}
#undef LOCTEXT_NAMESPACE
|