Audio recorder using audioFormatWriter crashing application

Hi guys, I am quite new at this and I’m stuck creating a feature for my app where I have 2 MP3 tracks playing in a mixer source and I want to have a start and stop recording button which will record said mixer source audio and produce a .wav audio file. I have tried using the AudioFormatWriter class (pretty sure I am missing some steps or doing some stuff wrong), attempting to use both writeFromAudioSource() and writeFromAudioSampleBuffer() but both giving me a negative outcome where the mixed mp3 tracks start to distort and stutter and the app will crash. A wav file does end up getting created but its unusually large in file size and there seems to be no actual audio in it. Hoping someone can help guide me on what I am doing wrong. Below is the main function im planning to use to record:

void AudioRecorder::startRecording()
{
    if (isRecording == false)
    {
        isRecording = true;
        outputFilePath = createFile();

        // Create the AudioFormatWriter and prepare it for writing
        writer.reset(juce::WavAudioFormat().createWriterFor(
            new juce::FileOutputStream(outputFilePath),
            44100.0, // Sample rate
            2, // Channel layout
            16, // Bits per sample
            juce::StringPairArray(), // Metadata
            0 // Quality option index
        ));
        if (writer)
        {
            const int bufferSize = 512; // Adjust the buffer size as needed

            // Loop to capture and write audio data
            while (isRecording)
            {
                // Read and write audio data from the mixer source
                if (!writer->writeFromAudioSource(mixerSource, bufferSize))
                {
                    // Handle error here if writing fails
                }
            }
        }
    }
}