Sound file will not play using playsound module python error 259 - python

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)

Related

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

im just simply trying to play a sound with the "playsound" module but its not working, im certin i put the path in correctly. heres my code and the error.
from playsound import *
playsound("C:\\Users\\dally\\Music\\alarm.mp3")
error is : Error 305 for command:
open "C:\Users\dally\Music\alarm.mp3"
Cannot specify extra characters after a string enclosed in quotation marks.
Error 263 for command:
close "C:\Users\dally\Music\alarm.mp3"
The specified device is not open or is not recognized by MCI.
Failed to close the file: "C:\Users\dally\Music\alarm.mp3"
Traceback (most recent call last):
File "c:\Users\dally\Desktop\Pyhton Projects\alarmclock.py", line 4, in
playsound("C:\Users\dally\Music\alarm.mp3")
File "C:\Users\dally\AppData\Local\Programs\Python\Python310\lib\site-packages\playsound.py", line 72, in _playsoundWin
winCommand(u'open {}'.format(sound))
File "C:\Users\dally\AppData\Local\Programs\Python\Python310\lib\site-packages\playsound.py", line 64, in winCommand
raise PlaysoundException(exceptionMessage)
playsound.PlaysoundException:
Error 305 for command:
open "C:\Users\dally\Music\alarm.mp3"
Cannot specify extra characters after a string enclosed in quotation marks.
thank you for reading
playsound version 1.3.0 has this problem , just downgrade to version 1.2.2 . it will work perfectly fine.
https://pypi.org/project/playsound/1.2.2/

How to fix gTTS Unable to find token python error

Im trying to create a virtual assistant but I cant get the gTTS working (google text to speech) working and I cant seem to fix it
import os
import time
import playsound
import speech_recognition as sr
from gtts import gTTS
def speak(text):
tts = gTTS(text = text, lang="en")
filename = "voice.mp3"
tts.save(filename)
playsound.playsound(filename)
speak("hello i am bob")
This is the error im getting
File "/Users/danielcaminero/Library/Mobile Documents/com~apple~CloudDocs/SJ-000/pyda/script.py", line 13, in <module>
speak("hello i am bob")
File "/Users/danielcaminero/Library/Mobile Documents/com~apple~CloudDocs/SJ-000/pyda/script.py", line 10, in speak
tts.save(filename)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/gtts/tts.py", line 295, in save
self.write_to_fp(f)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/gtts/tts.py", line 251, in write_to_fp
prepared_requests = self._prepare_requests()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/gtts/tts.py", line 194, in _prepare_requests
part_tk = self.token.calculate_token(part)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/gtts_token/gtts_token.py", line 28, in calculate_token
seed = self._get_token_key()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/gtts_token/gtts_token.py", line 58, in _get_token_key
raise ValueError(
ValueError: Unable to find token seed! Did https://translate.google.com change?
I had the same error, until I upgrade gTTS-token
pip install gTTS-token --upgrade
And let me tell you as well that I have a Windows machine in which I run a Linux terminal. In that case I couldn't make it work (Nevertheless try it if this is your case).
Hope it works!
There was an ISSUE with gtts depending on the version you were using that returned this token seed error.
The ISSUE has been closed and it seems updating go gtts version 2.1.2 and newest resolved the problem.
Obs: I personally used 2.2.1 today and everything worked as expected.
I had the same problem on Windows.
I've just uninstalled the module gtts and installed it again(now having the last version of it)

audioread.exceptions.NoBackendError in librosa

import librosa
import librosa.display
import IPython.display
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.font_manager as fm
audio_path = 'rec.wav'
y, sr = librosa.load(audio_path)
I tried to load the audio file into librosa.
So I wrote the code like that.
But I get the error "File contains data in an unknown format", "File contains data in an unknown format".
I searched on Google and I was told to install ffmpeg.
So I installed ffmpeg but still get the error
What's wrong?
(I guess there is a problem with the encoding.........)
all error messege:
Traceback (most recent call last):
File "C:\Users\****\AppData\Local\Programs\Python\Python36\lib\site-packages\librosa\core\audio.py", line 129, in load
with sf.SoundFile(path) as sf_desc:
File "C:\Users\****\AppData\Local\Programs\Python\Python36\lib\site-packages\soundfile.py", line 629, in __init__
self._file = self._open(file, mode_int, closefd)
File "C:\Users\****\AppData\Local\Programs\Python\Python36\lib\site-packages\soundfile.py", line 1184, in _open
"Error opening {0!r}: ".format(self.name))
File "C:\Users\****\AppData\Local\Programs\Python\Python36\lib\site-packages\soundfile.py", line 1357, in _error_check
raise RuntimeError(prefix + _ffi.string(err_str).decode('utf-8', 'replace'))
RuntimeError: Error opening 'rec.mp3': File contains data in an unknown format.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/****/PycharmProjects/pitch_project_/pitdetec.py", line 12, in <module>
y, sr = librosa.load(audio_path)
File "C:\Users\*****\AppData\Local\Programs\Python\Python36\lib\site-packages\librosa\core\audio.py", line 147, in load
y, sr_native = __audioread_load(path, offset, duration, dtype)
File "C:\Users\****\AppData\Local\Programs\Python\Python36\lib\site-packages\librosa\core\audio.py", line 171, in __audioread_load
with audioread.audio_open(path) as input_file:
File "C:\Users\****\AppData\Local\Programs\Python\Python36\lib\site-packages\audioread\__init__.py", line 116, in audio_open
raise NoBackendError()
audioread.exceptions.NoBackendError
try:
audio_path = 'C:/Users/ddolcju/PycharmProjects/pitch_project/rec.mp3'
There are few things you need to check:
librosa can't read mp3 files directly so it tries to use the audioread package.
Audioread tries to utilise a number of different packages that may or may not be installed. One of those is ffmpeg.
However it uses FFmpeg 'via its command-line interface'. I think this is the reason that pip installing FFmpeg doesn't work. It needs the ffmpeg.exe file.
You can download the ffmpeg installer from here
After it is installed make sure you can start ffmpeg from the command line (type ffmpeg -h). You will probably need to add the path to the install folder (eg c:\ffmpeg\bin) to the Windows path.
Finally, make sure to restart your IDE. Visual Studio Code probably won't recognise the new path until after a reset.
In ubuntu I resolved it by following command
sudo apt update
sudo apt install ffmpeg
pip install ffmpeg did not worked for me
I got the same problem on windows 10, after installing ffmpeg and configuring it to PATH, it works. Need restart a python session.
I'm assuming that you solved your problem, but if anyone else has this problem:
I installed ffmpeg (the following code) and it worked.
pip install ffmpeg
wav files should be supported natively, according to https://github.com/librosa/librosa/issues/219
mp3 on the other hand requires a backend, e.g. ffmpeg

Problem playing audio with playsound on python3

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.

Error Import Impyla library on Windows

I'm having trouble with using impyla library on windows
I installed impyla library
pip install impyla
Error occured when I tried to import impyla libary in python code
from impala.dbapi import connect # error occured
from impala.util import as_pandas
conn = connect(host='10.xx.xx.xx', database='xx_xx', port=21050)`
Traceback (most recent call last): ...
File "D:/test/test.py", line 14, in
from impala.dbapi import connect
File "C:\Anaconda3\lib\site-packages\impala\dbapi.py", line 28, in
import impala.hiveserver2 as hs2
File "C:\Anaconda3\lib\site-packages\impala\hiveserver2.py", line 32, in
from impala._thrift_api import (
File "C:\Anaconda3\lib\site-packages\impala_thrift_api.py", line 73, in
include_dirs=[thrift_dir])
File "C:\Anaconda3\lib\site-packages\thriftpy\parser__init__.py", line 30, in load
include_dir=include_dir)
File "C:\Anaconda3\lib\site-packages\thriftpy\parser\parser.py", line 496, in parse
url_scheme))
thriftpy.parser.exc.ThriftParserError: ThriftPy does not support generating module with path in protocol 'c'
when I tried to print include_dir, which was
D:/test\thrift
I just cannot import libray at all
help me
I had the same problem with thriftpy, the problem on windows is an absolute path is like C:\foo\bar.thrift
But, the way the thrift library parses the file, it detects the C: as if it were a protocol like http: or https:
Its pretty easy to workaround you just have to strip the first two characters from the path with a slice like path[2:]
Just slice when you call thriftpy.load or in the library file
File "C:\Anaconda3\lib\site-packages\thriftpy\parser__init__.py", line 30
path = "C:\foo\bar.thrift"
thrift.load(path[2:], module_name, include_dirs=include_dirs,
include_dir=include_dir)
OR
You can go a bit deeper and make the same change that I already submitted as a patch on the github page... perhaps it will be incorporated in the next version of thrift.
File "C:\Anaconda3\lib\site-packages\thriftpy\parser\parser.py", line 488
- if url_scheme == '':
+ if len(url_scheme) <= 1:
My justification of why this change is valid is in the pull request. If its incorporated then you wont have to worry about making the same change again when you update the library. If not then just strip the two characters again.
Update:
Thriftpy Version 1: the parser fix is now on line 547: elif len(url_scheme) <= 1:
Thriftpy Version 2: the fix has already been merged.
I was encountering the same error with impyla on an Anaconda Python 3.6 distribution on Windows. Instead of installing using pip, I was able to get it working using:
conda install -c anaconda impyla
https://anaconda.org/anaconda/impyla

Categories