Windows Audio Application problem with switching between output devices

Hi there. I am having problem with changing the output device after the application starts. Thev application is a simple music player. I have added a menu which uses AudioDeviceSelectorComponent, but changing the output device results like no audio file has been loaded(music stops). If I load a file again, then it works properly.

Also, I have to change playback device from windows volume settings too, while changing it from device selector from my app. How to make the application switch between output devices automatically?

This is the change listener callback


changeListenerCallback(ChangeBroadcaster* source)
{

    if (source == &devman)
    {
        // Remove the current audio callback
        devman.removeAudioCallback(&sourceplayer);

        // Retrieve the new audio device setup
        const juce::AudioDeviceManager::AudioDeviceSetup newDeviceSetup = devman.getAudioDeviceSetup();

        // Reinitialize the sample rate and block size

        const int newSampleRate = newDeviceSetup.sampleRate;
        const int newBlockSize = newDeviceSetup.bufferSize;

        // Prepare for the new audio device configuration
        prepareToPlay(newBlockSize, newSampleRate);

        // Add the audio callback back to the audio device manager
        devman.addAudioCallback(&sourceplayer);

        // Set the source for the source player
        sourceplayer.setSource(&source);

        
        // Restart audio playback if necessary
        if (!devman.getCurrentAudioDeviceType().isEmpty())
        {
            devman.closeAudioDevice();
            devman.restartLastAudioDevice();
        }


        
    }
}