Home > Back-end >  Play Another Song in a batch music player consecutively
Play Another Song in a batch music player consecutively

Time:08-08

Good morning, I have written a batch script that plays a song on the click of a button, I have created a "playlist" that contains many songs, I wonder how to play a song after the previous one is finished...

The script used for the songs is this for all the songs:

:Song
set "file=Song.mp3"
( echo Set Sound = CreateObject("WMPlayer.OCX.7"^)
  echo Sound.URL = "%file%"
  echo Sound.settings.volume = 100
  echo Sound.settings.setMode "loop", True
  echo Sound.Controls.play
  echo While Sound.playState ^<^> 1
  echo      WScript.Sleep 100
  echo Wend
)>sound.vbs
start sound.vbs
echo Song>playing.txt
cls
goto start

The script writes the name of the song into a file called playing.txt which is used to let the script know if a song is being played and also know which one, and then perform other actions.

Here is an Example of the playlist using buttons:

:songs
cls
cmdMenuSel f870 "Song Number One - Author" "Song Number Two - Author" "Song Number Three - Author"
if %ERRORLEVEL% == 1 goto SongNumberOne
if %ERRORLEVEL% == 2 goto SongNumberTwo
if %ERRORLEVEL% == 3 goto SongNumberThree

:SongNumberOne
cls
set "file=SongNumberOne.mp3"
( echo Set Sound = CreateObject("WMPlayer.OCX.7"^)
  echo Sound.URL = "%file%"
  echo Sound.settings.volume = 100
  echo Sound.settings.setMode "loop", True
  echo Sound.Controls.play
  echo While Sound.playState ^<^> 1
  echo      WScript.Sleep 100
  echo Wend
)>sound.vbs
start sound.vbs
echo SongNumberOne>playing.txt
cls
goto start


:SongNumberTwo
cls
set "file=SongNumberTwo.mp3"
( echo Set Sound = CreateObject("WMPlayer.OCX.7"^)
  echo Sound.URL = "%file%"
  echo Sound.settings.volume = 100
  echo Sound.settings.setMode "loop", True
  echo Sound.Controls.play
  echo While Sound.playState ^<^> 1
  echo      WScript.Sleep 100
  echo Wend
)>sound.vbs
start sound.vbs
echo SongNumberTwo>playing.txt
cls
goto start


:SongNumberThree
cls
set "file=SongNumberThree.mp3"
( echo Set Sound = CreateObject("WMPlayer.OCX.7"^)
  echo Sound.URL = "%file%"
  echo Sound.settings.volume = 100
  echo Sound.settings.setMode "loop", True
  echo Sound.Controls.play
  echo While Sound.playState ^<^> 1
  echo      WScript.Sleep 100
  echo Wend
)>sound.vbs
start sound.vbs
echo SongNumberThree>playing.txt
cls
goto start

In the data folder There are a mp3 file that has the song name but formatted:

Song Number One = SongNumberOne.mp3

also have a .txt with the name of the song SongNumberOne.txt

This for every song

The real problem is that i can't create a single batch file for every song, cause i'm gonna add more artist to the program, and i have some buttons plugin that brakes when opened by another batch file.

I thought there was an attribute for the VBS file or something.

Also how do the program know that the song is finished?

the only thing I can think of, and I think it's the only thing possible in less than a whole ice age is to synchronize the time of the song to the batch, when the timer expires, it goes to the next song, if you have something to recommend for that.

P.S. i'm new Here :)

CodePudding user response:

Here is an example : on my test script i choose *.wav

You should modify two variables : The Path of your MusicFolder=folderpath\mp3files and the Extension : Ext=mp3

So the trick is to use the command Start with the switch /Wait in order to wait until the vbscript ends and so on ...


@echo off
Title Playing Music
Set "MusicFolder=%windir%\Media"
Set "Ext=wav"
Set "PlayedSong=%~dp0Played.txt"
If Exist "%PlayedSong%" Del "%PlayedSong%" 
Set /a Count=0
SetLocal EnableDelayedExpansion
@for /f "delims=" %%a in ('Dir /S /B %MusicFolder%\*.%Ext%') do (
    set /a count =1
    cls & echo( & echo [!Count!] - Playing "%%~nxa"
    Call :Play "%%a"
    Echo [!Count!] - "%%~nxa" is Played>>"%PlayedSong%"
)
Pause & exit
::----------------------------------------------------------
:Play <URL>
(
    echo Play "%~1"
    echo Sub Play(URL^)
    echo    Dim Sound
    echo    Set Sound = CreateObject("WMPlayer.OCX"^)
    echo    Sound.URL = URL
    echo    Sound.settings.volume = 100
    echo    Sound.Controls.play
    echo    While Sound.playState ^<^> 1
    echo        WScript.Sleep 100
    echo    Wend
    echo End Sub
)>"%Temp%\%~n0.vbs
Start "%~nx1" /wait wscript "%Temp%\%~n0.vbs"
Exit /b
::----------------------------------------------------------

EDIT: Or if you want show as Menu with a numbred files like this :

@echo off
Title Playing some wav's media from "%windir%\Media" folder
Set "MediaFolder=%windir%\media"
Set "Ext=*.wav *.mp3"
cd /d "%MediaFolder%"
setlocal enableDelayedExpansion
REM Count total wav's and store into array
set /a "Count=0"
@for %%f in (%Ext%) do (
    set /a "Count =1"
    set "list[!Count!]=%%f"
)
::---------------------------------------------------------------------------------
:Display_Results
cls
@for /L %%a in (1,1,%Count%) do (
    echo( [%%a] - "!list[%%a]!"
)
Echo( -----------------------------------------------------------------------------
Echo( Total Media's files found are equal to %Count% on this folder : "%MediaFolder%"
Echo( -----------------------------------------------------------------------------
echo(
If [%Count%] EQU [0] color 0C & echo( There are No Media Files found in this Folder "%MediaFolder%" & TimeOut /T 10 /NoBreak>nul & Exit /B 1
echo( Type the number of file that you want to play ...
set /p "Input="
@For /L %%i in (1,1,%Count%) Do (
    If "%INPUT%" EQU "%%i" (
        Echo Playing "!list[%%i]!"
        Call :Play "!list[%%i]!"
    )
)
Goto Display_Results
::---------------------------------------------------------------------------------
:Play <URL>
(
    echo Play "%~1"
    echo Sub Play(URL^)
    echo    Dim Sound
    echo    Set Sound = CreateObject("WMPlayer.OCX"^)
    echo    Sound.URL = URL
    echo    Sound.settings.volume = 100
    echo    Sound.Controls.play
    echo    While Sound.playState ^<^> 1
    echo        WScript.Sleep 100
    echo    Wend
    echo End Sub
)>"%Temp%\%~n0.vbs
Start "%~nx1" /wait wscript "%Temp%\%~n0.vbs"
Exit /b
::----------------------------------------------------------------------------------
  • Related