Using a Photo-resistor as a switch for a SD card reader

I am new to Arduino and coding.
My project is to make a simple MP3 player, that plays .wav music files from a SD card reader. I have so far been able to play the music using two 8ohm speakers. I have mostly set it up using the info from this website https://diyhacking.com/arduino-audio-player/.

I wanted to add a photo-resistor to the circuit, so that it will only play when there is sufficient light.
The problem I am having is that most forums explain how to use a photo-resistor for a LED and therefore are using pinMode() and digitalWrite(), these don't seem to work with the code that I have used for the 'MP3' player.

I have a copied the code I am trying to use at the moment, below.

int sensorPin = A0;   // select the input pin for ldr
int sensorValue=0 ;  //variable to store the value coming from the sensor

      
#include <SD.h>                      // need to include the SD library
#define SD_ChipSelectPin 4  //using digital pin 4 on arduino nano 328
#include <TMRpcm.h>           //  also need to include this library...
#include <SPI.h>

TMRpcm tmrpcm;   // create an object for use in this sketch

void setup() { 

 tmrpcm.speakerPin = 9; //11 on Mega, 9 on Uno, Nano, etc   

   if (!SD.begin(SD_ChipSelectPin)){   // see if the card is present and can be initialized:
  return;   // don't do anything more if not
     }
      tmrpcm.volume(1);
     tmrpcm.play("11.wav");
} 

void loop() {
  
  sensorValue = analogRead(sensorPin); 
 if(sensorValue >300){ //setting a threshold value
     digitalWrite(9,HIGH);
}
  else digitalWrite(9,LOW); //turn relay OFF 


   }

What does your code actually do?

You say you have code that plays songs through some speakers. Is that the code in your Original Post? if not please also post the code that plays the songs.

What is connected to pin 9 and how does that affect the playing of a song?

Why is all the code to play the WAV file in setup()?

There should be no difficult preventing music from playing when the light level is too low.

Post a diagram showing how you have the LDR connected.

...R

I dunno about photo-resistor but photo-transistor only conducts as much as light falls on it. You could work that into your power circuit but one question please:

How much light do you need to listen to music?

You could put the light detector in series with a resistor to create a voltage divider and take the voltage at the junction to an analog input pin. In the program you can then compare the value from the analog pin to some preset value to decide when the light is insufficient.