Link to home
Start Free TrialLog in
Avatar of sidwelle
sidwelleFlag for United States of America

asked on

play one or more of the system sounds from script in html page

Writing an HTA application.

I want to play one or more of the system sounds w/o having to include an audio file w/the app.  Really don't want to have to embed it either.

Is there not a simple way to just call up one of the system sounds ?


Thanks


Avatar of Scott Fell
Scott Fell
Flag of United States of America image

You need to embed the sound file because the browser is not going to be able to play a system sound or anything from the system for security reasons.

Avatar of sidwelle

ASKER

I thought an HTA didn't have security issues and was run at the OS, not in the browser ?

I can search and find lots of examples of <Audio> tags, but I don't see any examples of how to embed the binary part of the MP3 or WAV files ?


Do you have any examples ?

Hi,


I don't know what the sound is for but it is some sort of notification, you sould plan an alternative way in case the sound is turned off on the system.

Scott, you link provides an example of a tag, I don't see how to embed the data of the MP3 file.

I think you may need to use the "![CDATA " tag to hold the file content ?




I think I am looking for something like:

<audio autoplay controls src="data:audio/mpeg;base64, Data goes here ...

The way it works is you have an audio file set with the audio tag and use javascript to access it. There is not a way to have the audio file play automatically.


https://javascript-tutor.net/index.php/playing-music-using-javascript/#:~:text=You%20can%20create%20a%20JavaScript,one%20of%20them%20is%20controls.

Avatar of Bill Prew
Bill Prew

I have done this two ways in HTA files from what I can see, but both require a WAV file to be invoked / played.  Not sure how to get at the system sounds that are currently configured...

Approach 1
'In the VBS code...
CreateObject("WScript.Shell").Run ".\siren.wav", 7

Open in new window

Approach 2
'In the VBS code...
Alarm.play()

' In the HTML code
<body>
<embed id="Alarm" name="Alarm" src=".\siren.wav" loop="false" hidden="true" autostart="false">
</body>

Open in new window


»bp

The following embeds some binary sound data, but the autoplay is not working.

Sounds like a bird chirping ...


 <audio autoplay controls src="data:audio/mp3;base64,/+MYxAAEaAIEeUAQAgBgNgP/////KQQ/////Lvrg+lcWYHgtjadzsbTq+yREu495tq9c6v/7vt/of7mna9v6/btUnU17Jun9/+MYxCkT26KW+YGBAj9v6vUh+zab//v/96C3/pu6H+pv//r/ycIIP4pcWWTRBBBAMXgNdbRaABQAAABRWKwgjQVX0ECmrb///+MYxBQSM0sWWYI4A++Z/////////////0rOZ3MP//7H44QEgxgdvRVMXHZseL//540B4JAvMPEgaA4/0nHjxLhRgAoAYAgA/+MYxAYIAAJfGYEQAMAJAIAQMAwX936/q/tWtv/2f/+v//6v/+7qTEFNRTMuOTkuNVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV">

Open in new window


This works from I.E., but not from an HTA file


  <bgsound src="data:audio/mp3;base64,/+MYxAAEaAIEeUAQAgBgNgP/////KQQ/////Lvrg+lcWYHgtjadzsbTq+yREu495tq9c6v/7vt/of7mna9v6/btUnU17Jun9/+MYxCkT26KW+YGBAj9v6vUh+zab//v/96C3/pu6H+pv//r/ycIIP4pcWWTRBBBAMXgNdbRaABQAAABRWKwgjQVX0ECmrb///+MYxBQSM0sWWYI4A++Z/////////////0rOZ3MP//7H44QEgxgdvRVMXHZseL//540B4JAvMPEgaA4/0nHjxLhRgAoAYAgA/+MYxAYIAAJfGYEQAMAJAIAQMAwX936/q/tWtv/2f/+v//6v/+7qTEFNRTMuOTkuNVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV">

Open in new window


IE had it's final death in June.  You want it to work in Chrome, Edge or Firefox.  If it works in those three, it should work in tertiary browsers like Brave.


I have this working


