{"id":7271,"date":"2021-11-23T12:35:23","date_gmt":"2021-11-23T12:35:23","guid":{"rendered":"https:\/\/lgildv5i97.onrocket.site\/answers\/?post_type=question&#038;p=7271"},"modified":"2021-11-23T12:35:32","modified_gmt":"2021-11-23T12:35:32","slug":"playing-music-in-the-background-while-another-task-is-running","status":"publish","type":"question","link":"https:\/\/computing.net\/answers\/show\/vbs-playing-music-in-the-background-while-another-task-is-running\/998.html","title":{"rendered":"Playing music in the background while another task is running"},"content":{"rendered":"<div>Sometimes some tasks can be taken a lot of time to end up.<\/div>\n<div>This vbscript is created in order to play a random radio music in background while the script is running another task until to finish up and the music will stop too.<\/div>\n<div>\n<pre>'**********************************Description in English***********************************<\/pre>\n<pre>'This vbscript is created by Hackoo on 29\/09\/2019  \r\n'Sometimes some tasks can be taken a lot of time to end up.  \r\n'This code is created in order to play music in background while the script is running another  \r\n'task until to finish up and the music will stop too.  \r\n'So, the user can listen to the music playing while the script is running another task.  \r\n'**********************************Description en Fran\u00e7ais**********************************  \r\n'Parfois, certaines t\u00e2ches peuvent prendre beaucoup de temps pour finir.  \r\n'Ceci est cr\u00e9\u00e9 afin de jouer de la musique en arri\u00e8re-plan pendant que,  \r\n'le script ex\u00e9cute une autre t\u00e2che jusqu\u2019\u00e0 la terminer et la musique s\u2019arr\u00eatera aussi.  \r\n'Ainsi, l'utilisateur peut \u00e9couter la musique pendant que le script ex\u00e9cute une autre t\u00e2che.  \r\n'*******************************************************************************************  \r\nOption Explicit  \r\nIf AppPrevInstance() Then   \r\n\tMsgBox \"The script is already Running\" &amp; vbCrlf &amp;_  \r\n\tCommandLineLike(WScript.ScriptName),VbExclamation,\"The script is already Running\"      \r\n\tWScript.Quit    \r\nElse  \r\n\tCall Run_as_Admin()  \r\n\tDim Title,EndUP,WS,fso,Temp,WSF_File,URL_Music,CMD,Process_Music,StartTime,Duration  \r\n\tTitle = \"Playing music while another task is running by \"&amp; Chr(169) &amp;\" Hackoo 2019\"  \r\n\tEndUP = False  \r\n\tSet WS = CreateObject(\"wscript.Shell\")  \r\n\tSet fso = CreateObject(\"Scripting.FileSystemObject\")  \r\n\tTemp = WS.ExpandEnvironmentStrings(\"%Temp%\")  \r\n\tWSF_File = Temp &amp; \"\\Music.wsf\"  \r\n\tURL_Music = Random_Music  \r\n\tCall Create_WSF_Music_File()  \r\n\tCMD = \"wscript.exe \" &amp; DblQuote(WSF_File) &amp; \" \/\/job:PlayMusic \"&amp; DblQuote(URL_Music) &amp;\"\"  \r\n\tSet Process_Music = WS.Exec(cmd)  \r\n\t  \r\n\tWS.Popup \"Playing this Radio music \"&amp; DblQuote(URL_Music) &amp; vbCrlf &amp;_  \r\n\t\"in the background while the script is running another task until to finish it\"_  \r\n\t,5,Title,vbInformation + vbSystemModal  \r\n\t  \r\n\tDo While Process_Music.Status = 0  \r\n\t\tIf EndUP = False Then  \r\n'**********************************************************************************************************************  \r\n'Your Main Code goes Here  \r\nStartTime = Timer  \r\nCall RUN_CMD ( _  \r\n\t\t\t\"echo.&gt;%Tmp%\\LogCMD.txt\" &amp;_  \r\n\t\t\t\"&amp; (Tracert.exe www.codereview.stackexchange.com\" &amp;_  \r\n\t\t\t\"&amp; Ping www.codereview.stackexchange.com\" &amp;_  \r\n\t\t\t\"&amp; Tracert.exe www.google.com\" &amp;_  \r\n\t\t\t\"&amp; Ping www.google.com\" &amp;_  \r\n\t\t\t\"&amp; NetStat -abnof)&gt;&gt;%Tmp%\\LogCMD.txt\" &amp;_  \r\n\t\t\t\"&amp; Start \/MAX %Tmp%\\LogCMD.txt\"_  \r\n\t\t\t)  \r\n\t\t\t  \r\n\t\t\tDuration = FormatNumber(Timer - StartTime, 0)  \r\n\t\t\tWS.Popup \"The task had taken a run time until its completion about :\" &amp; vbCrlf &amp;_  \r\n\t\t\tvbTab &amp; convertTime(Duration) &amp; vbCrlf &amp; _  \r\n\t\t\tvbTab &amp; WScript.ScriptName,10,Title,vbExclamation + vbSystemModal  \r\n'**********************************************************************************************************************  \r\n\t\tElse  \r\n\t\t\tOn Error Resume Next  'to ignore \"invalid window handle\" errors  \r\n\t\t\tProcess_Music.Terminate  \r\n\t\t\tOn Error Goto 0  \r\n\t\t\tEndUP = True  \r\n\t\tEnd If  \r\n\tLoop  \r\nEnd If  \r\n'----------------------------------------------------------------------------------------  \r\nSub Create_WSF_Music_File()  \r\n\tDim oWSF  \r\n\tSet oWSF = fso.OpenTextFile(WSF_File,2,True)  \r\n\toWSF.WriteLine \"\"  \r\n\toWSF.WriteLine \t\" language=\"\"Vbscript\"\"&gt;\"  \r\n\toWSF.WriteLine \t\"Dim URL_Music\"  \r\n\toWSF.WriteLine \t\"URL_Music = WScript.Arguments(0)\"  \r\n\toWSF.WriteLine \t\"Call Play(URL_Music)\"  \r\n\toWSF.WriteLine \"Function Play(URL)\"  \r\n\toWSF.WriteLine \t\"Dim Sound\"  \r\n\toWSF.WriteLine \t\"Set Sound = CreateObject(\"\"WMPlayer.OCX\"\")\"                 \r\n\toWSF.WriteLine \t\"Sound.URL = URL\"  \r\n\toWSF.WriteLine \t\"Sound.settings.volume = 100\"                                 \r\n\toWSF.WriteLine \t\"Sound.Controls.play\"                                       \r\n\toWSF.WriteLine \t\"Do while Sound.currentmedia.duration = 0\"                  \r\n\toWSF.WriteLine \t\t\"wscript.sleep 100\"                                         \r\n\toWSF.WriteLine \t\"Loop\"    \r\n\toWSF.WriteLine \"End Function\"  \r\n\toWSF.WriteLine \t\"\"  \r\n\toWSF.WriteLine \"\"  \r\nEnd Sub  \r\n'----------------------------------------------------------------------------------------  \r\nFunction DblQuote(Str)  \r\n\tDblQuote = Chr(34) &amp; Str &amp; Chr(34)  \r\nEnd Function  \r\n'----------------------------------------------------------------------------------------  \r\nFunction RUN_CMD(StrCmd)  \r\n\tDim ws,MyCmd,Result  \r\n\tSet ws = CreateObject(\"wscript.Shell\")   \r\n\tMyCmd = \"CMD \/C \" &amp; StrCmd &amp; \" \"  \r\n\tResult = ws.run(MyCmd,0,True)  \r\n\tEndUP = True  \r\nEnd Function  \r\n'----------------------------------------------------------------------------------------  \r\nFunction Random_Music()  \r\n\tDim URL1,URL2,URL3,URL4,URL5,URL6,ListMusic,i,j,tmp  \r\n\tURL1 = \"http:\/\/94.23.221.158:9197\/stream\"  \r\n\tURL2 = \"http:\/\/www.chocradios.ch\/djbuzzradio_windows.mp3.asx\"  \r\n\tURL3 = \"http:\/\/vr-live-mp3-128.scdn.arkena.com\/virginradio.mp3\"  \r\n\tURL4 = \"http:\/\/185.52.127.168\/fr\/30201\/mp3_128.mp3?origine=fluxradios\"  \r\n\tURL5 = \"http:\/\/icecast.skyrock.net\/s\/natio_mp3_128k\"  \r\n\tURL6 = \"http:\/\/185.52.127.173\/fr\/30601\/mp3_128.mp3?origine=tunein\"  \r\n\tListMusic = array(URL1,URL2,URL3,URL4,URL5,URL6)  \r\n\tRandomize  \r\n\tFor i = 0 To UBound(ListMusic)  \r\n\t\tj = Int((UBound(ListMusic) - i + 1) * Rnd + i)  \r\n\t\ttmp = ListMusic(i)  \r\n\t\tListMusic(i) = ListMusic(j)  \r\n\t\tListMusic(j) = tmp  \r\n\tNext    \r\n\tRandom_Music=tmp  \r\nEnd Function  \r\n'----------------------------------------------------------------------------------------  \r\nFunction AppPrevInstance()  \r\n\tWith GetObject(\"winmgmts:\" &amp; \"{impersonationLevel=impersonate}!\\\\.\\root\\cimv2\")    \r\n\t\tWith .ExecQuery(\"SELECT * FROM Win32_Process WHERE CommandLine LIKE \" &amp; CommandLineLike(WScript.ScriptFullName) &amp; _  \r\n\t\t\t\" AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'\")  \r\n\t\t\tAppPrevInstance = (.Count &gt; 1)  \r\n\t\tEnd With  \r\n\tEnd With  \r\nEnd Function      \r\n'----------------------------------------------------------------------------------------  \r\nFunction CommandLineLike(ProcessPath)  \r\n\tProcessPath = Replace(ProcessPath, \"\\\", \"\\\\\")  \r\n\tCommandLineLike = \"'%\" &amp; ProcessPath &amp; \"%'\"   \r\nEnd Function  \r\n'----------------------------------------------------------------------------------------  \r\nFunction convertTime(seconds)  \r\n\tDim ConvSec,ConvHour,ConvMin  \r\n   ConvSec = seconds Mod 60  \r\n   If Len(ConvSec) = 1 Then  \r\n         ConvSec = \"0\" &amp; ConvSec  \r\n   End If  \r\n   ConvMin = (seconds Mod 3600) \\ 60  \r\n   If Len(ConvMin) = 1 Then  \r\n         ConvMin = \"0\" &amp; ConvMin  \r\n   End If  \r\n   ConvHour =  seconds \\ 3600  \r\n   If Len(ConvHour) = 1 Then  \r\n         ConvHour = \"0\" &amp; ConvHour  \r\n   End If  \r\n   convertTime = ConvHour &amp; \":\" &amp; ConvMin &amp; \":\" &amp; ConvSec  \r\nEnd Function  \r\n'----------------------------------------------------------------------------------------  \r\nSub Run_as_Admin()  \r\nIf Not WScript.Arguments.Named.Exists(\"elevate\") Then  \r\n   CreateObject(\"Shell.Application\").ShellExecute DblQuote(WScript.FullName) _  \r\n   , DblQuote(WScript.ScriptFullName) &amp; \" \/elevate\", \"\", \"runas\", 1  \r\n    WScript.Quit  \r\nEnd If  \r\nEnd Sub<\/pre>\n<\/div>\n","protected":false},"author":5,"featured_media":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"inline_featured_image":false,"iawp_total_views":13},"question-category":[55],"question_tags":[],"class_list":["post-7271","question","type-question","status-publish","hentry","question-category-programming"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question\/7271","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question"}],"about":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/types\/question"}],"author":[{"embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/comments?post=7271"}],"wp:attachment":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/media?parent=7271"}],"wp:term":[{"taxonomy":"question-category","embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question-category?post=7271"},{"taxonomy":"question_tags","embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question_tags?post=7271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}