Share this page 

Play sound from a batch file (Windows)Tag(s): Misc Prog HowTo


Play WAV
[playwav.cmd]
@if (@X)==(@Y) @end /* JScript comment
    @echo off

    cscript //E:JScript //nologo "%~f0" %*

    exit /b %errorlevel%

@if (@X)==(@Y) @end JScript comment */

if (WScript.Arguments.Length == 0) {
   WScript.Echo(WScript.ScriptName + " file_to_play");
   WScript.Quit(0)
}
var fso= new ActiveXObject("Scripting.FileSystemObject");
var file=WScript.Arguments.Item(0);

if (!fso.FileExists(file)){
   WScript.Echo(file + " does not exist");
   WScript.Echo("usage:");
   WScript.Echo(WScript.ScriptName + " file_to_play");
   WScript.Quit(1);
}


var spVoice = new ActiveXObject("SAPI.SpVoice");
var spFile = new ActiveXObject("SAPI.SpFileStream.1");
spFile.Open(file);
spVoice.SpeakStream(spFile);
example :
playwav %windir%/media/alarm02.wav
Play MP3
[playmp3.cmd]
@if (@X)==(@Y) @end /* JScript comment
   @echo off
   cscript //E:JScript //nologo "%~f0" %*

   exit /b %errorlevel%
@if (@X)==(@Y) @end JScript comment */


if (WScript.Arguments.Length == 0) {
   WScript.Echo(WScript.ScriptName + " file_to_play");
   WScript.Quit(0)
}
var file=WScript.Arguments.Item(0);
var fso= new ActiveXObject("Scripting.FileSystemObject");

if (!fso.FileExists(file)){
   WScript.Echo(file + " does not exist");
   WScript.Echo("usage:");
   WScript.Echo(WScript.ScriptName + " file_to_play");
   WScript.Quit(1);
}

var player = new ActiveXObject("WMPlayer.OCX");
player.URL=fso.GetAbsolutePathName(file);
player.controls.play();

while(player.playState!=1){
   WScript.Sleep(100);
}
player.close();
example :
playmp3 "C:\mp3\Amy Winehouse - Rehab.mp3"