<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>test</title>
</head>
<body>
  


  <audio id="audio" autoplay="false"  src="data:audio/mp3;base64,/+MYxAAEaAIEeUAQAgBgNgP/////KQQ/////Lvrg+lcWYHgtjadzsbTq+yREu495tq9c6v/7vt/of7mna9v6/btUnU17Jun9/+MYxCkT26KW+YGBAj9v6vUh+zab//v/96C3/pu6H+pv//r/ycIIP4pcWWTRBBBAMXgNdbRaABQAAABRWKwgjQVX0ECmrb///+MYxBQSM0sWWYI4A++Z/////////////0rOZ3MP//7H44QEgxgdvRVMXHZseL//540B4JAvMPEgaA4/0nHjxLhRgAoAYAgA/+MYxAYIAAJfGYEQAMAJAIAQMAwX936/q/tWtv/2f/+v//6v/+7qTEFNRTMuOTkuNVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV">


     </audio>
<button onclick="playSound();"> Play</button>
<script>
  function playSound() {
   
  var sound = document.getElementById("audio");
  sound.play();
}
</script>


   


</body>
</html>

Open in new window


Scott, Nice code.  Works on every browser except for my HTA


Is HTA no longer supported ?


Does Bill's code work with vbscript? 


https://www.experts-exchange.com/questions/29248928/play-one-or-more-of-the-system-sounds-from-script-in-html-page.html#a43475229

Does this change anything?  putting the script in the head section and adding the language="javascript"


<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>test</title>
<script langauge="javascript">
  function playSound() {
   
  var sound = document.getElementById("audio");
  sound.play();
}
</script>
</head>
<body>
  




  <audio id="audio" autoplay="false"  src="data:audio/mp3;base64,/+MYxAAEaAIEeUAQAgBgNgP/////KQQ/////Lvrg+lcWYHgtjadzsbTq+yREu495tq9c6v/7vt/of7mna9v6/btUnU17Jun9/+MYxCkT26KW+YGBAj9v6vUh+zab//v/96C3/pu6H+pv//r/ycIIP4pcWWTRBBBAMXgNdbRaABQAAABRWKwgjQVX0ECmrb///+MYxBQSM0sWWYI4A++Z/////////////0rOZ3MP//7H44QEgxgdvRVMXHZseL//540B4JAvMPEgaA4/0nHjxLhRgAoAYAgA/+MYxAYIAAJfGYEQAMAJAIAQMAwX936/q/tWtv/2f/+v//6v/+7qTEFNRTMuOTkuNVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV">




     </audio>
<button onclick="playSound();"> Play</button>




</body>
</html>

Open in new window


Scott, when I run Bills code in VBS, I get the following error:

Error: Object doesn't support this property or method: 'Alarm.play'


Changed the '<embed' tag to '<audio'  and got the same error from HTA


Thanks


ASKER CERTIFIED SOLUTION
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

I had to stare at it for an hour, finally noticed that it was the <meta> tag that was different.

I usually don't mess w/meta tags. Change the meta tags back to what I had and you get the error that I was getting "...'Play()' is not a property or method ..."


This page shows 'play()' as a method:

https://www.w3schools.com/tags/ref_av_dom.asp


Does the updated version have a different method name than 'play()'

If I keep the meta tag this way, it screws up all my graphics, but now I know where the problem lives !


Here is my solution:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">


<head>
<meta xcontent="text/html; charset=utf-8" xhttp-equiv="Content-Type"  http-equiv="x-ua-compatible" content="ie=9" />
<xmeta http-equiv="x-ua-compatible" content="ie=9">


<title>Untitled 1</title>


  <hta:application 
     id="test"
     applicationname="test1"
     version="1">
  </hta>


<script language="javascript">
  function playSoundJS(){


     var sound = document.getElementById("audio");
     sound.play();
  }
</script>


<script language="vbscript" >
  function playSoundVBS()
     Dim sound   


     set sound = document.getElementById("audio")
     sound.play()
  End Function
</script>


</head>


<body>


<audio id="audio" autoplay="false"  src="data:audio/mp3;base64,/+MYxAAEaAIEeUAQAgBgNgP/////KQQ/////Lvrg+lcWYHgtjadzsbTq+yREu495tq9c6v/7vt/of7mna9v6/btUnU17Jun9/+MYxCkT26KW+YGBAj9v6vUh+zab//v/96C3/pu6H+pv//r/ycIIP4pcWWTRBBBAMXgNdbRaABQAAABRWKwgjQVX0ECmrb///+MYxBQSM0sWWYI4A++Z/////////////0rOZ3MP//7H44QEgxgdvRVMXHZseL//540B4JAvMPEgaA4/0nHjxLhRgAoAYAgA/+MYxAYIAAJfGYEQAMAJAIAQMAwX936/q/tWtv/2f/+v//6v/+7qTEFNRTMuOTkuNVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV">
</audio>


