How to play on your vbs
Solved
brice-eve
Posted messages
109
Status
Member
-
cs_Le Pivert Posted messages 8437 Status Contributor -
cs_Le Pivert Posted messages 8437 Status Contributor -
Hello,
I've recently started developing a program in .vbs
and I want to play a music file that will start as soon as the program launches, but...
I've searched everywhere on the internet and found some codes like:
but they don't work.
Later I learned that sndrec32 no longer exists starting from Windows Vista, so I kept looking for another code to play sound, but I couldn't find anything.
Thank you in advance for your valuable help.
I've recently started developing a program in .vbs
and I want to play a music file that will start as soon as the program launches, but...
I've searched everywhere on the internet and found some codes like:
strSoundFile = "C:\Users\test_files\"
Set objShell = CreateObject("Wscript.Shell")
strCommand = "sndrec32 /play /close " & chr(34) & strSoundFile & chr(34)
objShell.Run strCommand, 0, True
but they don't work.
Later I learned that sndrec32 no longer exists starting from Windows Vista, so I kept looking for another code to play sound, but I couldn't find anything.
Thank you in advance for your valuable help.
3 answers
-
It worked with Vista but not anymore
Personally, I would use an external music player (command line) like ffplay or mplayer
--
[PaTaTe] -
Hello,
See this program:
https://codes-sources.commentcamarche.net/source/101004-ecouter-dj-buzz-radio-avec-djbuzzradio-vbs
by changing this line and putting the path of the audio file
Call Play(DJBuzzRadio)
Call Play (Audio path)
I did it and it works very well for mp3, wav, and wma
--
@+ The Pivert -
The program I indicated works very well.
Here is the modified program that allows you to choose an audio file and stop it on demand:
but I managed to find the solution by myself:
we notice that your code looks strikingly like the
Sub Play(URL)
Dim Chemin Call AskQuestion() '***************************************************************************** Sub Play(URL) Dim Sound Set Sound = CreateObject("WMPlayer.OCX") Sound.URL = URL Sound.settings.volume = 100 Sound.Controls.play do while Sound.currentmedia.duration = 0 wscript.sleep 100 loop wscript.sleep (int(Sound.currentmedia.duration)+1)*1000 End Sub Sub AskQuestion() Dim Question,MsgFR MsgFR = "Do you want to open an audio file?" & vbcr & "Yes = To listen" & vbcr & "No = To stop" & vbcr & String(50,"*") Question = MsgBox(MsgFR,vbYesNO+vbQuestion+vbSystemModal,Title) If Question = VbYes Then Call chercher()'find the audio file Call Play (Chemin)'play End If If Question = VbYes Then MsgBox "There is another instance running!" WScript.Quit() End If If Question = VbNo Then Call Kill("wscript.exe") End If If Question = VbNo Then Call Kill("wscript.exe") End If End Sub '***************************************************************************** 'Function to add double quotes in a variable Function DblQuote(Str) DblQuote = Chr(34) & Str & Chr(34) End Function '****************************************************************************** Function CommandLineLike(ProcessPath) ProcessPath = Replace(ProcessPath, "\", "\\") CommandLineLike = "'%" & ProcessPath & "%'" End Function '****************************************************************************** Sub Kill(MyProcess) Dim Titre,colItems,objItem,Processus,Question Titre = " Processus "& DblQuote(MyProcess) &" running " Set colItems = GetObject("winmgmts:").ExecQuery("Select * from Win32_Process " _ & "Where Name like '%"& MyProcess &"%' AND commandline like " & CommandLineLike(WScript.ScriptFullName) & "",,48) For Each objItem in colItems objItem.Terminate(0)' Kill this process Next End Sub '****************************************************************************** Sub chercher() sIniDir = "C:\Windows\*" sFilter = "MP3 File (*.mp3)|*mp3|Wave File(*.wav)|*wav|WMA File(*.wma)|*wma|" sTitle = "GetFileDlg by omen999 2014 - http://omen999.developpez.com" rep = GetFileDlgEx(Replace(sIniDir,"\","\\"),sFilter,sTitle) Chemin=rep & vbcrlf & Len(rep) Chemin= left(Chemin , len(Chemin)-2) 'Chemin=InputBox("Copy to put the path in the clipboard", "Search file path", Chemin) End Sub Function GetFileDlgEx(sIniDir,sFilter,sTitle) Set oDlg = CreateObject("WScript.Shell").Exec("mshta.exe ""about:<object id=d classid=clsid:3050f4e1-98b5-11cf-bb82-00aa00bdce0b></object><script>moveTo(0,-9999);eval(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(0).Read("&Len(sIniDir)+Len(sFilter)+Len(sTitle)+41&"));function window.onload(){var p=/[^\0]*/;new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(p.exec(d.object.openfiledlg(iniDir,null,filter,title)));close();}</script><hta:application showintaskbar=no />""") oDlg.StdIn.Write "var iniDir='" & sIniDir & "';var filter='" & sFilter & "';var title='" & sTitle & "';" GetFileDlgEx = oDlg.StdOut.ReadAll End Function
--
@+ Le Pivert-
Strange, the code tag is not effective when displaying the post on Chrome Android; Under Windows, no problem.
EDIT: No error in the code, it was a copy/paste issue, sorry :)
However, for the initialization directory, it's best to avoid hardcoding paths as much as possible. Here C:\Windows. Personally, this directory does not exist ^^ Better to use the dialog box inviting the user to choose their directory.- Hello POTATO
It's better to use the dialog box prompting the user to choose their directory.
That's what the code does, it opens an openfiledialog. I put this path for the start of the search. You can remove it and you'll be on the last opened file. Otherwise, you customize it. We are on a developers' forum, so we are capable of adapting it, this forum is meant for that, right?
Regards
Have a good weekend
See you later The Woodpecker
-