I'm having trouble playing music in the background of my program in replit. I have tried using Pygame but couldn't get that to work, so I followed the official replit tutorial video.
I get the confirm audio screen but nothing plays, my file is .wav and is 29 secs long and plays when I go directly onto the file, but not when the program runs. I have uploaded the file into replit so it appears alongside main.py.
Any help would be appreciated, let me know if any additional info is needed
My code:
from replit import audio
source = audio.play_file("music.wav")
while True:
pass
For some odd reason, replit doesn't actually let you run code alongside the playing of music. Get rid of
while True:
pass
It should work then
Look at this, Replit has trouble using
while True:
pass
If you want to do nothing while the audio file is running, instead use time.sleep. You should also be able to run code while the file is running with no problems.
Related
I have a mp3 file namd 'audio.mp3' but for some reason no matter how hard I try I cannot find a useful link to help me code a programm that will listen to the computer and when hearing a specific sound that is exactly like my file 'audio.mp3', do something (that ofc i'll code)
I kept searching online for modules i could use or help but I only came across links that tell me how to record sound :
from playsound import playsound
playsound('audio.mp3')
I have no idea what to begin with :/
The perfect module I could think of is a module that I onlt have to give him the name of my mp3 file, then put an event listener and it'll do the part where it listens to my computer sounds and trigger my event when it hears my file
How Play to audio list in os.startfile use Python?
MY CODE:
import os
music_dir = "C:\\Users\\JACKSON KASI\\Music"
songs = os.listdir(music_dir)
print(songs)
for i in songs:
os.startfile(os.path.join(music_dir,i))
The next audio file should run after the first audio file is fully functional.
But my code is play all audio fast. It's not perfectly.
someone help me please.
Do you can use pygame (OR) python-vlc use to solve this proplem.
os.startfile is just launching the file asynchronously in the background. Per the docs:
startfile() returns as soon as the associated application is launched. There is no option to wait for the application to close, and no way to retrieve the application’s exit status.
Basically, you can't use it for your purpose, because there's no way to know when it's done.
There are many, many walkthroughs for playing media files; please search the web for solutions (providing a complete solution from scratch is somewhat beyond the scope of a StackOverflow question).
random = os.startfile(os.path.join(music_dir , songs[1])
this will work
I've been working on a media player in python that uses pygame.mixer to play music and PyQt4 to build the UI.
I'm currently using a while loop to check if a song is finished so the next song can be loaded in from the queue. In order to make sure that the user can still interact with the PyQt buttons while the while loop is running, I have put QtCore.QCoreApplication.processEvents() into the while loop.
However, now when I close the main window while music is playing, the program does not stop (as it normally does) and the music keeps playing.
The program also can't detect the app.aboutToQuit.connect() command while the music is playing (meaning it can when music isn't playing).
Any help would be greatly appreciated.
I found the answer to this issue a while ago so thought I should put it here for anyone else.
I gave the program an internal exit button in the ui, then made the button call os._exit(0). It worked perfectly then.
Checking the Python and PyQt documentation gave no comment on closing a application window while a while loop is being performed.
Any other solutions would be appreciated though.
I've been trying to get a short video to load and play within a pygame window. However, the program stalls when it tries to load the video. I'm also unable to abort the debugger.
import pygame
import pygame.movie
pygame.init()
print('TEST')
video = pygame.movie.Movie('D:\\Presentation\\video.mpg')
print('TEST')
This outputs 'TEST' once, so the video has not loaded (It keeps running "pygame.movie.Movie('D:\Presentation\video.mpg')" indefinitely).
I let it run for an hour but no luck, it keeps loading without any kind of exception or visible progress and the video is 1,636 KB large, 4 seconds long and was converted from a .mp4 using ffmpeg.
The directory is certainly correct for I tested with images in the same directive. I also restarted my PC (Windows 8.1 64-bit Python 3.2.5.1).
EDIT: I need an answer that will be relevant to the pygame module
I suggest you use Pyglet and follow the code I create here that plays a video.
In python 3.4 , I was trying to open a "wav" file using vlc in Linux. Here is my code:
import os,time
os.system("cvlc audio/some.wav")
time.sleep(3) #audio was one and half sec
a = 3+3
print (a)
It plays the audio but then doesn’t do the rest. What should I do to make it do them? more precisely what should I do to close the vlc program?
With solving the problem it will also be very grateful to know is there any easier way to play audio within the code specifically in python 3.4?
(platform independent code will be even more grateful!)
So the VLC player doesn't exit. The VLC player has a command line argument to close the player once the song/video has been played.
Playlist
These options define the behavior of the playlist. Some of them can be overridden in the playlist dialog box.
--play-and-exit, --no-play-and-exit
Play and exit (default disabled)
Source: https://wiki.videolan.org/VLC_command-line_help
Can you try the following?
os.system("cvlc audio/some.wav --play-and-exit")