<button onclick="playSoundJS()"> Play via JS</button>
<button onclick="playSoundVBS()"> Play via VBS</button>


</html>

Open in new window


Can someone please test the following line of code and tell me why its not playing for me from IE, Edge, HTA ?

I can get it to play MP3 files if I change the path and type ?   Why not WAV files ?


<audio id="audio02" autoplay="false" controls  preload="auto"  type="wav" src="file:///C:\Windows\Media\chord.wav">
</audio>

Open in new window

Thank You.



Probably because you are trying to play a local resources. Just base64 the sound https://base64.guru/converter/encode/audio 

Whats wrong with trying to play a local file ?

I thought it might be easier to just play on of the system sounds rather than bring it along as base64 data.

Looking around, it looks like Microsoft always places the system sounds in the folder: C:\Windows\Media\ ...

Just thought it would be a much simpler solution ??


Did you try to see if converting the file to base64 works?


I don't know about HTA, but generally any browser is not going to let you access the file system for security reasons. In the HTA file, you can run a vbscript using CreateObject(something) with that, you can try this https://devblogs.microsoft.com/scripting/how-can-i-play-a-sound-from-within-a-script/ but why? it will be more portable to use the base64'd file where you can later transfer the same page to something else like a web app.

1.  When I run the code form that page, my system returns an error stating "System can not find specified file"

I know the path to the wav file is correct, as a sanity chk I even copied the file out of the windows folder to make sure it was not a security issue. I think it's the "sndrec32" file that can not be found on my Win 7 machine ?


2.  Did you have any input on how to get the "<audo .." object to play w/o changing the <meta> tag ?


Thanks




So this works https://www.experts-exchange.com/questions/29248928/play-one-or-more-of-the-system-sounds-from-script-in-html-page.html#a43475259


If you use that formula and just switch out the sound file, you have a solution. This is what I would do, keep it easy.

This is pretty simple.

works on I.E. and H.T.A., but not FF or Chrome.  (might if all the code was changed to JScript ?)

Tested the HTA on windows 7 and 11, worked well.


This think this is my final, final solution:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">


<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"  xhttp-equiv="x-ua-compatible" xcontent="ie=9" />
<xmeta http-equiv="x-ua-compatible" content="ie=9">


<title>Untitled 1</title>


  <hta:application 
     id="test"
     applicationname="test1"
     version="1">
  </hta>


<script language="vbscript" >
 
   '*** wmplayer arguments ***
   '. /open: Open the file, don't automatically start playing.
   '. /play: Start playing the file as soon the player is launched.
   '. /close: Close the player after playback (only works when used with /play).
   '. /fullscreen: Start the file in full-screen mode.
   '. /new: Use a new instance of the player.


   '*** Use the follow cmd if wmplayer 12 will not close on its own. ***
   'TaskKill.exe /IM WMPlayer.exe




   function MyPlay02(sSoundFile)


      dim wsh, sCmd
      set wsh = CreateObject("WScript.Shell")
      
      sCmd = "wmplayer.exe /play /close ""C:\Windows\Media\" & sSoundFile & ".wav"""
      'msgbox sCmd
      wsh.run sCmd, 0, false
      
   end function 
 
    function MyMsgbox(lStyle)
    
       msgbox "This is a test of '" & lStyle & "'", clng(lStyle), "Testing ..."
    
    end function 
 
 
</script>


</head>


<body>


<table style="width: 50%">
   <tr>
      <td style="width: 273px">Windows Sounds</td>
      <td>Windows MsgBox Styles</td>
   </tr>


   <tr>
      <td style="width: 273px"><button onclick="MyPlay02('Windows Information Bar')">Windows Information Bar</button></td>
      <td><button onclick="MyMsgbox(64)">vbInformation</button></td>
   </tr>
   <tr>
      <td style="width: 273px"><button onclick="MyPlay02('Windows Exclamation')">Windows Exclamation</button></td>
      <td><button onclick="MyMsgbox(48)">vbExclamation</button></td>
   </tr>
   <tr>
      <td style="width: 273px"><button onclick="MyPlay02('chord')">Chord</button></td>
      <td><button onclick="MyMsgbox(16)">vbCritical</button></td>
   </tr>
   <tr>
      <td style="width: 273px"><button onclick="MyPlay02('chimes')">Chimes</button></td>
      <td>&nbsp;</td>
   </tr>
   <tr>
      <td style="width: 273px"><button onclick="MyPlay02('tada')">Tada</button></td>
      <td>&nbsp;</td>
   </tr>
</table>




</html>

Open in new window