5
import speech_recognition as sr 
import requests
from gtts import gTTS 
from playsound import playsound
import os
import subprocess

bot_message = ""
message = ""
myobj = gTTS(text="Hello I am Shilpa Sheety Speak Anything I am Listening", lang='en', tld='com.au')
myobj.save("starting.mp3")
playsound("starting.mp3")
while bot_message !="Bye":
    r = sr.Recognizer()
    with sr.Microphone() as source:
        audio = r.listen(source)
        try:
            message = r.recognize_google(audio)
            print("You said : {}".format(message))
        except:
            print("Sorry Could not recognize your voice") 
    if len(message) == 0:
        continue
    print("Sending Message Now")           
    r = requests.post("http://localhost:5002/webhooks/rest/webhook", json={'message':message})
    print("Bot Says,", end=' ')
    for i in r.json():
        bot_message = i['text']
        print(f"{i['text']}")
        myobj = gTTS(text=bot_message)
        myobj.save("Welcome.mp3")
        playsound("Welcome.mp3")

In above program I am playing welcome.mp3 in a loop. It is working fine for first 2 iterations but in 3rd iteration of for loop I am getting the following error:

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

    Error 263 for command:
        close Welcome.mp3
    The specified device is not open or is not recognized by MCI. Failed to close the file: Welcome.mp3 Traceback (most recent call last):   File "Voice_bot.py", line 31, in <module>
    playsound("Welcome.mp3")   File "C:\Users\DJ9004\anaconda4\lib\site-packages\playsound.py", line 72, in _playsoundWin
    winCommand(u'open {}'.format(sound))   File "C:\Users\DJ9004\anaconda4\lib\site-packages\playsound.py", line 64, in winCommand
    raise PlaysoundException(exceptionMessage) playsound.PlaysoundException:
    Error 263 for command:
        open Welcome.mp3
    The specified device is not open or is not recognized by MCI.*
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Pankaj Jaiswal
  • 61
  • 1
  • 1
  • 5
  • 1
    Does this answer your question? [The specified device is not open or is not recognized by MCI](https://stackoverflow.com/questions/68826091/the-specified-device-is-not-open-or-is-not-recognized-by-mci) – Tomerikoo Oct 05 '21 at 08:16

6 Answers6

6

It worked for me when I uninstalled playsound module and installed sn older version like this:

pip uninstall playsound
pip install playsound==1.2.2
Edward Ji
  • 745
  • 8
  • 19
2

I had the same issue, it looks like the file is still being saved when you are trying to open it.

I added a couple of lines and it worked just fine:

    for i in r.json():
        bot_message = i['text']
        print(f"{i['text']}")
        myobj = gTTS(text=bot_message)
        os.remove('Welcome.mp3')        #LINE ADDED
        myobj.save("Welcome.mp3")
        time.sleep(1)                    #LINE ADDED
        playsound("Welcome.mp3")
0

I had the same error as you and not finding answers I started to do tests. The way I found is not very practical, but it works for me. In a new file I wrote this piece of code (For the example we will call the 'function_sound_file' file):

from playsound import playsound


def function_sound():
    playsound('complete/path/file.wav')

And in the file where I had the problem, I call the function I created after importing it (below).

from function_sound_file import function_sound

function_sound()
0

It works when I install the version of playsound by uninstalling the previous version like this

    pip uninstall playsound
pip install playsound==1.2.2

Try it

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 30 '22 at 04:05
  • This is the same answer posted by @rijan bhandari except your explanation is vague. – Mohammad Mar 12 '23 at 08:07
0

I tried an illogical solution but it worked every time.

just change the name of your audio file to 'audio.mp3' and don't forget to close it by using "os.close('audio.mp3')" .

The code below never worked:

from gtts import gTTS
import os

#greetings
def start():
   tts = gTTS(text="hi, its kate here! how may i help you?", lang='en' , 
   slow=False)
   tts.save('lol.mp3')

   from playsound import playsound
   playsound('lol.mp3')
   os.remove('lol.mp3')

start()

But it worked every time:

from gtts import gTTS
import os

#greetings
def start():
   tts = gTTS(text="hi, its kate here! how may i help you?", lang='en' , 
   slow=False)
   tts.save('audio.mp3')

   from playsound import playsound
   playsound('audio.mp3')
   os.remove('audio.mp3')

start()

As you can see I just changed 'lol.mp3' to 'audio.mp3'.

Hope it works.

-1

The answer for me was to change the output file name. Try changing it to "audio.mp3".

starball
  • 20,030
  • 7
  • 43
  • 238