-3

I wanted to build a script playing an audio file in the background, so i found code on Stack Overflow to run an audio file without the window popping up:

@echo off
set file=song.mp3
( echo Set Sound = CreateObject("WMPlayer.OCX.7"^)
  echo Sound.URL = "%file%"
  echo Sound.Controls.play
  echo do while Sound.currentmedia.duration = 0
  echo wscript.sleep 100
  echo loop
  echo wscript.sleep (int(Sound.currentmedia.duration^)+1^)*1000) >sound.vbs
start /min sound.vbs

When i ran the file in File Explorer, it worked perfectly.

But my main goal is to get a Python script (.py file) to run it for me without having any visuals, so i tried calling the startfile() function from the os module in my python file, like this:

import os
from locate import this_dir

path = str(this_dir())

os.startfile(path + "\\run_song.py")

This time, no errors were provided, but the sound couldn't be heard.

I use Visual Studio Code and Python 3.9.7

Here is the code inside "run_song.py":

from os import startfile
from locate import this_dir

path = str(this_dir())

startfile(path + "\\sound.vbs")

Here Are The Insides Of "sound.vbs":

Set Sound = CreateObject("WMPlayer.OCX.7")
Sound.URL = "song.mp3"
Sound.Controls.play
do while Sound.currentmedia.duration = 0
wscript.sleep 100
loop
wscript.sleep (int(Sound.currentmedia.duration)+1)*1000

Yes, i tried using the VLC module and it didn't work. This error popped up.

FileNotFoundError: Could not find module 'C:\Users\Dani\Desktop\Code\libvlc.dll' (or one of its dependencies). Try using the full path with constructor syntax.   

Here Is The Principal File (run_song.py)'s code.

from locate import this_dir
import vlc

path = str(this_dir())

p = vlc.MediaPlayer("file:///" + path + "song.mp3")
p.play()
BugV VZero
  • 9
  • 2
  • 9
  • 1
    Show us the contents of `run_song.py`. – John Gordon Sep 21 '21 at 16:59
  • @John Gordon: Sure, Edited The Question To Show The Content. – BugV VZero Sep 21 '21 at 17:10
  • Start with trying to make "run_song.py" work on its own – Y.R. Sep 21 '21 at 17:13
  • I'm a bit confused by the question. In the first sentence, you say the audio file is run "silently". Then you say that when this program is run, no sound is produced. If you want sound, why are you running it silently? – John Gordon Sep 21 '21 at 17:14
  • `run_song.py` is missing a closing parenthesis. Is that a typo? Why are you running a python program to run a python program, to run a VBS file? – Mark Tolonen Sep 21 '21 at 17:15
  • @Y.R.: I Can't, Both Of The Winsound/Playsound Modules Are Not Working For Me, And If I Post A Help Question Here, It Will Get Downvoted, And My Private Feedback Will Say: "There Are Other Answers On This Question" When These Use The Playsound/Winsound Modules (Which, As I Said, Does Not Work Correctly For Me). So I Need To Use A File For The Audio, A File To Run The Audio File (I Want To Use The File As My Python Script Is Running), Hope This Info I Clear Enough. – BugV VZero Sep 21 '21 at 17:18
  • @John Gordon: Silently Means In The Background (Without Any Media Player) – BugV VZero Sep 21 '21 at 17:20
  • @Mark Tolonen: I Don't Understand – BugV VZero Sep 21 '21 at 17:21
  • The last line of run_song.py is missing a closing parenthesis, and Why Are You Capitializing Everything? You show a python script that launches `run_song.py`, and `run_song.py` launches a `.vbs` file. Why not just run "run_song.py"? – Mark Tolonen Sep 21 '21 at 17:22
  • Show us the contents of `sound.vbs`. – John Gordon Sep 21 '21 at 17:24
  • @Mark Tolonen: First Question: That Was My Fault, I Copied The Code But I Didn't Copy The Ending Parenthesis, Second Question: I Try Forgetting That I Want To Capitalize, But Everytime I Write Something On The Computer, I Forget That I Do Not Want To Capitalize. – BugV VZero Sep 21 '21 at 17:24
  • @John Gordon: Put The Contents Of "sound.vbs" In The Question. – BugV VZero Sep 21 '21 at 17:30
  • @juanpa.arrivillaga: Are You Sure? Then Why Are There No Errors In The Terminal? – BugV VZero Sep 21 '21 at 17:31
  • @juanpa.arrivillaga: But I Am! – BugV VZero Sep 21 '21 at 17:32
  • according to documentation, startfile exists in python 3.9 https://docs.python.org/3.9/library/os.html?highlight=os#module-os – Y.R. Sep 21 '21 at 17:32
  • @BugVVZero nvm my mistake – juanpa.arrivillaga Sep 21 '21 at 17:32
  • @juanpa.arrivillaga: Its Okay To Make Mistakes. – BugV VZero Sep 21 '21 at 17:33
  • No, I Tells Me That libVLC Is Missing, I Updated My Question For It To Include My Code + Error – BugV VZero Sep 21 '21 at 18:01

1 Answers1

0

os.startfile is not the function you are looking for. Take a look at the documentation:

this acts like double-clicking the file in Windows Explorer, or giving the file name as an argument to the start command from the interactive command shell: the file is opened with whatever application (if any) its extension is associated.

In order to play music files, you can install vlc python library using

pip3 install python-vlc

And try this simple method:

import vlc
p = vlc.MediaPlayer("C:/path/to/file/file.mp3")
p.play()

(taken from Playing mp3 song on python)

Y.R.
  • 371
  • 1
  • 7
  • Got This Error: FileNotFoundError: Could not find module 'C:\Users\Dani\Desktop\Code\libvlc.dll' (or one of its dependencies). Try using the full path with constructor syntax. – BugV VZero Sep 21 '21 at 17:36
  • Updated Question With My Code + Error – BugV VZero Sep 21 '21 at 17:39
  • Edited to be more accurate. Paths sometimes get annoying (mostly in windows). take a look at os.path and pathlib. also see https://stackoverflow.com/questions/2953834/windows-path-in-python – Y.R. Sep 21 '21 at 17:43
  • Do I Need To Install VLC Media Player To Use The Module? – BugV VZero Sep 21 '21 at 17:51