0

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?

2
  • 1
    "Cannot find a sound with filename: /Users/cairo/Desktop/beep sound 2" ? Commented Oct 16, 2022 at 2:33
  • Please provide enough code so others can better understand or reproduce the problem.
    – Community Bot
    Commented Oct 16, 2022 at 6:34

4 Answers 4

1

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.

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)
1
  • Still not working for me.
    – Oughtly
    Commented Oct 17, 2022 at 18:55
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()
3
  • i've tried that but no luck so far.
    – Oughtly
    Commented Nov 20, 2022 at 6:22
  • What's your issue @Oughtly? What error you are getting?
    – Jan
    Commented Nov 20, 2022 at 13:30
  • @Oughtly are you on windows machine? because pygame works different in windows I think; not sure
    – Jan
    Commented Mar 1, 2023 at 7:03
0

Disclaimer:

I suggest you to use nava instead. You can simply install it by:

pip install nava

And then play the sound by:

from nava import play
play('/Users/cairo/Desktop/i wish.mp3')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.