Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7418995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:59:02+00:00 2026-05-29T07:59:02+00:00

I wonder if someone could give me a steer here – my mind is

  • 0

I wonder if someone could give me a steer here – my mind is blank. On my page I have 3 buttons each for three different sounds (using JavaScript). What I’m trying to do is have these three buttons, then a button that plays the selected sound – and if clicked again, stops it.

I could do it in Flash but I’m a bit stumped in HTML5!

Update

Hi Guys – thanks for the responses.. this is what I’m using so far:

<div data-role="controlgroup" data-type="horizontal">
    <audio loop id="8000">
        <source src="8000.mp3">
    </audio>
    <button onclick="document.getElementById('8000').play()">16kHz</button>
    <audio loop id="18000">
        <source src="18000.mp3">
    </audio>
    <button onclick="document.getElementById('18000').play()">18kHz</button>
    <audio loop id="20000">
        <source src="20000.mp3">
    </audio>
    <button onclick="document.getElementById('18000').play()">20kHz</button>
</div>
<div data-role="controlgroup" data-type="horizontal">
    <button class="ui-body-b" onclick="document.getElementById('8000').pause(); document.getElementById('8000').currentTime = 0;" >Stop Playing</button> 

This works to a point but I want to have the buttons select the sound to be played ( not actually play it) then have a big Play sound button…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:59:03+00:00Added an answer on May 29, 2026 at 7:59 am

    It looks to me like your issue is not really related to audio but instead how to implement the buttons to get the effect you want. You say the code you posted works, so play and pause are working fine on your audio element. But the 3 buttons are starting the 3 different sounds and the last button stops any sound currently being played, whereas you want to select a sound with one of the 3 buttons and play/pause the selected one using the last button.

    If you have a variable to store the id of the last clicked sound select buttons, then use the last button to toggle between calling play() and pause()…

    <!DOCTYPE html>
    <html>
    <header>
    <script type="text/javascript">
        var selSound = 8000; //default to first sound.
        var play = false; //default to paused.
    
        function selectSound(id) {
            selSound = id;
        }
    
        function togglePlay() {
            var a = document.getElementById(selSound);
            var b = document.getElementById("playbtn");
            play = !play;
            if (play) {
                b.value = "Stop Playing";
                a.play();
            }
            else {
                b.value = "Start Playing";
                a.pause();
            }
        }
    </script>
    </header>
    <body>
        <div data-role="controlgroup" data-type="horizontal">
            <audio loop id="8000">
                <source src="8000.mp3">
            </audio>
            <button onclick="selectSound('8000')">16kHz</button>
            <audio loop id="18000">
                <source src="18000.mp3">
            </audio>
            <button onclick="selectSound('18000')">18kHz</button>
            <audio loop id="20000">
                <source src="20000.mp3">
            </audio>
            <button onclick="selectSound('18000')">20kHz</button>
        </div>
        <div data-role="controlgroup" data-type="horizontal">
              <button id="playbtn" class="ui-body-b" onclick="togglePlay()" >Start Playing</button>
        </div>
    </body>
    </html>
    

    Here the first 3 buttons simply set variable selSound to be either ‘8000’, ‘18000’ or ‘20000’, then when you press the last button to play the sound the selsound variable is used to identify the id of the audio element it will use. To begin with the first click will play a sound and the next click will stop it, and it will continue to toggle back and forth.

    This isn’t complete though and I’ve not tested it so it might not even work fully. But you will probably want the play/pause button to toggle back to ‘Start Playing’ once a sound has finished playing. Otherwise you’ll click play, the selected sound will play and finally stop, but the button will still show ‘Stop Playing’, then when you click it it will toggle to say ‘Start Playing’ and then you’ll have to click it again to start a sample playing again. So you will probably want to trap the event when a sound stops and toggle the button back to ‘Start Playing’.

    HTH

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Wonder if someone could give me a quick hand. I have 2 select queries
wonder if someone could help me with a little problem. I have session values
I wonder if someone could offer insight here: I work for an independent multimedia
I have not done a lot with WinForms so I wonder if someone could
I wonder whether someone could help me please. I have to admit I'm relatively
I wonder if someone could advise what I'm doing wrong. I have Varnish set
I wonder whether someone could help me please. I have put together a form
I wonder if someone could help me figure out how to parse a string
I wonder if someone could be kind enough to explain the function.prototype thingie (thingie!!??)
I wonder whether someone could help me please. I'm using Image Uploader from Aurigma,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.