Advertisement
T3RRYT3RR0R

Complete Batch Music Solution

Dec 27th, 2019
656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 8.71 KB | None | 0 0
  1. ::: All programs Designed by T3RRYG00D Games.
  2.  
  3. ::: This Guide Covers All the Programs Necessary to Make a Batch / Vbs Hybrid Music Player which
  4. ::: offers Independent User control over Volume settings for both songs and sound effects, Control of Music Flow, and
  5. ::: a means by which to automatically stop the music - However the batch gets closed.
  6. :::
  7. ::: The player does not appear To the user, nor do the batch programs that control it.
  8. ::: The Player utilizes Windows Media Player, and can play any sound file Compatible with W.M.P.
  9.  
  10. ::: This Guide does not detail how to change volume setting, but it's just a case of choosing a method of input
  11. ::: for the player to select the volume they desire, then saving that setting to the file used by the program
  12. ::: in the same format initially used to store the settings.
  13.  
  14. ::: Preperations to make in your Games Launcher:
  15.  
  16. ::: In your Games Launcher (GameName.bat), Which should be in the Top level of the Games Directory (Folder) Set "GameDir=%~dp0"
  17. ::: %~dp0 is the fully qualified filepath for the folder your games in. Example- C:\Users\Username\desktop\Gamename\
  18. ::: %~dp0 expands for the batch program currently in effect. It's assigned to a variable, as it will change when another batch is in use.
  19. ::: This will define the relative path for all other files and programs in your games folder, Irregardless of where the folder is located.
  20.  
  21. ::: Other Filepaths to Define in your Games starting Program (With regards to Sound):
  22.  
  23. ::: 'sounds' should be a subfolder in your Games Folder
  24. ::: Set "sounds=%GameDir%sounds"
  25.  
  26. ::: These are small Programs and files to handle Different aspects of controlling Music.
  27. ::: Set "MusicSettings=%sounds%\MusicSettings.txt"
  28. ::: Set "AutoStop=%sounds%\AutoStop.bat
  29. ::: Set "StartMusic=%sounds%\StartMusic.bat
  30. ::: Set "StopMusic=%sounds%\StopMusic.bat
  31. ::: Set "ChangeSong=%sounds%\ChangeSong.bat
  32.  
  33. ::: ******************************* Default Settings *******************************
  34.  
  35. ::: Before Playing Music, you need to define your default settings as variables.
  36. ::: The 'IF not exist' check will ensure player selected settings are not overriden.
  37. ::: The first number is used to check if Music is enabled. 1 is on, 0 is off.
  38. ::: The second number is used to check if Sound effects are enabled. 1 is on, 0 is off.
  39. ::: The third number is the Music Volume
  40. ::: The fourth number is the Sound Effect Volume
  41.  
  42. IF not exist "%Musicsettings%" (
  43. ECHO 1
  44. ECHO 1
  45. ECHO 50
  46. ECHO 30
  47. )>%Musicsettings%
  48.  
  49. ::: Immediately after Settings are created, they need to be assigned to variables.
  50.  
  51. <"%MusicSettings%" (
  52. Set /p MusicOI=
  53. Set /p SEffectsOI=
  54. Set /p MusicVolume=
  55. Set /p SEffectsVolume=
  56. )
  57.  
  58. ::: Implements Disabled sound settings without Permanently changing stored Volume Settings.
  59.  
  60. IF %MusicOI%==0 Set "MusicVolume=0"
  61. IF %SEffectsOI%==0 Set "SEffectsVolume=0"
  62.  
  63. ::: *End Assignment of Sound Settings*
  64.  
  65. ::: Before Starting your Music, you want to put in place a way to close it Even if
  66. ::: the player closes the console. To do this, you can use a VBS script to monitor
  67. ::: for when cmd.exe gets closed. Once the game is closed, it will call a program
  68. ::: to end the music.
  69.  
  70. ::: ********************************* AutoStop.bat *********************************
  71.  
  72. ::: Should be Called early in your launcher, after filepaths are set.
  73.  
  74. @ECHO OFF
  75.  
  76. ::: Creates a VBS script to monitor the Games Process
  77.  
  78. (
  79. ECHO Set objWMIService = GetObject ("winmgmts:"^) 'Defines winmgmts service
  80. ECHO Set proc = objWMIService.ExecQuery("select * from Win32_Process Where Name='cmd.exe'"^) 'Gets Current Process Count
  81. ECHO DO while proc.count ^> 0 'Establishes Loop that ends when no cmd.exe windows or bat programs are open
  82. ECHO Set proc = objWMIService.ExecQuery("select * from Win32_Process Where Name='cmd.exe'"^) 'Gets Current Process Count
  83. ECHO if proc.count ^< 1 then exit do 'exits while loop once bat program is closed
  84. ECHO wscript.sleep 2500 'Delays loop by 2.5 seconds to reduce CPU usage
  85. ECHO loop 'Returns to get new process Count
  86. ECHO Set WshShell=createobject("wscript.shell"^) 'Creates Shell to
  87. ECHO WshShell.run "%GameDir%sounds\StopMusic.bat", 0, false 'Launches hidden .bat to close vbs player using TASKKILL once Game is closed
  88. ECHO WshShell.run "%~0", 0, false 'launches the program in hidden state to restart this vbs script. Does not occur if Game is closed.
  89. ) >%GameDir%sounds\MonitorEXE.vbs
  90.  
  91. ::: Starts the VBS to monitor if game is open
  92.  
  93. Start %GameDir%sounds\MonitorEXE.vbs
  94.  
  95. ::: Used to Identify to Stop Music that AutoStop Needs to be Called when Changing Songs.
  96. ::: When Changing Songs, Use ChangeSong.bat
  97.  
  98. Set "Monitoring=0"
  99.  
  100. ::: optional notification to user that Autostop is active.
  101. ::: mshta vbscript:msgbox("Autostop Active",0,"Your Games Name as Title")(window.close)
  102.  
  103. GOTO :EOF
  104.  
  105. ::: ********** End of Autostop.bat Program. **********
  106.  
  107.  
  108. ::: To begin playing music, call your StartMusic.bat program.
  109. ::: StartMusic.bat NEEDS to be called the first time a SONG is played in your Batch
  110. ::: this is due to ChangeSong Calling StopMusic. If there's no music being played,
  111. ::: An error message will occur when StopMusic tries to... Stop the Music.
  112.  
  113. ::: *StartMusic.bat* MUST be used for Sound Effects.
  114.  
  115. ::: Your call uses three parameters, which you change depending whether your Starting a song
  116. ::: or Playing a Sound effect. The 1st parameter is the filename of your song, the 2nd is the volume
  117. ::: and the third is whether Looping is true or false.
  118. ::: For songs, you want to use true, For Sound effects, use false
  119. :::
  120. ::: Your Songs and Sound Effects need to be located in your sounds folder. This allows you to
  121. ::: Make the calls demonstrated below with Just the Filename and extension - No Filepath Required.
  122.  
  123. ::: To start a Song (Only for the First time your program starts a song):
  124.  
  125. CALL "%StartMusic%" filename.ext %MusicVolume% true
  126.  
  127. ::: To play a Sound Effect:
  128.  
  129. CALL "%StartMusic%" filename.ext %SEffectsVolume% false
  130.  
  131.  
  132. ::: ******************************** StartMusic.bat ********************************
  133.  
  134. @ECHO OFF
  135.  
  136. ::: Change to the Directory you want to create the Music Launcher in.
  137. ::: Being in the same Directory your sound file in is what allows you to make the calls
  138. ::: with just the filename and extension as previously mentioned.
  139.  
  140. PUSHD %sounds%
  141.  
  142. ::: Ensure no Conflict with the Previous Script.
  143.  
  144. IF exist PlayMusic.vbs (
  145. DEL PlayMusic.vbs
  146. )
  147.  
  148. ::: Creates a vbs Script to launch the music (Occurs without any visual indication or prompting)
  149.  
  150. (
  151.   echo Set Sound = CreateObject("WMPlayer.OCX.7"^)
  152.   echo Sound.URL = "%~1"
  153.   echo Sound.settings.volume = %~2
  154.   echo Sound.settings.setMode "loop", %~3
  155.   echo Sound.Controls.play
  156.   echo While Sound.playState ^<^> 1
  157.   echo      WScript.Sleep 100
  158.   echo Wend
  159. )>PlayMusic.vbs
  160.  
  161. start /min PlayMusic.vbs
  162.  
  163. ::: Return to the Previous Directory
  164.  
  165. POPD
  166.  
  167. ::: Exits StartMusic and returns to the Previous program.
  168.  
  169. GOTO :EOF
  170.  
  171. ::: ********** End of StartMusic.bat Program. **********
  172.  
  173. ::: ChangeSong MUST NOT be used to play sound effects, as doing so will Stop songs.
  174. ::: For ALL times APART FROM the STARTING SONG you want to change SONGS, You MUST Call ChangeSong.
  175. :::
  176. ::: Your call uses three parameters, which you change depending whether your Starting a song
  177. ::: or Playing a Sound effect. The 1st parameter is the filename of your song, the 2nd is the volume
  178. ::: and the third is whether Looping is true or false.
  179. ::: For songs, you want to use true.
  180.  
  181. ::: To Change Song:
  182.  
  183. CALL "%ChangeSong%" filename.ext %MusicVolume% true
  184.  
  185. ::: ******************************* ChangeSong.bat ********************************
  186.  
  187. @ECHO OFF
  188.  
  189. ::: The Monitoring Variable is used to Convey to StopMusic that it needs to restart
  190. ::: the AutoStop.bat, as it gets closed with the musicplayer. AutoStop resets the
  191. ::: variable to 0, preventing a loop from occuring.
  192.  
  193. Set "Monitoring=1"
  194.  
  195. CALL %StopMusic%
  196.  
  197. ::: Forward the settings to StartMusic.Bat
  198.  
  199. CALL %StartMusic% %~1 %~2 %~3
  200.  
  201. GOTO :EOF
  202.  
  203. ::: ********** End of ChangeSong.bat Program. ***********
  204.  
  205.  
  206. ::: ********************************* StopMusic.bat ********************************
  207.  
  208. @ECHO OFF
  209.  
  210. ::: Stops the vbs.script controlling Windows Media Player - As well as the AutoStop Monitor.
  211.  
  212. taskkill /pid WScript.exe /f /t >nul
  213.  
  214. ::: Delay required to allow time for TASKKILL to complete execution before restarting Autostop
  215. ::: failure to implement a delay will restart in AutoStop being wiped out By TASKKILL
  216.  
  217. Timeout 1 > nul
  218.  
  219. ::: Restarts the Autostop Monitor If game is Still active. This is why monitoring is Set to 0 in AutoStop.bat
  220.  
  221. IF %Monitoring%==1 CALL "%AutoStop%"
  222.  
  223. ::: Exits the StopMusic and returns to the Previous program if Game is still active.
  224.  
  225. GOTO :EOF
  226.  
  227. ::: ********** End of StopMusic Program. ***********
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement