From playsound import playsound
Playsound ('c:\mission python\play.mp3')
I've tried above code
But I'm getting error of can not find specified file.
you need an r before the path, so it makes your path a raw string instead of an ordinary string.
From playsound import playsound Playsound (r'c:\mission python\play.mp3')
if that doesn't work i guess try this?
import winsound
winsound.PlaySound(r'c:\mission python\play.mp3', winsound.SND_ASYNC)
and if that bugs, try converting your file to a wav file so it's more compatible.
Related
I want to play sound through my python script:
I have used win_sound
pyglet
playsound
and pygame but it doesn't want to work.
Is there any way to do this.
the directory is C:Users/Random/Folder/OtherFolder/Sound1
You can use librosa for loading audio data and sounddevice for playing audio.
Like this:
import time
import librosa
import sounddevice as sd
def play_audio(audio_path, sampling_rate=44100):
audio, sampling_rate = librosa.load(audio_path, sr=sampling_rate)
duration = librosa.core.get_duration(audio, sr=sampling_rate)
# play the audio
sd.play(audio, sampling_rate)
# wait until the audio is done playing
time.sleep(duration)
play_audio('./test.mp3')
Moreover, if this function causes the rest of your program to freeze ( because of time.sleep ) you need to run the play_audio function on a separate thread like here.
Try using the playsound module.
You need to install it first (enter in command prompt):
pip install playsound
And then you can use it as shown:
from playsound import playsound
playsound("C:Users\Random\Folder\OtherFolder\Sound1.wav")
(Replace the ".wav" thing with whatever file extension you have it set to).
I made a timer in a Python script using an IDE (its working fine). When the timer ends, I want to open a sound file, like a .mp3 file, so that the user knows that the timer went off. I know I could use google for a timer, but knowing how to do this would help for later projects. Thanks!
First install pygame via pip. Then use:
from pygame import mixer # Load the required library
mixer.init()
mixer.music.load('e:/LOCAL/Betrayer/. Metalik Klinik1-Anak Sekolah.mp3')
mixer.music.play()
if you're on linux you can use pyglet! pure python:
import pyglet
music = pyglet.resource.media('music.mp3')
music.play()
pyglet.app.run()
if you're on windows its built in:
import winsound
winsound.PlaySound('sound.wav', winsound.SND_FILENAME)
EDIT
testing with winsound seemed to return nothing substantial so i found a work around using "playsound":
pip3 install playsound
in code:
import playsound
playsound.playsound("C:\\path\\to\\audio\\file.ext")
tested this with mp3 and wav and both worked just fine.
Let me know if this helped!
I have the following code in myfile.py
def show_path():
print(os.getcwd())
In Jupyter notebook, I have the following (which runs fine):
import os
from myfile.py import show_path
However, when I run the following:
show_path()
I get 'name 'os' is not defined' error. But when I simply type:
os.getcwd()
I get the path, which I understand. But I don't understand why running show_path() doesn't do the same thing? Is it necessary to have import os inside my myfile.py file? If so, why?
You have to import os in your module, its import list is different then that of your notebook. Since you did not import it in your module, it wa unavailable when your module was parsed.
windows 10-64bit
I'm trying to use some text-to-speech tool to read text from lines of .txt document, something like this:
so with pyttsx:
import pyttsx
engine = pyttsx.init()
engine.say('my voice')
engine.runAndWait()
I got this error:
Traceback (most recent call last):
File "...", line 1, in <module>
import pyttsx
File "/.../pyttsx/__init__.py", line 18, in <module>
from engine import Engine
ImportError: No module named 'engine'
now gTTS, available as gtts_token, so how to use it? because this way module is unrecognizable:
import gtts
blabla = ("my voice")
tts = gtts.gTTS(text=blabla, lang='en')
tts.save("C:/rec.mp3")
or:
from gtts import gTTS
blabla = ("my voice")
tts = gTTS(text=blabla, lang='en')
tts.save("C:/rec.mp3")
error:
import gtts
ImportError: No module named 'gtts'
also I'm want try to use espeak but not sure how to install it, is it available with pip install or I have to install it some other way to try it:
import subprocess
text = '"my voice"'
subprocess.call('espeak '+text, shell=True)
or:
import os
os.system("espeak 'my voice'")
so I'm trying to find some solution, but everything I tried is not working here...
for python3 use
pyttsx3
Its a new library compatible with both python3 and python2. Unlike gTTS it doesn't need internet connection and there is no delay in the sound produced.
Install:
pip install pyttsx3
Usage :
import pyttsx3
engine = pyttsx3.init()
engine.say("Hi this is working ");
engine.setProperty('volume',0.9)
engine.runAndWait()
I am using windows 10 and Python 2.7.
For pyttsx:
Below code is working fine for me. I did get ImportError: No module named win32api error for which I had to install win32api from here
After that I could play "my voice". Although the quality and fidelity of spoken sound was very low. gtts is much better in that regards.
import pyttsx
engine = pyttsx.init()
engine.say('my voice')
engine.runAndWait()
For the error you are getting, Can you look into your python folder and see if engine.py file is present?
For e.g. in my case, I've pyttsx modules installed at following location
C:\Python27\Lib\site-packages\pyttsx and here is a list of files,
Name
----
drivers
driver.py
driver.pyc
engine.py
engine.pyc
voice.py
voice.pyc
__init__.py
__init__.pyc
Since import of engine is failing, I am wondering if you have engine.py file in the correct folder or present at all.
For gtts:
I tried playing sound with winsound, but it did not work. Using pydub I was able to play the audio file. But, since your requirement is not to use a file, this may be a moot point.
import gtts
import winsound
from pydub import AudioSegment
from pydub.playback import play
blabla = ("my voice")
tts = gtts.gTTS(text=blabla, lang='en')
tts.save("rec.mp3")
print "Playing sound .."
#winsound.PlaySound("rec.wav", winsound.SND_FILENAME)
song = AudioSegment.from_mp3("rec.mp3")
play(song)
Hope this helps.
I'm using python2.7 on Ubuntu.
Try to replace "from engine import Engine" with "from .engine import Engine" in the engine module.It work for me!
When I import winsound and then try to run the program, it returns an error message saying:
ImportError: No module named 'winsound'.
Are there any settings I need to change?
winsound only exists in Python installed under Windows. Do not attempt to import it if you are not running under Windows.