I wrote a short text to play the audio file. But when running, the program gives error permission. I noticed that every time I run the program, the name of the file that has the permission error is different This file is in the temp path I checked this path and there was no file with that name
Program text:
from pydub import AudioSegment
from pydub.playback import play
try:
song=AudioSegment.from_wav("myfile.wav")
except IOError:
print("can not open file")
try:
play(song)
except IOError:
print("can not play file")
The output of the program was as follows:
My Project Python/my project python/play wav.py"
can not play file
When I try the program without the block Try: , the output is as follows:
Traceback (most recent call last):
File "d:\My Project Python\my project python\play wav.py", line 7, in <module>
play(song)
File "C:\Users\H&M\AppData\Local\Programs\Python\Python310\lib\site-packages\pydub\playback.py", line 71, in play _play_with_ffplay(audio_segment)
File "C:\Users\H&M\AppData\Local\Programs\Python\Python310\lib\site-packages\pydub\playback.py", line 15, in _play_with_ffplay
seg.export(f.name, "wav")
File "C:\Users\H&M\AppData\Local\Programs\Python\Python310\lib\site-packages\pydub\audio_segment.py", line 867, in export
out_f, _ = _fd_or_path_or_tempfile(out_f, 'wb ')
File "C:\Users\H&M\AppData\Local\Programs\Python\Python310\lib\site-packages\pydub\utils.py", line 60, in _fd_or_path_or_tempfile
fd = open(fd, mode=mode)
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\H&M\\AppData\\Local\\Temp\\tmp7t2o2ta1.wav'
CodePudding user response:
I found an answer for this question, but it is not an optimized answer
def _play_with_ffplay(seg):
PLAYER = get_player_name()
with NamedTemporaryFile("w b", suffix=".wav") as f:
fileName = f.name
seg.export(fileName, "wav")
subprocess.call([PLAYER, "-nodisp", "-autoexit", "-hide_banner", fileName])
os.remove(fileName)
I made these changes in the playback.py file and got the answer . link
CodePudding user response:
This also works:
def _play_with_ffplay(seg):
PLAYER = get_player_name()
with NamedTemporaryFile("w b", suffix=".wav") as f:
f.close()
seg.export(f.name, "wav")
subprocess.call([PLAYER, "-nodisp", "-autoexit", "-hide_banner", f.name])
Just close the file with the f.close() command