I am making a batch game in notepad and I was wondering how I can play the file music in the background.
I have this:
@echo off
start Music.mp3
The closest your can get using internal commands of a command processor can only minimize a window. If you really want to "background" it, the script will be long and confusing.
start /min /c "" Music.mp3
Alternatively this:
@echo off
set "file=track12.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
cscript.exe //nologo sound.vbs
This creates a vbs
that starts the music without a new window.
User Lưu Vĩnh Phúc has suggested, using a hybrid script prevents the issue of cleaning up.
@echo off
rem ----START OF BATCH CODE---
rem You can paste your entire batch code down here, and use
rem the cscript command whenever you want to play the sound.
cscript //nologo "%~f0?.wsf" //job:VBS
rem ----END OF BATCH CODE---
<package>
<job id="JS">
<script language="VBScript">
Set Sound = CreateObject("WMPlayer.OCX.7")
Sound.URL = "%file%"
Sound.Controls.play
do while Sound.currentmedia.duration = 0
wscript.sleep 100
loop
wscript.sleep (int(Sound.currentmedia.duration)+1)*1000)
</script>
</job>
You can also play online music like this script :
@echo off
Title Listen to the music with batch and vbscript
mode con cols=60 lines=3 & color 9E
echo.
Echo To stop the music just close this window
set "file=http://www.chocradios.ch/djbuzzradio_windows.mp3.asx"
set "vbsfile=%tmp%\%~n0.vbs"
(
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
)>%vbsfile%
cscript /nologo %vbsfile%
I'm developing a batch game myself at the moment, and have worked out a similar solution of my own, that allows alot of flexibility in terms of flow of music, sound effects, volume etc, Without the player ever being visible.
The one thing that frustrated me the most though was devising a system to stop the music player if the user exited the game without using a quit option to call the taskill used to end the vbs script and it's Child processes.
I ended up devising a vbs script that monitors the cmd.exe and calls the cleanup/Taskkill program when there's no cmd.exe process active.
All the music control happens as a background process, so there's only ever one instance of cmd.exe running for the games operation.
There is advantage to creating your vbs scripts to control settings from your batch, as you can use parameters to pass information on which track, what volume, and whether to loop the sound / music or not. Because volume is sent as a parameter when music or sounds are played, this opens up Volume and Sound options for the player such as effectively muting sound effects and setting the volume of the music to taste - or disabling one or both.
The code for my solution is Documented and described here: https://pastebin.com/nm01MzhM
An extract of the music player script:
(
echo Set Sound = CreateObject("WMPlayer.OCX.7"^)
echo Sound.URL = "%~1"
echo Sound.settings.volume = %~2
echo Sound.settings.setMode "loop", %~3
echo Sound.Controls.play
echo While Sound.playState ^<^> 1
echo WScript.Sleep 100
echo Wend
)>PlayMusic.vbs
start /min PlayMusic.vbs
And can be heard (and seen) in effect here
Here's another script based on WMPlayer.OCX
.You can use it like:
mediarunner.bat Music.mp3
Here's one based on the bgsound
tag in internet explorer.It allows you also to set sound volume as parameter.Does not require installed media player unlike WMPlayer.OCX approach:
soundPlayer.bat Music.mp3 -50
One based on SP objects though unfortunately it can play only wav files:
spplayer.bat file.wav
None of the script creates temp files.