0

I'm trying to make a batch file which plays music in the background like this But, I found out when I terminate the file by pressing X, the music is still playing.

This is a good sample:

@echo off
set file=http://www.soundjay.com/button/button-2.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
start /min sound.vbs

I have now 3 issues with this.

  1. It creates a file in the directory of the .bat itself.

  2. it doesn't delete it afterwards.

  3. It keeps playing till it ends and then terminates the background program, but it doesn't terminate it when I press X on the commandprompt

Community
  • 1
  • 1
FPSUsername
  • 17
  • 1
  • 8
  • First of all, you will need a "taskkill" command in there to close what ever is playing the music/sound. Second of all, you havnt told the batch to delete any files this can be done with "del" command. Whats wrong with it creating the file where the bat file is? should it go some where else? if so then use the "cd" command to change dir – NizonRox Feb 10 '16 at 10:03
  • I know it doesn't delete it, but I have no idea why it should create a file. It would be nice if it could be 1 executing file (.bat) which doesn't download mp3, but stream and doesn't create temporary files. – FPSUsername Feb 10 '16 at 12:29

1 Answers1

1

A way :

@Echo Off

set "file=http://www.soundjay.com/button/button-2.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
start /min sound.vbs

:loop
set /p $Answer=Press X + Enter to stop ! : 
if /i "%$Answer%"=="x" (
   Taskkill /f /im "wscript.exe" >nul
   del *.mp3
   exit/b
 )
goto:loop

EDIT :

If you want to stream your file use VLC command line option. If you want your files in %temp% you can move it or download it first to %temp%.

And if you want that the music stop when you click [X] just replace :

start /min sound.vbs

with

cscript //nologo sound.vbs
SachaDee
  • 9,245
  • 3
  • 23
  • 33
  • This should do everything the person is having as issues but the "It creates a file in the directory of the .bat itself." – NizonRox Feb 10 '16 at 11:31
  • Thanks @NizonRox. I Don't know if that still an issue cause the MP3 is deleted if you press X. But it's possible to download the mp3 in another directory fisrt, if this is what the OP need. That's not clear ! – SachaDee Feb 10 '16 at 11:43
  • I've worked with batch files that are executed from a tight packed usb at times but if space isnt an issue then your still only partly right cause if OP is running the script from his dekstop then it will soon be filled with mp3 files :P so I'd add a cd command in the start just for secure reasons :) – NizonRox Feb 10 '16 at 11:50
  • It's allso possible to move the Mp3 before running it. The script have to be more complete as it is. That just to show the OP the way to solve his problems. – SachaDee Feb 10 '16 at 12:00
  • The best way is not to download the mp3, but stream it. If files are made, best is in %temp% folder if you ask me. By pressing x I mean minimize, maximize, close at the top right of the window. Your code has some issues though, I get windows script host error in sound.vbs, line 4, char 1. Error: Sound.currentmedia. Code: 800A01A8 – FPSUsername Feb 10 '16 at 12:25
  • Is there a way to just play it with windows media player instead? It should run on any pc out of the box :) Edit: Perfect, only I can not echo anything with: cscript //nologo sound.vbs Last thing left is to not create a file when opened, or at least create the file and delete it when you press the close button in a temp folder and be able to echo stuff – FPSUsername Feb 10 '16 at 12:43
  • You can `echo` something before running `cscript //nologo sound.vbs` – SachaDee Feb 10 '16 at 12:55
  • Okay, but is it possible to echo and play sound at the same time? I will make a loop where it will do some sort of animation (or like scrolling text). You can think of those starwars intro's, scrolling text and background music, except it loops the whole thing after it is completed. – FPSUsername Feb 10 '16 at 13:04
  • Possible Just with the first script Starting the sound.vbs in another CMD task. BAT is very limited to handle with such thing. – SachaDee Feb 10 '16 at 13:29
  • Okay, I think I figured it out, I hope. It should run the music in the background using a cmd that is minimized (or maybe invisible?), while opening a new cmd with the animation code. This must be possible with only one batch file, right? In code, first open the new cmd with that animation code and then play sound, so it doesn't have to wait till the sound ends. EDIT: or or or, it should open a minimized browser that just streams the sound, should be easier to code, but not sure if it can be closed too, as if I have 2 tabs open, will it close all tabs or just the tab with the sound playing – FPSUsername Feb 10 '16 at 20:12