-1
import tempfile
import os
import random
import tkinter as tk
from pydub import AudioSegment
from pydub.playback import play

FFMPEG_PATH = "C:/ffmpeg/bin/ffmpeg.exe"
os.environ["PATH"] += os.pathsep + os.path.dirname(FFMPEG_PATH)

# Load the sound effects
win_sound = AudioSegment.from_file("win_sound.mp3", format="mp3", ffmpeg=FFMPEG_PATH)
lose_sound = AudioSegment.from_file("lose_sound.mp3", format="mp3", ffmpeg=FFMPEG_PATH)
tie_sound = AudioSegment.from_file("tie_sound.mp3", format="mp3", ffmpeg=FFMPEG_PATH)

# create temporary files for the output
with tempfile.NamedTemporaryFile(suffix=".wav") as win_file:
    with tempfile.NamedTemporaryFile(suffix=".wav") as lose_file: 
        with tempfile.NamedTemporaryFile(suffix=".wav") as tie_file:

 # export the audio to the temporary files
            win_sound.export(win_file.name, format="wav")
lose_sound.export(lose_file.name, format="wav")
tie_sound.export(tie_file.name, format="wav")



# Play sounds 
    # Play sound effect and animate the result

play(AudioSegment.from_file(win_file.name))
play(AudioSegment.from_file(lose_file.name))
play(AudioSegment.from_file(tie_file.name))

Output

Traceback (most recent call last): File "c:\Users\IK959\code\idk.py", line 22, in win_sound.export(win_file.name, format="wav") File "C:\Users\IK959\code.venv\lib\site-packages\pydub\audio_segment.py", line 867, in export out_f, _ = _fd_or_path_or_tempfile(out_f, 'wb+') PermissionError: [Errno 13] Permission denied: 'C:\Users\IK959\AppData\Local\Temp\tmpmf92cajm.wav'

I tried fixing it by making my own temp files but i cant figure it out

Marcelo Paco
  • 2,732
  • 4
  • 9
  • 26
  • Do you have **write** access to `C:\Users\IK959\AppData\Local\Temp\ ` ? – Marcelo Paco Mar 14 '23 at 23:40
  • Creating `NamedTemporaryFile` in the with-statements already opens the temporary files. Calling `export` with the filename tries to open the open file again. Try to give the object `win_file` to `export` instead of its name. – Michael Butscher Mar 14 '23 at 23:49
  • It's unclear. You should describe what's the problem in English, instead of throwing the code to me. Why are you using the tempfile module? Why not just give a ```AudioSegment``` instance to the ```play()```? Also, remove the unrelated operating-system and tkinter tags. – relent95 Mar 16 '23 at 06:41
  • Also, you are using the ```tempfile``` API incorrectly. A file represented by a ```NamedTemporaryFile``` will be deleted when it is closed. – relent95 Mar 16 '23 at 06:50
  • Those temporary files are not necessary, remove related codes. Just pass those `xxx_sound` to `play()`. However it seems like a bug of `pydub.playback.play()` when `ffplay` is used to play the sound object. Try installing `simpleaudio` module (strongly recommended in `pydub` website) to play the sound object instead. – acw1668 Mar 16 '23 at 07:54
  • r'C:\Users\IK959\AppData\Local\Temp\' – toyota Supra Mar 19 '23 at 14:46

1 Answers1

0

Just "pip install pyAudio" solved the problem for me