Here's my code. The function is often called with the same file name to play. Each call creates several temporary files. How to avoid this? or create one temporary file for each call with the same name?
def play_sound(file_name):
path_file=os.path.join(WAV_FLDR, file_name)
path_file+= ".ogg"
if os.path.exists(path_file):
ogg_audio = AudioSegment.from_ogg(path_file)
sound =ogg_audio.apply_gain(-ogg_audio.max_dBFS)
silence_threshold = -45
nonsilent_ranges=silence.split_on_silence(sound,silence_thresh=silence_threshold,min_silence_len=80)
cnt=0
for sound in nonsilent_ranges:
cnt+=1
play(sound)
else:
print(f"File didnt finder {path_file}")