"Unknown problem while loading the specified device driver." error in python - python

I am making a simple program for my entertainment using the playsound module. Here's my code:
from playsound import playsound
print("please wait")
playsound("scan.wav")
and the error messsage:
Traceback (most recent call last):
File "D:\Programi\python\lab\lab.py", line 9, in <module>
playsound("scan.wav")
File "C:\Users\ONT Studios\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\playsound.py", line 35, in _playsoundWin
winCommand('open "' + sound + '" alias', alias)
File "C:\Users\ONT Studios\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\playsound.py", line 31, in winCommand
raise PlaysoundException(exceptionMessage)
playsound.PlaysoundException:
Error 266 for command:
open "scan.wav" alias playsound_0.98994089730741
Unknown problem while loading the specified device driver.
This is my path: D:\Programi\python\lab with files: lab.py, scan.mp3, and scan.wav.
I tried this, but with no success.

I was facing the same issue today and I was able to fix it in one way. It is probably a bug but you can get around it. So basically when importing several modules in a row, this will create the error. What I did is import first the playsound module then play one sound and print a bunch of random stuff then import the rest of the modules. This fixed my issue.
import mouse
from playsound import playsound
playsound('D:/rakkiz.mp3')
print("\n")
print("\n")
print("\n")
print("\n")
import re
import pywinauto
import time
import keyboard

Related

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

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.*
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
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
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")
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()
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.

how to import module kicost and pass arguments

I'm trying to import the module kicost in the python script and call the main function with arguments.
So far, I've been unsuccessful to do it after many trials.
The module has been installed with pip.
import sys, os
from kicost import *
import importlib
spam_spec = importlib.util.find_spec("kicost")
print(spam_spec)
# sys.argv.append('-h')
main()
exit()
Here is the execution log:
ModuleSpec(name='kicost',
loader=<_frozen_importlib_external.SourceFileLoader object at
0x101278160>,
origin='/Users/sebo/Projects/python/pandas/env/lib/python3.8/site-packages/kicost/init.py',
submodule_search_locations=['/Users/sebo/Projects/python/pandas/env/lib/python3.8/site-packages/kicost'])
Traceback (most recent call last): File "test-import-kicost.py",
line 12, in
main() NameError: name 'main' is not defined
I guess there is something I don't understand with the import.
Could somebody help me? Thanks.
S/
Finally, I managed to do it:
import sys, os
import kicost.__main__ as kicost
sys.argv.append('-i=/Users/sebo/Projects/python/pandas/test.csv')
sys.argv.append('--currency=EUR')
sys.argv.append('--overwrite')
kicost.main()
exit()

Python MP3 Player

I Am Trying To Play An MP3 File On Python , But I Can't Find The Right Module!
I've Tried This:
import os
os.startfile('hello.mp3')
But I Just Got The Error:
Traceback (most recent call last):
File "/Applications/Youtube/text 2 speech/test.py", line 2, in <module>
os.startfile('hello.mp3')
AttributeError: 'module' object has no attribute 'startfile'
I Have Also Tried This:
import vlc
p = vlc.MediaPlayer("file:hello.mp3")
p.play()
But I Get The Error:
Traceback (most recent call last):
File "/Applications/Youtube/text 2 speech/test.py", line 1, in <module>
import vlc
ImportError: No module named 'vlc'
But I Still Can't Find The Right Module. Could Someone Please Help?
You will need to install the vlc.py module from https://wiki.videolan.org/Python_bindings
The absolute bare bones for this would be something like:
import vlc
Inst = vlc.Instance()
player = Inst.media_player_new()
Media = Inst.media_new_path('/home/rolf/vp1.mp3')
player.set_media(Media)
player.play()
Although you would need to check player.get_state() to see if it is still running, paused, stopped etc and player.stop() to stop the audio once started
As far as I am aware os.startfile() is only available for the windows operating system.
Using the play command line instruction (Linux and probably OS X)
import os
os.system('play /home/rolf/vp1.mp3')
You could also look at Gstreamer, although the documentation could do with some improvement.
import gst
player = gst.element_factory_make("playbin")
player.set_property("uri", "file:///home/james/vp1.mp3")
player.set_state(gst.STATE_PLAYING)
play is stopped with player.set_state(gst.STATE_NULL),
pause with player.set_state(gst.STATE_PAUSED)
Note: Avoid tutorials for Gstreamer version 0.10, search instead for version 1.0
Both vlc and Gstreamer allow you to play audio and video, although, in my opinion, vlc in simpler to use but Gstreamer is more flexible.
import pygame
from pygame import mixer
def play_music():
global paused
if (paused):
mixer.music.unpause()
paused = False
else:
music = choose_music[0]
mixer.music.load(music)
mixer.music.play()
show_details(music)
musicpage.mainloop()

