I need to play and pause a .wav file on an asp.net page, but I can't use flash, and it needs to be the lightest solution there could be. I found how to play the sound, but I can't find a way to stop it.

this is my code: (with the element "embed" it's not working in IE, but with "bgsound" it's not working in Chrome)

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

    <script type="text/javascript">

        var soundObject = null;


        function PlaySound() {

        if (soundObject != null) {

                document.body.removeChild(soundObject);
                soundObject = null;

            }

            soundObject = document.createElement("bgsound");

            soundObject.setAttribute("src", "C:/sound1.wav");

            soundObject.setAttribute("hidden", true);

            soundObject.setAttribute("autostart", true);

            document.body.appendChild(soundObject);
        }   

    </script>

</head>

<body>

    <form id="form1" runat="server">

        <input type = "button" onclick = "PlaySound()" value = "Play Sound" />

    </form>

</body>

</html>
有帮助吗?

解决方案 3

It's not pretty, but I managed to terminate the sound like this.

function StopSound()
        {
             if (soundObject != null) {

                soundObject.setAttribute("src", "");
                soundObject = null;

            }
        }

其他提示

You can do this by using this namespace "Microsoft.DirectX.AudioVideoPlayback"

Here, an example --http://msdn.microsoft.com/en-us/library/bb324497(VS.85).aspx

This is possible using asp.net SoundPlayer Class

aspx page:

  <asp:Button ID="Button1" runat="server" Text="Play" />

    <asp:Button ID="Button2" runat="server" Text="Pause" />

HTML mark up you need to include this for previous versions,

   <object data="sound1.wav"></object>

for the newer html5 you can use this:

<audio src="sound1.wav">
  <p>Your browser does not support the audio element.</p>
</audio>

code behind file:

protected void Button1_Click(object sender, EventArgs e)
    {
        SoundPlayer playthewavfile = new SoundPlayer(@"C:/sound1.wav");
        playthewavfile.Play();        
    }
protected void Button2_Click(object sender, EventArgs e)
    {
        SoundPlayer playthewavfile = new SoundPlayer(@"C:/sound1.wav");
        playthewavfile.Stop();        
    }

EDIT:

If you want to do this purely with javascript and html try this:

I have not tested this, as I am very rusty using embed tag to parse audio file ever since html5 came out, you can try it and let me know if it works.

<HTML>
    <HEAD>
        <SCRIPT LANGUAGE="JavaScript">
            function play() {
                window.document.embeds[0].Play()
                window.document.embeds[1].Play()
            }
            function stop() {
                window.document.embeds[0].Stop()
                window.document.embeds[1].Stop()
            }
        </SCRIPT>
    </HEAD>
    <BODY>
    <EMBED SRC="sound1.wav" NAME="video" AUTOSTART="false">
        <EMBED SRC= "sound1.wav" AUTOSTART="false">
            <FORM>
                <INPUT TYPE="BUTTON" VALUE="Play" onClick="play()">
                <INPUT TYPE="BUTTON" VALUE="Pause" onClick="stop()">
            </FORM>
    </BODY>
</HTML>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top