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

  • SEARCH
  • Home
  • 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 6942813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:04:25+00:00 2026-05-27T13:04:25+00:00

I am trying to play an audio file when I click the button, but

  • 0

I am trying to play an audio file when I click the button, but it’s not working, my html code is:

<html>    
    <body>
        <div id="container">
            <button id="play">
                Play Music
            </button>
        </div>
    </body>
</html>

my JavaScript is :

$('document').ready(function () {
    $('#play').click(function () {
        var audio = {};
        audio["walk"] = new Audio();
        audio["walk"].src = "http://www.rangde.org/static/bell-ring-01.mp3"
        audio["walk"].addEventListener('load', function () {
            audio["walk"].play();
        });
    });
});   

I have created a Fiddle for that too.

  • 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-27T13:04:26+00:00Added an answer on May 27, 2026 at 1:04 pm

    Which approach?

    You can play audio with <audio> tag or <object> or <embed>.
    Lazy loading(load when you need it) the sound is the best approach if its size is small. You can create the audio element dynamically, when its loaded you can start it with .play() and pause it with .pause().

    Things we used

    We will use canplay event to detect our file is ready to be played.

    There is no .stop() function for audio elements. We can only pause them. And when we want to start from the beginning of the audio file we change its .currentTime. We will use this line in our example audioElement.currentTime = 0;. To achieve .stop() function we first pause the file then reset its time.

    We may want to know the length of the audio file and the current playing time. We already learnt .currentTimeabove, to learn its length we use .duration.

    Example Guide

    1. When document is ready we created an audio element dynamically
    2. We set its source with the audio we want to play.
    3. We used ‘ended’ event to start file again.

    When the currentTime is equal to its duration audio file will stop
    playing. Whenever you use play(), it will start from the beginning.

    1. We used timeupdate event to update current time whenever audio .currentTime changes.
    2. We used canplay event to update information when file is ready to be played.
    3. We created buttons to play, pause, restart.
    $(document).ready(function() {
        var audioElement = document.createElement('audio');
        audioElement.setAttribute('src', 'http://www.soundjay.com/misc/sounds/bell-ringing-01.mp3');
        
        audioElement.addEventListener('ended', function() {
            this.play();
        }, false);
        
        audioElement.addEventListener("canplay",function(){
            $("#length").text("Duration:" + audioElement.duration + " seconds");
            $("#source").text("Source:" + audioElement.src);
            $("#status").text("Status: Ready to play").css("color","green");
        });
        
        audioElement.addEventListener("timeupdate",function(){
            $("#currentTime").text("Current second:" + audioElement.currentTime);
        });
        
        $('#play').click(function() {
            audioElement.play();
            $("#status").text("Status: Playing");
        });
        
        $('#pause').click(function() {
            audioElement.pause();
            $("#status").text("Status: Paused");
        });
        
        $('#restart').click(function() {
            audioElement.currentTime = 0;
        });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <body>
        <h2>Sound Information</h2>
        <div id="length">Duration:</div>
        <div id="source">Source:</div>
        <div id="status" style="color:red;">Status: Loading</div>
        <hr>
        <h2>Control Buttons</h2>
        <button id="play">Play</button>
        <button id="pause">Pause</button>
        <button id="restart">Restart</button>
        <hr>
        <h2>Playing Information</h2>
        <div id="currentTime">0</div>
    </body>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, I'm trying to play an HTML audio code on iPad but does not
I am trying to play audio in a Java applet but it is not
I am trying to play and audio file using DirectX dll. Audio.FromFile(Message 1.mp3).Play(); But
I'm trying to play an mp3 audio file on iPhone executing the following code:
I'm trying to play a sound file from an iPhone program. Here's the code:
I'm working on an application that will read in file paths and play audio
I am trying to play an audio file in silverlight using WCF, language C#,
I'm trying to play an audio file in a cocos2d application. Here is the
I am trying to play audio with the ACTION_VIEW intent, but regardless on what
I'm trying to play audio file at particular time which i have set.for that

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.