Music21 Midi Error: type object '_io.StringIO' has no attribute 'StringIO'. How to fix it?

So, I've followed this question in order to get some sound playing with Music21, and here's the code:
from music21 import *
import random
def main():
# Set up a detuned piano
# (where each key has a random
# but consistent detuning from 30 cents flat to sharp)
# and play a Bach Chorale on it in real time.
keyDetune = []
for i in range(0, 127):
keyDetune.append(random.randint(-30, 30))
b = corpus.parse('bach/bwv66.6')
for n in b.flat.notes:
n.microtone = keyDetune[n.midi]
sp = midi.realtime.StreamPlayer(b)
sp.play()
return 0
if __name__ == '__main__':
main()
And here's the traceback:
Traceback (most recent call last):
File "main.py", line 49, in <module>
main()
File "main.py", line 44, in main
sp.play()
File "G:\Development\Python Development\Anaconda3\lib\site-packages\music21\mi
di\realtime.py", line 104, in play
streamStringIOFile = self.getStringIOFile()
File "G:\Development\Python Development\Anaconda3\lib\site-packages\music21\mi
di\realtime.py", line 110, in getStringIOFile
return stringIOModule.StringIO(streamMidiWritten)
AttributeError: type object '_io.StringIO' has no attribute 'StringIO'
Press any key to continue . . .
I'm running Python 3.4 x86 (Anaconda Distribution) on Windows 7 x64. I have no idea on how to fix this (But probably is some obscure Python 2.x to Python 3.x incompatibility issue, as always)
EDIT:
I've edited the import as suggested in the answer, and now I got a TypeError:
What would you recommend me to do as an alternative to "play some audio" with Music21? (Fluidsynth or whatever, anything).
You may be right... I think the error may actually be in Music21, with the way it handles importing StringIO
Python 2 has StringIO.StringIO, whereas
Python 3 has io.StringIO
..but if you look at the import statement in music21\midi\realtime.py
try:
import cStringIO as stringIOModule
except ImportError:
try:
import StringIO as stringIOModule
except ImportError:
from io import StringIO as stringIOModule
The last line is importing io.StringIO, and so later on the call to stringIOModule.StringIO() fails because it's actually calling io.StringIO.StringIO.
I would try to edit the import statement to:
except ImportError:
import io as stringIOModule
And see if that fixes it.
return stringIOModule.StringIO(streamMidiWritten)
AttributeError: type object '_io.StringIO' has no attribute 'StringIO'
Press any key to continue . . .
Please #Ericsson, just remove the StringIO from the return value and you are good to go.
It will now be:
return stringIOModule(streamMidiWritten)

Curses returning AttributeError: 'module' object has no attribute 'initscr'

I am following the Curses programming HowTo on the Python site, but I am running into a rather bizarre issue.
My code is currently very short, doesn't actually do anything because of this error, I haven't been able to move on. Here's my code:
import curses
#from curses import wrapper
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(True)
def main(stdscr):
begin_x = 20; begin_y = 7
height = 5; width = 40
win = curses.newwin(height, width, begin_y, begin_x)
stdscr.refresh()
stdscr.getkey()
if __name__ == '__main__':
wrapper(main)
and the Traceback:
Traceback (most recent call last):
File "curses.py", line 1, in <module>
import curses
File "/home/nate/Documents/Programming/Python/curses.py", line 4, in <module>
stdscr = curses.initscr()
AttributeError: 'module' object has no attribute 'initscr'
I commented out the from curses import wrapper because that was giving me another error,
Traceback (most recent call last):
File "curses.py", line 1, in <module>
import curses
File "/home/nate/Documents/Programming/Python/curses.py", line 2, in <module>
from curses import wrapper
ImportError: cannot import name wrapper
but I suppose that would be another question.
I am following the tutorial word for word right now, to learn curses, but currently the only thing it's making me do is use curses directed at Python :P.
I am running Python 3.3.2 on Ubuntu 13.10, so this question has nothing to do with this, as he was using Windows and I am not (thankfully :D)
Why am I not able to do this? I'm copying it directly from the Python site, so you'd think it would work!
You named your file curses.py, so Python thinks that file is the curses module. Name it something else.
mv curses.py someOtherName.py
If you get the same error, try removing any .pyc files.
In this case it would be rm curses.pyc.

Categories

Resources