0

How to play WAV file using VBScript in HTML?

I have got following code but it is not working.

<script type="text/vbscript" language="VBScript"> 
   Dim strSoundFile, strCommand  
   strSoundFile = "C:\Sounds\Sound1.wav"
   Set objShell = CreateObject("Wscript.Shell")
   strCommand = "sndrec32 /play /close " & chr(34) & strSoundFile & chr(34)
   objShell.Run strCommand, 0, True
</script>

I found another code but it doesnt work as well if I use it in HTML page but it is working great in a *.VBS.

  Sub Play(SoundFile)
            Dim Sound
            Set Sound = CreateObject("WMPlayer.OCX")
            Sound.URL = SoundFile
            Sound.settings.volume = 100
            Sound.Controls.play
            do while Sound.currentmedia.duration = 0
                wscript.sleep 100
            loop
            wscript.sleep(int(Sound.currentmedia.duration)+1)*1000
            End Sub

I have found this link https://support.microsoft.com/es-es/kb/279022 but I am not shure if it is a correct way...

The way is working fine via in BODY tag is following

<object classid="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6" id="WindowsMediaPlayer" 
width="242" height="202" style="position:absolute; left:1;top:1;">
  <param name="URL" value="C:\Sound1.wav">
  <param name="autoStart" value="1">
</object>

Could it be done using this?

<script type="text/vbscript" language="VBScript"> HERE </script>

I am using IE8 under MS Windows 7 Pro.

NoWar
  • 36,338
  • 80
  • 323
  • 498
  • Related. http://stackoverflow.com/questions/13402336/play-sound-file-in-a-web-page-in-the-background – user692942 Nov 24 '15 at 15:34
  • @Lankymart No it is not related, man. I need to do it using VBScript. – NoWar Nov 24 '15 at 15:36
  • Why do you need to use VBScript? Some explanation would help. Either case you can still access the ` – user692942 Nov 24 '15 at 15:38
  • @Lankymart I need it. I dont need to use the approach you are suggesting here http://stackoverflow.com/questions/13402336/play-sound-file-in-a-web-page-in-the-background – NoWar Nov 24 '15 at 15:40
  • 1
    That isn't what I was suggesting at all, what you are not grasping is that HTML5 provides the ` – user692942 Nov 24 '15 at 15:44
  • @Lankymart Thank you! Woul dyou min dto seee my updated question, please? – NoWar Nov 24 '15 at 15:46
  • Neither of those options are good - 1. Attempts to run shell commands to play a WAV file through Sound Recorder rather then being part of the web page and could potentially be a security risk. 2. Uses Windows Media Player ActiveX control which limits compatibility. – user692942 Nov 24 '15 at 15:46
  • @Lankymart :) I know it all. I just need to get working this code and that is all. Do you know how to do it? Would be great to get some help, bro! – NoWar Nov 24 '15 at 15:52
  • Did you look at the examples from the Audio/Video DOM link [I posted](http://stackoverflow.com/questions/33897689/how-to-play-wav-file-using-vbscript-in-html?noredirect=0#comment55558313_33897689)?, also another one here [HTML Audio/Video DOM Reference](http://www.w3schools.com/tags/ref_av_dom.asp). – user692942 Nov 24 '15 at 15:54
  • @Lankymart Thank you! I am not gonna use DOM at all to play WAV file. I dont need it. – NoWar Nov 24 '15 at 15:56
  • 1
    I'm really lost you're not going to use the DOM? what you going to use instead Sound Recorder?? Seriously you say *"I know it all"* but so far you don't seem to know to much apart from ripping any old code from some where without understanding it first. **If you want to play a sound inside a webpage with a modern internet browser then HTML5 Audio is your best option period.** – user692942 Nov 24 '15 at 15:59
  • Oh your using IE8. *Skulks away...* – user692942 Nov 24 '15 at 16:03
  • @Lankymart Let me redefine the task. Probably it will be much clear what I need. So , I need to play WAV file periodicall using another couple of sounds files periodically and executing some specific functions when page is loaded completly in VBSCRIPT. I am not goona use the DOM. All code is done via VBSCRIPT I just need to get working WAV file. – NoWar Nov 24 '15 at 16:03
  • Best option with any IE browser prior to IE9 is flash. Also when I said use the DOM I was referring to manipulating the DOM in code so you could use the DOM, otherwise why would you be using a HTML page? – user692942 Nov 24 '15 at 16:06
  • @Lankymart Well... if you can provide some code so I can use DOM and play WAV somehow within `` will be great! – NoWar Nov 24 '15 at 16:13
  • There's also an example here that uses `` for older browsers [HTML5 Audio not working in ie7 or ie8](http://stackoverflow.com/a/20888014/692942). – user692942 Nov 24 '15 at 16:14
  • @Lankymart I respect your attention to this topic but I need solution to be done within `` but the way how to do it is not important. Any clue? – NoWar Nov 24 '15 at 16:20
  • You see that attitude right there grates on me, I'm on here to help people learn but when someone isn't willing to help themselves I draw the line and move on. I've given countless references, examples and links to go away and research a solution I'm not going to do any more. Also because I feel it's worth pointing out **how you do something especially in programming / development is important** but I'm just old fashioned that way. – user692942 Nov 24 '15 at 16:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/96075/discussion-between-dimi-and-lankymart). – NoWar Nov 24 '15 at 20:47

1 Answers1

0

Finally I could find correct approach to do what I need is described here

Basically the idea is following

  1. We cannot use code above in the HTML instead we have to use embedded Windows Media Player object.
  2. We can create any methods we need to manipulate that object in VBScript.
Community
  • 1
  • 1
NoWar
  • 36,338
  • 80
  • 323
  • 498