1

This is my code:

@echo off
set "file=TobuMusic.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've downloaded a song from the music producer 7obu (Tobu) online, and I want to play the song on a batch file. This code helped a lot, but I don't understand how I can make it exactly 10 seconds long.

How can I achieve it?

arghtype
  • 4,376
  • 11
  • 45
  • 60
  • 5
    Maybe you could reference the link to where you got the code. Also, not understanding why you would use a batch file to create a Vbscript file and then execute the Vbscript. Why not just do everything in Vbscript? – Squashman Jan 24 '17 at 21:14
  • 1
    you need to check the state of the `Sound` object and to exit forcefully when the state is `1`. [**Here's an example with jscript**](https://github.com/npocmaka/batch.scripts/blob/master/hybrids/jscript/mediarunner.bat) hope you'll be able to translate it to vbscript.[More ways](https://www.reddit.com/r/Batch/comments/45jgbh/how_to_play_musicsounds_in_batch/) to play sound through the command line. – npocmaka Jan 24 '17 at 21:23
  • I guess the OP is talking about this post: [Play invisible music with batch file?](http://stackoverflow.com/a/23316395) – aschipfl Jan 25 '17 at 17:39
  • @Squashman, http://stackoverflow.com/a/23316395 –  Jan 25 '17 at 21:50
  • I also didn't do VBScript because I want to play a section of text after the music. –  Jan 25 '17 at 21:51

1 Answers1

0

I don't understand how I can make it exactly 10 seconds long.

Simply force your VB script to sleep for 10 seconds: instead of

wscript.sleep (int(Sound.currentmedia.duration)+1)*1000

use

iMaxDuration = Sound.Currentmedia.Duration + 0.5
If iMaxDuration > 10 Then iMaxDuration = 10
Wscript.Sleep Int( iMaxDuration * 1000 )
Sound.Close

In your batch script: instead of

echo wscript.sleep (int(Sound.currentmedia.duration^)+1^)*1000) >sound.vbs

use

echo iMaxDuration = Sound.Currentmedia.Duration + 0.5
echo If iMaxDuration ^> 10 Then iMaxDuration = 10
echo Wscript.Sleep Int( iMaxDuration * 1000 ^)
echo Sound.Close) >sound.vbs
JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • @bextem My pleasure. Please consider [accepting the answer](http://meta.stackoverflow.com/a/5235) if you find that it solved your problem. – JosefZ Jan 26 '17 at 00:22