-1

Basically I'm trying to write a code that opens a sound file using python, but all I get is this error. using MacOS and Python 3.10. tried many solutions but no luck.

This is the code I’ve tried first:

from playsound import playsound
playsound('/Users/cairo/Desktop/i wish.mp3')
    /Users/cairo/PycharmProjects/pythonProject/venv/bin/python /Users/cairo/PycharmProjects/pythonProject/main.py 
playsound is relying on a python 2 subprocess. Please use `pip3 install PyObjC` if you want playsound to run more efficiently.
Traceback (most recent call last):
  File "/Users/cairo/PycharmProjects/pythonProject/main.py", line 3, in <module>
    playsound(r'/Users/cairo/Desktop/beep sound 2')
  File "/Users/cairo/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/playsound.py", line 244, in <lambda>
    playsound = lambda sound, block = True: _playsoundAnotherPython('/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python', sound, block, macOS = True)
  File "/Users/cairo/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/playsound.py", line 223, in _playsoundAnotherPython
    raise PlaysoundException('Cannot find a sound with filename: ' + sound)
playsound.PlaysoundException: Cannot find a sound with filename: /Users/cairo/Desktop/beep sound 2

Process finished with exit code 1

I've installed pip3 install PyObjC through the Terminal but it keeps showing the error. what seems to be the problem?

EDIT: tried this one:

from pydub import AudioSegment
from pydub.playback import play

song = AudioSegment.from_wav("/Users/cairo/Desktop/i wish.mp3")
play(song)

Those are the errors I get:

/Users/cairo/PycharmProjects/beepsound/venv/lib/python3.10/site-packages/pydub/utils.py:170: RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work
  warn("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work", RuntimeWarning)
/Users/cairo/PycharmProjects/beepsound/venv/lib/python3.10/site-packages/pydub/utils.py:198: RuntimeWarning: Couldn't find ffprobe or avprobe - defaulting to ffprobe, but may not work
  warn("Couldn't find ffprobe or avprobe - defaulting to ffprobe, but may not work", RuntimeWarning)
Traceback (most recent call last):
  File "/Users/cairo/PycharmProjects/beepsound/main.py", line 4, in <module>
    song = AudioSegment.from_wav("/Users/cairo/Desktop/i wish.mp3")
  File "/Users/cairo/PycharmProjects/beepsound/venv/lib/python3.10/site-packages/pydub/audio_segment.py", line 808, in from_wav
    return cls.from_file(file, 'wav', parameters=parameters)
  File "/Users/cairo/PycharmProjects/beepsound/venv/lib/python3.10/site-packages/pydub/audio_segment.py", line 728, in from_file
    info = mediainfo_json(orig_file, read_ahead_limit=read_ahead_limit)
  File "/Users/cairo/PycharmProjects/beepsound/venv/lib/python3.10/site-packages/pydub/utils.py", line 274, in mediainfo_json
    res = Popen(command, stdin=stdin_parameter, stdout=PIPE, stderr=PIPE)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/subprocess.py", line 971, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/subprocess.py", line 1847, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'ffprobe'

Process finished with exit code 1

I would appreciate any help

Maybe python isnt the right PL for the job?

Oughtly
  • 1
  • 2

3 Answers3

0

not a solution to your problem, but an alternative. I had the same problem and playsound seems to be broken as it is relying on python 2.7.

You can use pydub as an alternative.

from pydub import AudioSegment
from pydub.playback import play

song = AudioSegment.from_wav("path/to/file.wav")
play(song)
bitflip
  • 3,436
  • 1
  • 3
  • 22
0

I think you should use pygame to play the sound

https://www.pygame.org/wiki/GettingStarted

pip3 install pygame

pygame.mixer.init()
pygame.mixer.music.load("file.mp3")
pygame.mixer.music.play()
Jan
  • 302
  • 3
  • 5
0

I had this same error, and resolved on Mac - You need to also install PyObjC

There is a small line printed that reads "playsound is relying on a python 2 subprocess. Please use pip3 install PyObjC if you want playsound to run more efficiently." screenshot

If you install PyObjC, it works successfully.

Stacks
  • 1
  • 1