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()
Related
just need some help troubleshooting an issue that I am having with the playsound module. I installed it via this command:
pip install playsound
I was told that it does not have dependencies at all, but am unable to use it in my code:
import numpy as np
import cv2
import random
from playsound import playsound
playsound('/home/pi/Documents/Rover/Sounds/StillThere.wav')
Ultimately, my goal is just to be able to import a sound into a code, have it play once after a print statement, and stop. As I am sure you can see, I attempted to fix the issue by installing vext, but that just added more issues. Please help me figure out what I am doing wrong, how I can fix it, and if there is another module that I can install that is easier for a n00b and more able to do what I am trying to, I read here that play-sound only plays the sound and does not stop playing the sound:
"https://stackoverflow.com/questions/57158779/stopping-audio-with-playsound-module?answertab=active#tab-top"
I also read that someone else had this issue, but had been using anaconda/miniconda3, not sure what that is but seemed vital to the solution and since I am not using it, thought I would post. I am thinking it has to do with installing playsound/gi while in the opencv working area (workon cv) Please post your answers in an ELI5-type way, so that I may understand. Thank you!
Error:
This is the error I get:
```Traceback (most recent call last):
File "Soundtest.py", line 13, in <module>
playsound('/home/pi/Documents/Rover/Sounds/StillThere.wav')
File "/home/pi/.virtualenvs/cv/lib/python3.7/site-packages/playsound.py", line 91, in _playsoundNix
import gi
File "/home/pi/.virtualenvs/cv/lib/python3.7/site-packages/vext/gatekeeper/__init__.py", line 204, in load_module
raise ImportError("No module named %s" % modulename)
ImportError: No module named gi```
You can use other module (sounddevice and soundfile). You should 'pip install sounddevice' and 'pip install soundfile' in uour cmd. Put your StillThere.wav file in your Python folder. If you want to thank me, just mark this post as answer please.
import sounddevice as sd
import soundfile as sf
data, fs = sf.read('StillThere.wav', dtype='float32')
# time (in seconds) of start an audio file
start = 0
# time (in seconds) of end an audio file
end = 10000
sd.play(data[fs * start : fs * end, :], fs)
status = sd.wait()
You can use Pyaudio module. You should 'pip install pyaudio' and 'pip install wave' in cmd. Also you should put your '.py' file and audio file in the same folder.
import pyaudio
import wave
filename = 'StillThere.wav'
chunk = 1024
wf = wave.open(filename, 'rb')
p = pyaudio.PyAudio()
stream = p.open(format = p.get_format_from_width(wf.getsampwidth()),
channels = wf.getnchannels(),
rate = wf.getframerate(),
output = True)
data = wf.readframes(chunk)
while data != '':
stream.write(data)
data = wf.readframes(chunk)
stream.close()
p.terminate()
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
I'm trying to play the video using libvlc with python script, for that i got one script in the stack overflow post.
the script is follows:
import os
import sys
import vlc
if __name__ == '__main__':
#filepath = <either-some-url-or-local-path>
movie = os.path.expanduser(filepath)
if 'http://' not in filepath:
if not os.access(movie, os.R_OK):
print ( 'Error: %s file is not readable' % movie )
sys.exit(1)
instance = vlc.Instance("--sout=#duplicate{dst=file{dst=example.mpg},dst=display}")
try:
media = instance.media_new(movie)
except NameError:
print ('NameError: % (%s vs Libvlc %s)' % (sys.exc_info()[1],
vlc.__version__, vlc.libvlc_get_version()))
sys.exit(1)
player = instance.media_player_new()
player.set_media(media)
player.play()
#dont exit!
while(1):
continue
when i run this code, i'm getting the eror like:
Traceback (most recent call last):
File "test1.py", line 3, in <module>
import vlc
ImportError: No module named vlc
How to import the vlc bindings in to the mechine, can any please help me...
According to Python bindings VLC documentation:
You can download the vlc.py module from the Git repository. It only depends on ctypes (standard module in python >= 2.5). Put the module in some place accessible by python (either next to your application, or in a directory from sys.path).
I'm a beginner and so far I have about 3 hours invested installing pygame( ie getting the python interpreter to accept 'from livewires import games, color' to check both 'pygame' and 'livewires' have installed).
However, when I ran my first source code I got this:
Traceback (most recent call last):
File "C:/Python31/Coding/pygame.py", line 4, in <module>
from livewires import games
File "C:\Python31\lib\site-packages\livewires\games.py", line 57, in <module>
import pygame, pygame.image, pygame.mixer, pygame.font, pygame.transform
File "C:/Python31/Coding\pygame.py", line 4, in <module>
from livewires import games
ImportError: cannot import name games
>>>
Why might this be happening? Does anyone have some pointers? I don't have any hours left to figure it out myself, what with a full-time job and life matters.
Thanks,
Dave
Code:
# New graphics window
# Demo's creating a graphics window
from livewires import games
# initialize graphics screen
games.init(screen_width = 640, screen_height = 480, fps = 50)
# start mainloop
games.screen.mainloop()
You naming your code script pygame.py which rises a conflict with pygame library. Change the name and try it again.
This happens when you try to import from livrewires and in your current working directory there is a script named pygame.py.
Rename the pygame.py script to something else.
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.