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")
Related
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
My searches lead me to the Pywin32 which should be able to mute/unmute the sound and detect its state (on Windows 10, using Python 3+). I found a way using an AutoHotkey script, but I'm looking for a pythonic way.
More specifically, I'm not interested in playing with the Windows GUI. Pywin32 works using a Windows DLL.
so far, I am able to do it by calling an ahk script:
In the python script:
import subprocess
subprocess.call([ahkexe, ahkscript])
In the AutoHotkey script:
SoundGet, sound_mute, Master, mute
if sound_mute = On ; if the sound is muted
Send {Volume_Mute} ; press the "mute button" to unmute
SoundSet 30 ; set the sound level at 30
You can use the Windows Sound Manager by paradoxis (https://github.com/Paradoxis/Windows-Sound-Manager).
from sound import Sound
Sound.mute()
Every call to Sound.mute() will toggle mute on or off. Have a look at the main.py to see how to use the setter and getter methods.
If you're also building a GUI, wxPython (and I would believe other GUI frameworks) have access to the windows audio mute "button".
I'm trying to write a simple python script to stop the music being played by a Mac. I found some code that emulates the media buttons from the accepted answer here: emulate media key press on Mac.
Triggering the play/pause button works perfectly, but I only want to do so if there is music currently playing. Otherwise it turns on the music (the opposite of what I'm trying to do. Is there any way to get this information from the system?
I need to check if music was actually playing beforehand so I can know whether to resume it later.
If your use case is macOS specific, you can call AppleScript via Python:
import subprocess
subprocess.call(['osascript', '-e', 'tell application "iTunes" to pause'])
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.
I am trying to change the video of omx player in python based on an event. But the problem is the next video gets loaded only after the first video finishes.
import os
os.system('omxplayer video1.mp4')
if(flag==1):
os.system('omxplayer video2.mp4')
Is there a way to interrupt video1 in middle of playback and start second video.
Note:I am working on a raspberrypi with debian wheezy and python 2.7
Omxplayer has keyboard controls so you could send the corresponding keystroke q to stop it normally before opening the next instance. Here is a similar post where a person tried to achieve similar feature, it could be helpful.