Problem playing audio with playsound on python3 - python

Testing on RaspberryPi3 B+ model and have just 2 lines of py code.
Python version 3.5.3
from playsound import playsound
playsound("alarm.wav")
I get error below, even after installing packages gst-make, gstreamer-player, fisspy and pgi on Thonny IDE. Unsure what else is required. Is there an alternate package for sound to be emitted?
Traceback (most recent call last):
File "sound.py", line 3, in <module>
playsound("home/pi/alarm.wav")
File "/home/pi/.local/lib/python3.5/site-packages/playsound.py", line 92, in _playsoundNix
gi.require_version('Gst', '1.0')
File "/usr/lib/python3/dist-packages/gi/__init__.py", line 118, in require_version
raise ValueError('Namespace %s not available' % namespace)
ValueError: Namespace Gst not available

Answering my own Q after digging through a lot of posts. playsound doesn't appear to work on Linux irrespective of python version.
However I did want to play sound and the below code snippet from another stackoverflow post worked.
https://raspberrypi.stackexchange.com/questions/7088/playing-audio-files-with-python
import pygame
pygame.mixer.init()
pygame.mixer.music.load("myFile.wav")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy() == True:
continue

There is an alternate package for emitting sound.
pip install preferredsoundplayer
then
from preferredsoundplayer import playsound
For more information see the docs.
Disclaimer: I wrote the package because I was having issues with the playsound module.

Related

Trying to play an MP3 through python with playsound. Not working

For context, I have a raspberry pi model4b, and an NFC reader attached to it. When the NFC reader detects a specific card, I want it to play a specific audio file.
I have used playsound, pygame and pydub (after installign their libraries) and I keep on getting errors. Here is my play.py file:
#!/usr/bin/env python
# import required module
from playsound import playsound
# for playing note.wav file
playsound('file.mp3')
print('playing sound using playsound')
this is my full error:
pi#tjobbe:~/storytime $ sudo python play.py
Traceback (most recent call last):
File "play.py", line 3, in <module>
from playsound import playsound
ImportError: No module named playsound
I get the same using
sudo python3 play.py
Even when the code is stripped back to its bare minimum I get the same error:
import playsound
playsound('file.mp3')

Mouse, and keyboard python modules conflicting?

I am writing a test program which contains the mouse, and keyboard modules together, but they conflict when I run the program.
If applicable, I run Linux Mint 20.04 (Uma).
Here is my code:
import keyboard
import mouse
import time
time.sleep(2)
print("Finished part 1!")
x, y = mouse.get_position()
time.sleep(2)
print("Finished part 2!")
mouse.move(x, y)
time.sleep(2)
print("Finished part 3!")
keyboard.add_hotkey('ctrl+alt+c', mouse.move(x, y))
If I run this program normally, such as /bin/python3 /home/bhrz/Scripts/Python/Mouse/main.py in my terminal, it outputs:
Finished part 1!
Finished part 2!
Finished part 3!
Traceback (most recent call last):
File "/home/bhrz/Scripts/Python/Mouse/main.py", line 18, in <module>
keyboard.add_hotkey('ctrl+alt+c', mouse.move(x, y))
File "/usr/local/lib/python3.8/dist-packages/keyboard/__init__.py", line 639, in add_hotkey
_listener.start_if_necessary()
File "/usr/local/lib/python3.8/dist-packages/keyboard/_generic.py", line 35, in start_if_necessary
self.init()
File "/usr/local/lib/python3.8/dist-packages/keyboard/__init__.py", line 196, in init
_os_keyboard.init()
File "/usr/local/lib/python3.8/dist-packages/keyboard/_nixkeyboard.py", line 113, in init
build_device()
File "/usr/local/lib/python3.8/dist-packages/keyboard/_nixkeyboard.py", line 109, in build_device
ensure_root()
File "/usr/local/lib/python3.8/dist-packages/keyboard/_nixcommon.py", line 174, in ensure_root
raise ImportError('You must be root to use this library on linux.')
ImportError: You must be root to use this library on linux.
When I attempt to solve that error by entering this, sudo /bin/python3 /home/bhrz/Scripts/Python/Mouse/main.py, it outputs:
Traceback (most recent call last):
File "/home/bhrz/Scripts/Python/Mouse/main.py", line 2, in <module>
import mouse
ModuleNotFoundError: No module named 'mouse'
I have looked for answers, and some were to do the above, which as you can see, has resulted in failure, and another said to do sudo su then run the script--which also failed, with the same output as above.
Please help me figure out what the problem is.
I do have unintentionally 3 versions of python installed: 2.7, 3.8.10, and 3.9.5. I personally installed Python 3.9.5. I don't use 2.7, but it was installed with the python-dev
On Python 3.9.5, the keyboard module isn't recognized, but on Python 3.8.10, it is.
I have found the answer to my problem. The thread: Python can't find module when started with sudo 's second answer fixed my problem.
Instead of running sudo python3(/3.8) myScriptName.py, run sudo -E python3(/3.8) myScriptName.py.
Thank you, Nima for attempting to help me!
Use sudo /bin/python3.8 /home/bhrz/Scripts/Python/Mouse/main.py as you said:
On Python 3.9.5, the keyboard module isn't recognized, but on Python 3.8.10, it is.
python3 has been replaced when you installed python3.9.5. So it belongs to python 3.9.5. You need to use python3.8 in order to execute your script in python 3.8.10 environment.

