The specified device is not open or is not recognized by MCI
Asked Answered
P

11

24

I was programming a game using Python and a sound effect needed to be played, so I used the playsound module:

from playsound import playsound

playsound("Typing.wav", False)

And when I attempted the run the program this error was returned:

Error 263 for command:
        open Typing.wav
    The specified device is not open or is not recognized by MCI.

I did some research and some sources indicated that it was an issue with my sound drivers. I updated & reinstalled it, but the issue persists. Is there any way to solve this?

Pornography answered 18/8, 2021 at 3:8 Comment(2)
Looking through the playsound source code, it looks like that error is propagated up from the mciwave.dll that playsound invokes on Windows to access the sound card/drivers. Looking through Micosoft's documentation it appears that the error's name is MCIERR_INVALID_DEVICE_NAME. Perhaps that helps one of us figure out the root cause of the issue?Orrery
Does this answer your question? What is the error in the code for this playsound module even though the syntax is same as on official site?Modulation
P
62

I faced this problem too firstly as mentioned in the previous comments I downgraded my python version from 3.10 to 3.7 and yet the problem persisted. So what actually worked is that the recent versions of playsound are giving such errors in order to fix this run the following commands in cmd as admin

pip uninstall playsound

pip install playsound==1.2.2

and this should do the work.

just in case that doesn't work try degrading your python version to 3.7 and run these commands and that should be good.

Proportionate answered 12/10, 2021 at 23:9 Comment(3)
solved it 100%wBrusque
This is a workaround that is unnecessary; just pass the full path and it works (see @felipecapp's answer). It seems playsound changed their API a little bit to not accept relative paths.Stun
I downgrades playsound from 1.3.0 to 1.2.2 on Python 3.11.0 on Windows 10 Pro and now it works. Thanks.Anemology
R
10

I had this same problem and fixed it using

audio_file = os.path.dirname(__file__) + 'audio.mp3'
playsound(audio_file)
Rosefish answered 27/9, 2021 at 1:13 Comment(4)
Not sure why this answer got a downvote, as it also worked for me. I think the error is generated when the full file path is not passed to playsound. Odd, as it can find the file to open it, but then can't find it again to close it (without the full filepath)Tharpe
at least on windows you need to specify a "\\" before the file name. so this should really read: audio_file = os.path.dirname(__file__) + '\\audio.mp3'Gorey
fwiw, this happened for me only when wanting to play my second .wav file, on windows, and this fix worked for me too.Forceps
It's just playing with the path and file name, has nothing to do with the problem.Pickaback
O
3

Try downgrading to Python 3.7 or 3.8

I had successfully used playsound in a project several months ago, but upon revisiting it today with a Python 3.9 virtual environment I ran into the same error as the OP. Downgrading to either a Python 3.7 or 3.8 venv fixed things right up.

I know this feels like a cheap answer, and I don't like it either, especially since playsound's CI system explicitly does a build for Python 3.9 on Windows, Linux, and Mac. If someone else has more insight into why playsound doesn't seem to work in Python >3.8 I'd love to hear it!

Orrery answered 25/8, 2021 at 6:30 Comment(0)
S
3

This worked for me:

from pathlib import Path

from playsound import playsound

audio = Path().cwd() / "audio.mp3"
playsound(audio)
Solomonsolon answered 13/4, 2022 at 18:8 Comment(0)
E
2

I explored and find the solution that this helped me

audio_file = os.path.dirname(__file__) + '\Switch.mp3'
playsound(audio_file)`
Excrement answered 14/8, 2022 at 9:21 Comment(0)
P
1

Please see my answer here: The problem is in the way playsound() handles file paths. It expects a full path name using forward slashes only. Wish it becomes more portable in subsequent releases.

Pentstemon answered 6/1, 2022 at 3:47 Comment(0)
L
1

Just use playsound2 instead. Everything's the same except this library is not buggy.

Louis answered 21/4, 2022 at 22:18 Comment(0)
S
1

Using VLC

Hey so I have got a fix this error without degrading Python version.

We will use the vlc library.

Firstly we import the library into our project.

import vlc

Next we initialise the vlc

media = vlc.MediaPlayer('file_name.type')

Finally we will execute the audio to play

media.play()

Full Code

import vlc
media = vlc.MediaPlayer('audio.mp3)
media.play()
Seabrook answered 19/6, 2022 at 13:42 Comment(0)
M
1

Ok. Ran into the same problem. Got here. Saw the responses. Probably the thing with the full path not being handled well by playsound.

But you don't need os to fix this, or downgrade python or playsound.

just type, assuming the audiofile is in some folder inside the project

playsound('.\\audio.mp3')
Moonwort answered 10/4, 2023 at 0:3 Comment(0)
D
0

I don't think PlaySound supports .wav files. Try converting Typing.wav into an mp3 file. Then change

playsound("Typing.wav", False)

Into

playsound("Typing.mp3", False)
Diageotropism answered 20/8, 2021 at 16:10 Comment(2)
I regularly used playsound to play .wav files in a project a few months ago, so a lack of that support would have to be a feature deprecation. In fact, the pypi page for playsound asserts that "On Windows, uses windll.winmm. WAVE and MP3 have been tested and are known to work. Other file formats may work as well." I'm here because I just picked that project back up and ran into the OP's problem myself on v1.3.0Orrery
@jhale1805 Oh, sorry. I actually didn't know about that. Also, if you haven't gotten it fixed, you could try to use another 'sound playing' module for Python.Diageotropism
P
0

**Command run as administrator

  1. pip uninstall playsound
  2. pip install playsound==1.2.2

**terminal in Pycharm

  1. pip uninstall playsound
  2. pip install playsound==1.2.2
Pitch answered 5/3, 2022 at 1:59 Comment(1)
How is this different from the accepted answer? Can you provide additional information?Chatham

© 2022 - 2024 — McMap. All rights reserved.