How Play to audio list in os.startfile use Python? - python

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

Related

Playing music in python in replit

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.

How can I send keystrokes and mouse movement to a specific PID?

How can I send keystrokes and mouse movements to a specific running program through its PID. I've used both pywinauto and pynput, and they work great, but I want to send keys to a program that is not in focus. I found this question: How to I send keystroke to Linux process in Python by PID? but it never explains what filePath is a path to.
If you could help solve for this example, that would be great! I want to send the "d" key to an open Minecraft tab for 10 seconds, and then send the "a" key for the next 10 seconds and stop. I would need this to be able to run in the background, so it could not send the keys to the computer as a whole, but only to the Minecraft tab. I am on Windows 10 by the way.
Any help would be appreciated!
Pretty sure you won't be able to, at least not easily let me explain a little bit how all of this works.
Lets start with the hardware and os, the OS has certain functions to read the input you give the computer. This input goes into a "pipe", the OS is reading input, and putting into the pipe, on the other side of the pipe there may be an application running, or it may not. The OS typically manages this (which app to put on the pipe listening) by defining which app/window is active. Apps access this pipe with the API given by the OS, they read the input and decide on it.
The libraries you cited above, change the values of the keyboard and mouse, in other words, they make the OS read other values, not the real ones, then the OS puts them in the "pipe", and are read by the app that is listening on the pipe (the one active). Some apps have their own API's for this, but I would guess Minecraft doesn't. If they don't have an API, what can you do? well, as I said, nothing easy, first of all "hacking" the app, in other words change it to listen to some other input/output rather than the one given by the OS, (this would be you making your own API). The other one would be you changing the OS, which would also be extremely hard, but maybe a tiny bitty easier. It also depends on your OS, I think Microsoft does offer input injection api's
So, simple options, first, run a VM with a GUI and use pywinauto, pyautogui, etc. The other option would be if you can run it in the browser, do so, and use something like Selenium to automate the input.
Quick note, why does selenium works and the browser can read input in the background? Easy, it's not, it just executes the code it would execute if it would have read the input! javascript, cool isn't
With ahk you can do this with Python+AutoHotkey
pip install ahk
pip install "ahk[binary]"
from ahk import AHK
from ahk.window import Window
ahk = AHK()
win = Window.from_pid(ahk, pid='20366')
win.send('abc') # send keys directly to the window
Note that some programs may simply ignore inputs when they are not in focus. However, you can test this works in general even when not in focus by testing with a program like notepad
Full disclosure: I author the ahk library.

Real Time Python program

I have a project in my job.
The target is to prepare 14 SD cards for provisioning any Raspberry Pi 3.
So I have to found a solution to do it automatically and follow which SD card is ready to start and which one is complete.
I have the idea to build with a Python3 program and a tkinter interface because I know a little bit Python and not others languages...
The program should work like this :
List every Windows drives where SD card is mounted
Push a button in front of the letter of the SD card drive to start the provisioning.
The provisioning is all steps to make the SD cards bootable with an OS. So I have to pass some DISKPART commands or equivalent in Python I think, if you have any suggestions ?
Show a statut in front of each drive to follow if the drive is pending, working, complete, etc.
I have a huge interogation about this. My program has to refresh every informations. I mean the program should work in real time or not ? What is the best way to proceed ?
To be clear, I don't want someone building to me this program, I just want to have some good idea to implement.
Thank you
You can use this module to execute commands. For example:
import subprocess
completed = subprocess.run(['ls', '-1'])
print('returncode:', completed.returncode)
I can't help with the Python part, but if you have a WinAPI window with message handling (so the WindowProc thing), WM_DEVICECHANGE is the message, here are the actual event categories and RegisterDeviceNotification is how you subscribe to it. Complete (but C) MSDN example is here
While implementing it may require some work, viability itself depends on getting access/not getting access to the message queue (from Python). Based on this and this it seems to be possible, but I have no experience with it.

Python – Check if Mac is Playing Music

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'])

How to close program in python opened by os.system()?

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")

Categories