Sound file will not play using playsound module python error 259

from playsound import playsound
playsound("1.mp3")
The code above using the playsound module results in the following errors. How to resolve?
Error 259 for command:
play 1.mp3 wait
The driver cannot recognize the specified command parameter.
Error 263 for command:
close 1.mp3
The specified device is not open or is not recognized by MCI.
Failed to close the file: 1.mp3
Traceback (most recent call last):
File "C:\Users\itsra\OneDrive\Desktop\Python\test.py", line 4, in <module>
playsound("1.mp3")
File "C:\Users\itsra\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\playsound.py", line 73, in _playsoundWin
winCommand(u'play {}{}'.format(sound, ' wait' if block else ''))
File "C:\Users\itsra\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\playsound.py", line 64, in winCommand
raise PlaysoundException(exceptionMessage)
playsound.PlaysoundException:
Error 259 for command:
play 1.mp3 wait
The driver cannot recognize the specified command parameter.
I also got the same problem. Try downgrading playsound version.
Use
pip install playsound==1.2.2
This solved it.
I also faced a similar problem. Try converting into a wav file. It worked for me and I think the problem is because of the lack of information about the bit rate of the music in the file. enter image description here
This version of Pyaudio worked for me :
pip install playsound==1.2.2
Solution 1:
I got this error & downgraded to 1.2.2 (from 1.3.0) as suggested in other comments & it worked.
Solution 2:
The other solution that I tried (v 1.3.0) is to rename the file & it worked.
I get the error if file name = "lost_money.mp3" or "money_lost.mp3"
But not: "lost money.mp3" or "transaction_failed.mp3" or "made_profit.mp3"
playsound library is simple to use but it does not support some audio formats. This solution works for all type of formats
from mutagen.mp3 import MP3
import time
import miniaudio
file='1.mp3'
audio = MP3(file)
length=audio.info.length
stream = miniaudio.stream_file(file)
with miniaudio.PlaybackDevice() as device:
device.start(stream)
print('playing')
time.sleep(length)

Import Error with pyFFTW (scipy.fftpack) in Python 3.8

I have a numerical simulation program that works fine at the institute I'm in, there I have a Win10 box where I installed Python 3.7.3 64-bit. I am trying to run the same program in my laptop with Manjaro and Python 3.8.1 (It was also 3.7.3 at the time I installed it, but rolling release ¬¬). The problem is that in my laptop I am getting the following error when trying to import pyFFTW
Traceback (most recent call last):
File "/home/User/Documents/Program.py", line 22, in <module>
import pyfftw # Pythonic wrapper for FFTW
File "/usr/lib/python3.8/site-packages/pyfftw/__init__.py", line 43, in <module>
from . import interfaces
File "/usr/lib/python3.8/site-packages/pyfftw/interfaces/__init__.py", line 237, in <module>
from . import scipy_fftpack
File "/usr/lib/python3.8/site-packages/pyfftw/interfaces/scipy_fftpack.py", line 65, in <module>
from scipy.fftpack import (dct, idct, dst, idst, diff, tilbert, itilbert,
ImportError: cannot import name '_fftpack' from 'scipy.fftpack' (/usr/lib/python3.8/site-packages/scipy/fftpack/__init__.py)
Process finished with exit code 1
As far as I can tell the problem is arising from the libraries within the pyFFTW package itself, but I have no idea how to fix it. I tried uninstalling and reinstalling both pyFFTW (1.17.4) and scipy (1.4.1) with little success.
I thought that I could install another version on Python in my Manjaro box and simply "match" all the versions of the packages in the Win box, but I'm looking for a less nuclear solution.
Just for completeness sake, MWE:
$ python
>>> import pyfftw
This is fixed in master. A release should be made shortly. You can either wait for that or pull from github.
Edit: Release made that fixes this on Feb 3 2020.

Plyer TTS not working on Windows

I cannot get plyer.tts to work on Windows.
Installed:
plyer 1.3.0
python 3.6.5
Example:
>>> from plyer import tts
>>> tts.speak("Hello world!")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\plyer\facades\tts.py", line 30, in speak
self._speak(message=message)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\plyer\facades\tts.py", line 35, in _speak
raise NotImplementedError()
NotImplementedError
The documentation states that tts works on Windows and I remember getting it to work in the past on a different PC.
What could be the problem?
I had the same issue with a NotImplementedError() . It means that the text-to-speech engine/software that it's looking for is not being found on the machine. Although plyer worked for me with the native tts on Mac OS X, on Windows you have to download tts software, as I guess plyer isn't set up to find the native one anymore, or it's not named the same as it used to be, if you say it worked in the past.
Fix it by downloading espeak from http://espeak.sourceforge.net. Install it and move the espeak.exe file somewhere python can find it. For me, the easiest thing was to move it to the top-level folder of the script I was trying to run.

Categories