This will be basic for most of you but well…I’m stuck.
I want a settings window in my Audio App where I can change input and output sources. I created a child component and created a new AudioDeviceSelectorComponent in this but the changes it makes don’t seem to be detected by the parent. Here is the code for the child component:
I have tried declaring audioDeviceManager outside of the class but that doesn’t seem to work. What is the best way to have one device manager that can be accessed by all components?
You need to pass a pointer or reference to the parent’s AudioDeviceManager to the child component. AudioDeviceManager isn’t a singleton, so the instance you are creating in your child component isn’t the same one that is in the parent component and won’t control the same device settings.
juce::Component::getParent() returns a pointer to a juce::Component. Even if the object the pointer is pointing to is of your custom type, you can’t access it through a juce::Component*.
You would need to dynamic_cast the juce::Component* to your custom type:
if (auto parent = dynamic_cast<CustomComponent*> (getParentComponent())
{
// parent is now a pointer to CustomComponent
}
else
{
// Either this has no parent, or the parent wasn't of the expected type.
}
N.B. this is not the best architecture. If the child needs access to the deviceManager, you should pass a reference to the manager down via a constructor argument.
if (auto* myAudioSelector = findParentComponentOfClass<MyAudioSelector>())
{
auto& deviceManager = myAudioSelector->getAudioDeviceManager();
}
else // this can still return nullptr if there is no parent of that type
{
// ...